Issues

Issue #217 new

Idea: local variable caching of filters/filter stacks

guest
created an issue

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_filtershtml_escape = filters.html_escape }}}
  • and/or maybe better yet create a single cached callable for all of these filter "stacks": {{{ M_f_unicode_filtershtml_escape = lambda args,*kwargs: M_f_unicode(M_f_filtershtml_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 :)

Comments (1)

  1. Mike Bayer repo owner

    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.

  2. Log in to comment