diff --git a/mercurial/revset.py b/mercurial/revset.py
index 0b7853f969ac69b9db735778389b62184a65c48c_bWVyY3VyaWFsL3JldnNldC5weQ==..c23c03605c59556910e643a36dd4278f0143bd87_bWVyY3VyaWFsL3JldnNldC5weQ== 100644
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -526,4 +526,5 @@
 def checkstatus(repo, subset, pat, field):
     hasset = matchmod.patkind(pat) == 'set'
 
+    mcache = [None]
     def matches(x):
@@ -529,4 +530,2 @@
     def matches(x):
-        m = None
-        fname = None
         c = repo[x]
@@ -532,8 +531,10 @@
         c = repo[x]
-        if not m or hasset:
-            m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=c)
-            if not m.anypats() and len(m.files()) == 1:
-                fname = m.files()[0]
+        if not mcache[0] or hasset:
+            mcache[0] = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=c)
+        m = mcache[0]
+        fname = None
+        if not m.anypats() and len(m.files()) == 1:
+            fname = m.files()[0]
         if fname is not None:
             if fname not in c.files():
                 return False