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

Christian Ebert blacktrash at gmx.net
Thu Nov 4 14:35:37 CDT 2010


Hi Martin,

* Martin Geisler on Thursday, November 04, 2010 at 16:55:20 +0100
> 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?

In my simpleton way I ran test-keyword.t quite often before and
after, and with the patch it seemed (slightly) faster. Perhaps
because some commands don't need any regex at all.

> 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.

Yes, yes.

> 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.

I tried this already and could not get it to work, I get:

TypeError: '_sre.SRE_Pattern' object is not callable

But I'll play around with it some more. I never used decorators
so far, so I'm probably doing something really stupid.

Thanks for the feedback!

c
-- 
Die Wolke Wolfgang
Eine Kindergeschichte mit Bildern. Von Michael Weber.
Das Buch   -->> http://www.blacktrash.org/baustellen#wolkewolfgang
Online     -->> http://www.blacktrash.org/wolkewolfgang


More information about the Mercurial-devel mailing list