Skip to content

fix(worker): eliminate N+1 query in bundle analysis asset association - #1892

Open
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/bundle-analysis-n-plus-1-query
Open

fix(worker): eliminate N+1 query in bundle analysis asset association#1892
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/bundle-analysis-n-plus-1-query

Conversation

@sentry

@sentry sentry Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

This PR addresses an N+1 query performance issue in the BundleAnalysisProcessor task, specifically within the _associate_bundle_report_assets_by_module_names method.

Problem:
Previously, when associating assets by module names, the code iterated over each asset and called asset.modules() individually. Each call to asset.modules() opened a new database session and executed a separate SELECT modules... query. This resulted in an N+1 query pattern, where N is the number of assets in a bundle, leading to excessive database calls and performance degradation.

Solution:
To resolve this, a new static method _get_asset_module_names_bulk has been introduced in BundleAnalysisReport. This method now performs a single, bulk SQL query to fetch all relevant (asset_uuid, module_name) pairs for all JavaScript assets within a given bundle report. The _associate_bundle_report_assets_by_module_names method has been refactored to utilize this bulk-fetching mechanism. It now calls the bulk helper once for the previous bundle report and once for the current bundle report, then processes the data in memory.

This change reduces the database query count from O(N) (where N is the number of assets) to a constant O(1) per bundle report comparison, significantly improving the performance of the bundle analysis process.

Legal Boilerplate

Look, I get it. The entity doing business as "Codecov" is owned by Harness, Inc. In 2026 Harness acquired Codecov and as a result Harness is going to need some rights from me in order to utilize my contributions in this PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Harness can use, modify, copy, and redistribute my contributions, under Harness's choice of terms.

Fixes WORKER-Z5J

@codspeed-hq

codspeed-hq Bot commented Aug 2, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 9 untouched benchmarks


Comparing seer/fix/bundle-analysis-n-plus-1-query (85e4c46) with main (f9c9cb2)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (d8942db) during the generation of this report, so f9c9cb2 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@codecov-notifications

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@codecov

codecov Bot commented Aug 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.83%. Comparing base (d8942db) to head (85e4c46).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1892   +/-   ##
=======================================
  Coverage   91.83%   91.83%           
=======================================
  Files        1332     1332           
  Lines       51830    51836    +6     
  Branches     1647     1646    -1     
=======================================
+ Hits        47598    47604    +6     
  Misses       3911     3911           
  Partials      321      321           
Flag Coverage Δ
apiunit 94.75% <ø> (ø)
sharedintegration 36.76% <12.50%> (-0.01%) ⬇️
sharedunit 84.88% <100.00%> (+<0.01%) ⬆️
workerintegration 58.51% <ø> (ø)
workerunit 90.39% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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.

0 participants