Repository navigation
perf(api): build related_fields' nested serializers once per page - #16992
Open
blakeaowens wants to merge 1 commit into
Open
blakeaowens wants to merge 1 commit into
blakeaowens wants to merge 1 commit into
Conversation
A 1000-row ?related_fields=true findings page spent most of its time building serializers: get_related_fields built a FindingRelatedFieldsSerializer for every row, which built a FindingTestSerializer (three nested levels) and a JIRAIssueSerializer for every row too. Each is now built once per serializer instance and reused for every row. related_fields' jira url read each issue's JIRA project and instance with a query each, so a related_fields page now prefetches jira_issue__jira_project__jira_instance. Plain pages are unchanged. Response bodies are byte-identical. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HJqD5EJ6LWVNAPF4qew47G
This branch has not been deployed
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.
Description
A 1000-row
GET /api/v2/findings/?related_fields=truepage spent most of its wall time in Python, and with JIRA enabled it also ran two queries per finding that has a JIRA issue.FindingSerializer.get_related_fieldsbuilt a newFindingRelatedFieldsSerializerfor every row, and that built a newFindingTestSerializer(three levels of nested serializers) and a newJIRAIssueSerializerfor every row too. Building a serializer builds its whole field tree, and nothing in those trees depends on the row. Each is now built once per serializer instance (acached_property) and reused for every row the list renders, the same pattern perf(api): load the relations of prefetched duplicate findings once per page #16789 used in the prefetcher. A page withoutrelated_fields=truestill builds none of them.related_fields.jira.urlreads the issue's JIRA project and its instance. Nothing loaded them with the page, so each issue cost two queries.FindingViewSet.get_querysetnow prefetchesjira_issue__jira_project__jira_instance, only whenrelated_fields=true, so plain pages run exactly the statements they ran before.Response bodies are byte-identical before and after.
Measured on 1000 findings (a third with a JIRA issue linked to a project), median of 9 requests, before and after under the same load:
related_fields=truerelated_fields=trueA profile of the before request put about 6.3 s (under the profiler) in DRF
get_fields, 6,537 calls, almost all fromget_test.Test results
New
unittests/test_apiv2_finding_related_fields_reuse.py:dev: twice for 2 rows),related_fieldsadds the same statements to 2 rows as to 6, measured against the plain page (ondev: 4 and 12),2 of the 5 fail on
devfor those reasons, and all 5 pass with the change. The paired downstream suite (finding list query scaling and performance tests) passes unchanged.Documentation
No documentation change: nothing a user sees or configures changes.
Checklist
dev.devbranch.🤖 Generated with Claude Code
https://claude.ai/code/session_01HJqD5EJ6LWVNAPF4qew47G