Skip to content

Preload variant and product when calculating taxes to avoid an N+1#6497

Open
ikraamg wants to merge 1 commit into
solidusio:mainfrom
ikraamg:perf/tax-calculator-variant-product-preload
Open

Preload variant and product when calculating taxes to avoid an N+1#6497
ikraamg wants to merge 1 commit into
solidusio:mainfrom
ikraamg:perf/tax-calculator-variant-product-preload

Conversation

@ikraamg

@ikraamg ikraamg commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Calculating order taxes resolves each line item's tax category through its variant, falling back to the product when the variant has none. With those associations unloaded, that was two queries per line item, and taxes are recalculated on every order recalculation.

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

Comment thread core/spec/models/spree/tax_calculator/default_spec.rb Outdated
Comment thread core/spec/models/spree/tax_calculator/default_spec.rb
Comment thread core/spec/models/spree/tax_calculator/default_spec.rb
Comment thread core/spec/models/spree/tax_calculator/default_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 (3adc5a8).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6497   +/-   ##
=======================================
  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.

Calculating order taxes read each line item's tax category through its
variant and, when the variant had none, through its product. With the
associations unloaded that meant two queries per line item, every time
taxes were recalculated.

Preloading the variant and product for all line items keeps that load
constant regardless of how many items the order has.
@ikraamg ikraamg force-pushed the perf/tax-calculator-variant-product-preload branch from 2d68876 to 3adc5a8 Compare June 26, 2026 03:30
Comment thread core/spec/models/spree/tax_calculator/default_spec.rb
@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