mako
mako copied to clipboard
Idea: local variable caching of filters/filter stacks
Migrated issue, originally created by Anonymous
Analyzing the generated .py files from my current project, there's lots of this pattern, as my default_filters are ['h', 'unicode']
:
__M_writer(unicode(filters.html_escape(x)))
__M_writer(u'some_constant_stuff')
__M_writer(unicode(filters.html_escape(y)))
__M_writer(u'more_constant_stuff')
__M_writer(unicode(filters.html_escape(z)))
__M_writer(u'even_more_constants')
so it would seem to make sense, to reduce name lookups (in favor of LOAD_FAST) to either
- cache the filter callables as locals in the function (as is being done with
__M_writer
) to something like__M_f_unicode = unicode __M_f_filters__html_escape = filters.html_escape
- and/or maybe better yet create a single cached callable for all of these filter "stacks":
__M_f_unicode___filters__html_escape = lambda *args,**kwargs: __M_f_unicode(__M_f_filters__html_escape(*args,**kwargs))
and have the codegen use those instead.
I haven't studied the codegen more deeply to find out what sort of magic this would require though :)
Michael Bayer (@zzzeek) wrote:
not to mention turning all those write() calls into one:
__M_writer("foo %s bar %s" % (
unicode(filters.html_escape(x))
unicode(filters.html_escape(y))
)
all good things.