Skip to content

Preload line item variants and products in the promotions order adjuster - #6525

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

Preload line item variants and products in the promotions order adjuster#6525
ikraamg wants to merge 1 commit into
solidusio:mainfrom
ikraamg:perf/promotions-variant-product-preload

Conversation

@ikraamg

@ikraamg ikraamg commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

SolidusPromotions::OrderAdjuster preloads each line item's adjustments, but DiscountOrder#adjust_line_items then reads line_item.variant.product for every line item and nothing preloaded that chain. Line items hold distinct variants, so the query cache cannot collapse the lookups.

adjust_line_items runs for every lane before its benefits are consulted, so a store that installs SolidusPromotions and configures no promotions at all still pays one variant and one product lookup per line item on every recalculation.

  • preloads {variant: :product} over the line items, as its own pass since shipments have no variant
  • variant and product loads stay constant regardless of cart size
  • adds two specs asserting each table is loaded in a single query

Recalculating a five line item order drops from 31 to 25 queries, and a ten line item order from 38 to 25. The saving appears when the order is loaded fresh from the database, which is the ordinary storefront path of loading a cart and recalculating it.

Both specs were confirmed to fail without the fix, reporting "expected 1 query, but 3 were made" on a three line item order.

Necessary because DiscountOrder reads `line_item.variant.product` for
every line item to check whether the product is promotionable, and
nothing preloaded that chain. Line items hold distinct variants, so the
query cache cannot collapse the lookups: an order paid one variant query
per line item on every recalculation, and building a cart performs one
recalculation per item added.

This runs whether or not any promotion exists, so a store that installs
the gem and configures nothing still pays it. On a freshly loaded
twenty-five item cart with no promotions at all, those lookups were
thirty-one of the recalculation's thirty-eight queries.

Shipments have no variant association, so this cannot be folded into the
existing adjustments preload and runs as its own pass over line items.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@ikraamg
ikraamg requested a review from a team as a code owner July 29, 2026 20:45
@github-actions github-actions Bot added the changelog:solidus_promotions Changes to the solidus_promotions gem label Jul 29, 2026
@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.38%. Comparing base (72f9fcd) to head (bcefff2).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6525      +/-   ##
==========================================
- Coverage   95.07%   92.38%   -2.70%     
==========================================
  Files         460       42     -418     
  Lines        7924      788    -7136     
==========================================
- Hits         7534      728    -6806     
+ Misses        390       60     -330     

☔ 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.

@mamhoff mamhoff left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, thank you!

@mamhoff mamhoff added the backport-v4.7 Backport this pull-request to v4.7 label Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-v4.7 Backport this pull-request to v4.7 changelog:solidus_promotions Changes to the solidus_promotions gem

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants