Repository navigation
perf: build ResourceLoader's Django template engine once per process - #972
Closed
kdmccormick wants to merge 1 commit into
Closed
kdmccormick wants to merge 1 commit into
kdmccormick wants to merge 1 commit into
Conversation
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>
Member
Author
|
Closed in favor of #973 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
The XBlock API for rendering templates,
ResourceLoader.render_django_template, built a new template engine on every call. Building the engine calls Django'sget_installed_libraries(), which scans thetemplatetagspackage 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_APPSchanges, which only happens in tests that useoverride_settings. Compiled templates are still not cached. Thetransandblocktranstags inxblock.utils.templatetags.i18nkeep 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.