Skip to content

Use hash map to improve performance of item separation in SourceItem's SaveMultiple - #3464

Open
stollr wants to merge 1 commit into
magento:developfrom
stollr:separation_improvement
Open

Use hash map to improve performance of item separation in SourceItem's SaveMultiple#3464
stollr wants to merge 1 commit into
magento:developfrom
stollr:separation_improvement

Conversation

@stollr

@stollr stollr commented Aug 6, 2026

Copy link
Copy Markdown

Description (*)

This PR replaces the O(n × m) nested-loop implementation of separateExistingAndNewItems with an O(n + m) hash-map-based approach.

Problem

The original implementation used two nested foreach loops to match input source items against stored database rows. The inner loop has no early exit (break), so it iterates
through all stored rows for every input item. This results in quadratic time complexity — with 10,000 items all existing in the database, the method took 289 seconds in a
real-world benchmark.

Solution

The optimized version builds a two-level hash map ($indexedSourceItems[$sourceCode][$sku]) from the input items upfront. The DB query then iterates over the (typically smaller)
result set and performs O(1) lookups into the hash map. This reduces complexity from O(n × m) to O(n + m).

Performance comparison

Real-world benchmark: ~10,000 items, all existing in DB with the old algorithm: 289s. With the new algorithm: 0.32s (~900 times faster).

Fixed Issues (if relevant)

Fixes #3463: Nested loop causes quadratic performance degradation in SourceItem SaveMultiple

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready for Review

Development

Successfully merging this pull request may close these issues.

Nested loop causes quadratic performance degradation in SourceItem SaveMultiple

1 participant