Skip to content

perf: build ResourceLoader's Django template engine once per process - #972

Closed
kdmccormick wants to merge 1 commit into
masterfrom
kdmccormick/cache-resource-loader-engine
Closed

kdmccormick wants to merge 1 commit into
masterfrom
kdmccormick/cache-resource-loader-engine

Conversation

@kdmccormick

@kdmccormick kdmccormick commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

AI Note: This PR was authored with Claude Fable 5.2 and manually reviewed by @kdmccormick.

Context

The XBlock API for rendering templates, ResourceLoader.render_django_template, built a new template engine on every call. Building the engine calls Django's get_installed_libraries(), which scans the templatetags package of every installed app. In openedx-platform that takes a nontrivial amount of time: in local dev, it scanning installed libraries took on average 3 ms (and subsequent calls took about the same amount of time).

For XBlocks which call ResourceLoader once, this has not been a noticeable performance issue.

But now, we are refactoring ProblemBlock, DiscussionBlock, and VideoBlock to render templates using ResourceLoader--up unti lthis point, they've used openedx-platform-coupled django rendering up until this point, which avoided this performance pitfall. For ProblemBlock in particular, this means that it'd call ResourceLoader ~9 times per render. For a unit with 5 problems, that's 45 * 3ms == 135ms, which becomes a nontrivial performance regression.

Optimization: Cache get_installed_libraries

This commit makes it so ResourceLoader builds the engine once and reuses it. It is rebuilt when INSTALLED_APPS changes, which only happens in tests that use override_settings. Compiled templates are still not cached. The trans and blocktrans tags in xblock.utils.templatetags.i18n keep state on the template node that is only correct for the node's first render.

Merge considerations

This is not urgent for the majority of XBlocks.

For ProblemBlock, it might be urgent: we need to fix localization on the extracted ProblemBlock, which means merging openedx/xblocks-core#344 or finding an alternative to it. I'm not sure if a 100+ ms regression per assessment unit is acceptable.

ResourceLoader.render_django_template built a new template engine on
every call. Building it calls Django's get_installed_libraries(), which
scans the templatetags package of every installed app. In
openedx-platform that takes about 3 ms, so it dominated the cost of
rendering a small template: 3.4 ms per render, of which 0.16 ms was
loading, parsing and rendering the template itself.

Build the engine once and reuse it. It is rebuilt when INSTALLED_APPS
changes, which only happens in tests that use override_settings.

Compiled templates are still not cached. The trans and blocktrans tags
in xblock.utils.templatetags.i18n keep state on the template node that
is only correct for the node's first render.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@kdmccormick

Copy link
Copy Markdown
Member Author

Closed in favor of #973

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant