[PATCH 2 of 2 V2] check-code: build translation table for repquote in global for efficiency

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Mon Jun 20 12:00:33 EDT 2016


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1466437839 -32400
#      Tue Jun 21 00:50:39 2016 +0900
# Node ID 9152f357bce542ee363960e264832dd2c67bbef9
# Parent  8d6c4af8cecd562a8769a7fb78e35b717c3d800d
check-code: build translation table for repquote in global for efficiency

Rebuilding translation table (256 size) at each repquote() invocations
is redundant.

For example, this patch decreases user time of command invocation
below from 18.297s to 13.445s (about -27%) on a Linux box. This
command is main part of test-check-code.t.

    hg locate | xargs python contrib/check-code.py --warnings --per-file=0

This patch adds "_repquote" prefix to functions and variables factored
out from repquote() to avoid conflict of name in the future.

diff --git a/contrib/check-code.py b/contrib/check-code.py
--- a/contrib/check-code.py
+++ b/contrib/check-code.py
@@ -50,25 +50,26 @@ def compilere(pat, multiline=False):
             pass
     return re.compile(pat)
 
+# check "rules depending on implementation of repquote()" in each
+# patterns (especially pypats), before changing around repquote()
+_repquotefixedmap = {' ': ' ', '\n': '\n', '.': 'p', ':': 'q',
+                     '%': '%', '\\': 'b', '*': 'A', '+': 'P', '-': 'M'}
+def _repquoteencodechr(i):
+    if i > 255:
+        return 'u'
+    c = chr(i)
+    if c in _repquotefixedmap:
+        return _repquotefixedmap[c]
+    if c.isalpha():
+        return 'x'
+    if c.isdigit():
+        return 'n'
+    return 'o'
+_repquotett = ''.join(_repquoteencodechr(i) for i in xrange(256))
+
 def repquote(m):
-    # check "rules depending on implementation of repquote()" in each
-    # patterns (especially pypats), before changing this function
-    fixedmap = {' ': ' ', '\n': '\n', '.': 'p', ':': 'q',
-                '%': '%', '\\': 'b', '*': 'A', '+': 'P', '-': 'M'}
-    def encodechr(i):
-        if i > 255:
-            return 'u'
-        c = chr(i)
-        if c in fixedmap:
-            return fixedmap[c]
-        if c.isalpha():
-            return 'x'
-        if c.isdigit():
-            return 'n'
-        return 'o'
     t = m.group('text')
-    tt = ''.join(encodechr(i) for i in xrange(256))
-    t = t.translate(tt)
+    t = t.translate(_repquotett)
     return m.group('quote') + t + m.group('quote')
 
 def reppython(m):
diff --git a/tests/test-contrib-check-code.t b/tests/test-contrib-check-code.t
--- a/tests/test-contrib-check-code.t
+++ b/tests/test-contrib-check-code.t
@@ -276,7 +276,11 @@ web templates
   >           ''' "%-6d \n 123456 .:*+-= foobar")
   > EOF
 
-  $ "$check_code" stringjoin.py
+(Checking multiple invalid files at once examines whether caching
+translation table for repquote() works as expected or not. All files
+should break rules depending on result of repquote(), in this case)
+
+  $ "$check_code" stringjoin.py uigettext.py
   stringjoin.py:1:
    > foo = (' foo'
    string join across lines with no space
@@ -301,9 +305,6 @@ web templates
   stringjoin.py:8:
    >        'bar foo-'
    string join across lines with no space
-  [1]
-
-  $ "$check_code" uigettext.py
   uigettext.py:1:
    > ui.status("% 10s %05d % -3.2f %*s %%"
    missing _() in ui message (use () to hide false-positives)


More information about the Mercurial-devel mailing list