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.
--- a/contrib/check-code.py Tue Jun 21 00:50:39 2016 +0900
+++ b/contrib/check-code.py Tue Jun 21 00:50:39 2016 +0900
@@ -50,25 +50,26 @@
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):
--- a/tests/test-contrib-check-code.t Tue Jun 21 00:50:39 2016 +0900
+++ b/tests/test-contrib-check-code.t Tue Jun 21 00:50:39 2016 +0900
@@ -276,7 +276,11 @@
> ''' "%-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 @@
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)