Skip to content

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

Merged
mamhoff merged 1 commit into
solidusio:mainfrom
ikraamg:perf/tax-calculator-variant-product-preload
Jul 21, 2026
Merged

Preload variant and product when calculating taxes to avoid an N+1#6497
mamhoff merged 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 92.02%. Comparing base (b6fbbe7) to head (57af561).
⚠️ Report is 17 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6497   +/-   ##
=======================================
  Coverage   92.02%   92.02%           
=======================================
  Files        1035     1035           
  Lines       21125    21126    +1     
=======================================
+ Hits        19440    19441    +1     
  Misses       1685     1685           

☔ 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 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
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.
@mamhoff
mamhoff force-pushed the perf/tax-calculator-variant-product-preload branch from 3adc5a8 to 57af561 Compare July 16, 2026 09:39
@mamhoff
mamhoff merged commit cdcdfaf into solidusio:main Jul 21, 2026
67 of 68 checks passed
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.

4 participants