Repository navigation
Conversation
d8709e1 to
876c1b5
Compare
1a2eced to
c69971b
Compare
1f02395 to
5be1e17
Compare
|
|
||
| @ddt.data( | ||
| (ModuleStoreEnum.Type.split, 2, 26), | ||
| (ModuleStoreEnum.Type.split, 2, 25), |
There was a problem hiding this comment.
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 isrepr(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.
ffa001d to
5be1e17
Compare
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>
5be1e17 to
6f0bbdf
Compare
Description
get_all_partitions_for_course()is wrapped in@request_cached(), andrequest_cached()builds its cache key by callingstr()on each argument. For a course block, that runsXBlock.__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 throughget_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
RequestCachedirectly. Generating those is the part that does database queries. The static partitions incourse.user_partitionsare 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.Why this is safe. The three built-in dynamic partition generators (enrollment track, content type gating, teams) read only
course.idand 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:
course_index(used by the authoring MFE), 975-block course with content groupsoutline?view=full, same coursecourse_index, 881-block course with no content groupsAll 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 samerepr()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
TestGetAllPartitionsForCourseCaching:pytest xmodule/partitions/tests/test_partitions.pyactive_only=Truegets its own entry, every caller gets the same list, and the key is the course'srepr. For aMockcourse in a test, thatreprchanges with each object and doesn't includeuser_partitions. (With real course blocks, the old key did pick up partition edits.)test_block.py, contentstoretest_utils.py, course settings, the v1 course index API, and the v2 outline API.split_test, content type gating, and team partitions. Run the content type gating tests separately fromsplit_testuntil test: fix test isolation bugs that broke content type gating tests #39448 is merged.course_indexrequest time before and after.Deadline
None.
Other information
@request_cached()functions also take course blocks as arguments, such ascourse_quality._get_sections()andcourse_liveis_enabled(). They run only a few times per request, so I left them alone. A more general fix would be forrequest_cached()to refuse XBlocks, or key them by location andid(). That would change every caller, so it isn't part of this PR.🤖 Generated with Claude Code