sqlalchemy / sqlalchemy/mako

Idea: local variable caching of filters/filter stacks

Open
#217 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

compiler feature low priority
Dominant language
Python
Stars
459
Forks
90
PR merge metrics
No merged PRs in 30d

Description

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 :)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated .py pattern shown in the issue and locate the Mako code-generation path that emits filter calls. Compare the proposed local or stacked caching approaches, then verify that generated output preserves filter behavior while reducing repeated name lookups.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.