[PATCH 1 of 2 resend] keyword: compile regexes on demand

Martin Geisler mg at aragost.com
Thu Nov 4 10:55:20 CDT 2010


Christian Ebert <blacktrash at gmx.net> writes:

> # HG changeset patch
> # User Christian Ebert <blacktrash at gmx.net>
> # Date 1288791461 -3600
> # Node ID 2ce1ff53e29f4b775ed550c13beb42da3942523e
> # Parent  0e0a52bd58f941c00b2a1d57f23676fa486e58c3
> keyword: compile regexes on demand

Are you sure this is faster? I tried to see how long the old code took
and here it's very fast:

% python -m timeit \
  -s "import re" \
  -s "escaped = 'RCSfile|Author|Header|Source|Date|RCSFile|Id|Revision'" \
  "kw = re.compile(r'\$(%s)\$' % escaped)" \
  "kwexp = re.compile(r'\$(%s): [^$\n\r]*? \$' % escaped)"
100000 loops, best of 3: 2.52 usec per loop

(I got the string for escaped from kwdemo.)

I don't think caching this is worth it since we only execute this code
once as far as I can see.

> +    def rekw(self):
> +        '''Compiles regex for unexpanded keywords on demand.'''
> +        if self.re_kw is None:
> +            self.re_kw = re.compile(r'\$(%s)\$' % self.escape())
> +        return self.re_kw

We have a mercurial.util.propertycache decorator for this kind of code.
The advantage of using that is that it will replace the rekw method with
an attribute of the same name after the first (and only) execution.

-- 
Martin Geisler

aragost Trifork
Professional Mercurial support
http://aragost.com/mercurial/


More information about the Mercurial-devel mailing list