Skip to content

perf: stop rendering the whole course for the partitions cache key - #39443

Open
ormsbee wants to merge 1 commit into
openedx:masterfrom
ormsbee:ormsbee/partitions-request-cache-key
Open

ormsbee wants to merge 1 commit into
openedx:masterfrom
ormsbee:ormsbee/partitions-request-cache-key

Conversation

@ormsbee

@ormsbee ormsbee commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Description

get_all_partitions_for_course() is wrapped in @request_cached(), and request_cached() builds its cache key by calling str() on each argument. For a course block, that runs XBlock.__repr__, which renders the value of every field on the course. That takes about 0.2 ms per call on its own, and more inside a busy request.

Studio's course outline calls this function four or five times per outline node: once from get_user_partition_info(), and then three or four more times through get_visibility_partition_info(). On a 975-block course that is about 1,570 calls per request. Each call is a cache hit, but building the key to find it cost far more than the work being cached. In a cProfile of the course index API, XBlock.__repr__ was 1.22 s of the 3.99 s profiled.

This PR drops the decorator and caches only the dynamic partitions, using RequestCache directly. Generating those is the part that does database queries. The static partitions in course.user_partitions are read from the course on every call, and each call builds a new list.

The key for the dynamic partitions is built from what the generators read:

  • course.id.
  • The IDs of the static partitions. The generators check these so they don't create a partition with an ID that's already taken.

Why this is safe. The three built-in dynamic partition generators (enrollment track, content type gating, teams) read only course.id and the static partition IDs from the course. Everything else they use comes from the database or settings. The teams generator gets team sets by course key, not from the course object. So the key changes whenever anything the dynamic partitions depend on changes on the course object. That includes adding or removing static partitions during a request, which Studio's group configuration handlers do. Editing a static partition without changing its ID doesn't change the key, and doesn't need to: the edited partition is read from the course, and the dynamic partitions don't depend on it.

A dynamic partition generator from a plugin could read another course field. If that field changed during a request, this function would now return cached dynamic partitions instead of generating them again. I don't know of any generator that does this.

Results

Measured in-process in a Tutor dev Studio container with production settings and warm caches, median of 7 requests:

master this PR
v1 course_index (used by the authoring MFE), 975-block course with content groups 1.94 s 1.54 s
v2 outline?view=full, same course 2.71 s 1.38 s
v1 course_index, 881-block course with no content groups 1.63 s 1.25 s

All three responses are byte-for-byte identical before and after.

These numbers are from an earlier version of this PR, which cached the whole result keyed on repr(course.user_partitions). I haven't measured the current version end to end. A cache hit now costs about 3 µs whatever the number of content groups, against 4 to 17 µs for 3 to 50 groups in the earlier version. At about 1,570 calls per request, that's a few milliseconds.

The v2 full outline was about half a second slower than v1 on master. This PR closes that gap: the extra time was all in XBlock.__repr__. I didn't work out why the same repr() calls cost more on the v2 path.

LMS callers use the same function: the user partitions block transformer, masquerade, and PartitionService.course_partitions. They now get the cheaper key too, but they call it far fewer times per request, and I haven't measured them.

Testing instructions

  • New tests in TestGetAllPartitionsForCourseCaching: pytest xmodule/partitions/tests/test_partitions.py
    • Six of the eight fail on master. On master the whole result is cached, a call with active_only=True gets its own entry, every caller gets the same list, and the key is the course's repr. For a Mock course in a test, that repr changes with each object and doesn't include user_partitions. (With real course blocks, the old key did pick up partition edits.)
  • The tests for this function's callers also pass locally:
    • Studio, 483 tests: group configurations, test_block.py, contentstore test_utils.py, course settings, the v1 course index API, and the v2 outline API.
    • LMS, 121 tests: the user partitions transformer, masquerade, experiments, split_test, content type gating, and team partitions. Run the content type gating tests separately from split_test until test: fix test isolation bugs that broke content type gating tests #39448 is merged.
  • To see the effect, open the course outline in Studio on a large course and compare the course_index request time before and after.

Deadline

None.

Other information

  • This is the largest single cost in the course outline, but it isn't the only one. Other costs, to be handled in separate PRs:
  • Other @request_cached() functions also take course blocks as arguments, such as course_quality._get_sections() and course_live is_enabled(). They run only a few times per request, so I left them alone. A more general fix would be for request_cached() to refuse XBlocks, or key them by location and id(). That would change every caller, so it isn't part of this PR.

🤖 Generated with Claude Code


@ddt.data(
(ModuleStoreEnum.Type.split, 2, 26),
(ModuleStoreEnum.Type.split, 2, 25),

@ormsbee ormsbee Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query that went away is the teams.content_groups_for_teams waffle flag lookup that the teams partition generator does in create_team_set_partition(). It's one of the dynamic partition generators that get_all_partitions_for_course() runs.

This test creates and publishes its course earlier in the same test as the get_blocks() call it measures, so they share a RequestCache. (CacheIsolationMixin only clears caches between tests.) Publishing builds the block structure, and the user partitions transformer's collect() calls get_all_partitions_for_course() each time.

  • On master, the @request_cached() key is repr(course), which changes as the test adds blocks. So the measured call always missed and generated the dynamic partitions again.
  • With this PR, the key is the course ID plus the static partition IDs, which don't change while the course is built. So the measured call reuses the partitions cached during publishing, and skips the flag query.

A real uncached request in production wouldn't have those partitions cached yet, so it would still make that query. The lower count comes from how this test is set up, not from a change in production behavior.

Claude traced this by logging the cache key and hit/miss for each call on master and on this branch, and diffing the captured queries between the two runs.

@ormsbee
ormsbee force-pushed the ormsbee/partitions-request-cache-key branch from ffa001d to 5be1e17 Compare November 28, 2026 00:19
get_all_partitions_for_course() was wrapped in @request_cached(), which
builds its key by calling str() on each argument. For a course block
that runs XBlock.__repr__, which renders every field on the course.
Studio's course outline calls this function about 1,570 times per
request on a 975-block course, and building the key cost far more than
the work being cached.

Drop the decorator and cache only the dynamic partitions, which are the
part that does database queries, using RequestCache directly. Static
partitions are read from the course on every call, and each call builds
a new list. The key is what the dynamic partition generators read from
the course: its ID and the IDs of its static partitions. So it changes
when static partitions are added or removed during a request, which
Studio's group configuration handlers do.

Also add type annotations to get_all_partitions_for_course().

TestGetBlocksQueryCounts.test_query_counts_uncached now expects 25
queries instead of 26. The test creates and publishes its course earlier
in the same request as the get_blocks() call it measures, and publishing
generates the dynamic partitions. The old key, repr(course), changed as
the test added blocks, so the measured call generated them again. The
new key doesn't change while the course is built, so the measured call
reuses the cached partitions and skips the teams partition generator's
waffle flag query. A real uncached request would still make that query.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ormsbee
ormsbee force-pushed the ormsbee/partitions-request-cache-key branch from 5be1e17 to 6f0bbdf Compare November 28, 2026 00:22
@ormsbee
ormsbee marked this pull request as ready for review November 28, 2026 00:54

This branch has not been deployed

No deployments
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