[PEAK] PEAK-Rules indexing performance
Phillip J. Eby
pje at telecommunity.com
Sat Jan 10 16:37:19 EST 2009
At 09:25 PM 1/10/2009 +0100, Alberto Valverde wrote:
>Phillip J. Eby wrote:
>>At 09:12 PM 1/9/2009 +0100, Alberto Valverde wrote:
>>
>>>After some peeking at peak.rules internals, motivated by the meta
>>>function recipe you posted some months ago to optimize the
>>>"isclass" test, I thought about using them to simplify some rules
>>>by "wrapping" several related checks under a function without
>>>incurring in performance penalties due to non-statically analyzable predicates.
>>
>>You can't really do that; meta-functions have to expand back into
>>statically-analyzable predicates, and so will these "predicate
>>functions". Essentially, both meta-functions are like macros, and
>>predicate functions will be like macros that take effect at runtime
>>instead of compile time. ;-)
>
>Yeah, I understand that the need to expand to statically analyzable
>predicates, I must have expressed myself wrong. What I meant is to
>wrap several statically analyzable checks like: "(isclass(obj) and
>issubclass(obj, Foo)) or isinstance(obj, Foo)" into something nicer
>like: "issubclass_or_instance(obj, Foo)". Mainly to clean up the
>hairy code in here [1]. Anyway, I think this could probably be done
>with a meta function (right?) but first I need to study the source better...
Yes, static (i.e. non-generic) predicate functions can be inlined
using meta-functions. You would do something like:
def issubclass_or_instance(ob, cls):
"""This is the "regular" function"""
return isclass(obj) and issubclass(ob, cls) or isinstance(ob, cls)
@meta_function(issubclass_or_instance)
def compile_issubclass_or_instance(__builder__, ob, cls):
"""This is the compiled version"""
__builder__.push({'ob':ob, 'cls':cls,
'classtypes':classtypes}) # temporarily bind vars
result = __builder__.parse("isinstance(obj, classtypes) and
issubclass(ob, cls) or isinstance(ob, cls)")
__builder__.pop()
return result
I've left the definition of 'classtypes' as an exercise for the
reader. ;-) (Also, technically speaking, for this to be correct one
should include isinstance and issubclass in the push, so as to ensure
that the caller didn't change or mask them. That is a downside of
macros in general, of course.)
More information about the PEAK
mailing list