Skip to content

Preload variants when building inventory units to avoid an N+1#6498

Open
ikraamg wants to merge 1 commit into
solidusio:mainfrom
ikraamg:perf/inventory-unit-builder-preload-variant
Open

Preload variants when building inventory units to avoid an N+1#6498
ikraamg wants to merge 1 commit into
solidusio:mainfrom
ikraamg:perf/inventory-unit-builder-preload-variant

Conversation

@ikraamg

@ikraamg ikraamg commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Building inventory units for an order's proposed shipments reads each line item's variant, and the variant's product when resolving its shipping category, with a separate query per line item. This runs whenever shipments are rebuilt, such as the delivery step of checkout.

  • preloads each line item's variant and product before building the units
  • variant and product loads stay constant regardless of order size
  • adds a spec asserting line item variants load in a single query

Building inventory units loads each line item's variant, and the
variant's product when resolving the shipping category, with a query
per line item. This runs whenever proposed shipments are rebuilt.

Preloading the variant and product for all line items keeps that load
constant regardless of how many items the order has.
Comment thread core/spec/models/spree/stock/inventory_unit_builder_spec.rb
Comment thread core/spec/models/spree/stock/inventory_unit_builder_spec.rb
Comment thread core/spec/models/spree/stock/inventory_unit_builder_spec.rb
@github-actions github-actions Bot added the changelog:solidus_core Changes to the solidus_core gem label Jun 26, 2026
@codecov

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.69%. Comparing base (8d781ac) to head (ee909aa).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6498   +/-   ##
=======================================
  Coverage   89.68%   89.69%           
=======================================
  Files         993      993           
  Lines       20863    20864    +1     
=======================================
+ Hits        18712    18713    +1     
  Misses       2151     2151           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ikraamg ikraamg marked this pull request as ready for review June 26, 2026 06:35
@ikraamg ikraamg requested a review from a team as a code owner June 26, 2026 06:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog:solidus_core Changes to the solidus_core gem

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants