Use hash map to improve performance of item separation in SourceItem's SaveMultiple - #3464
Open
stollr wants to merge 1 commit into
Open
Use hash map to improve performance of item separation in SourceItem's SaveMultiple#3464stollr wants to merge 1 commit into
stollr wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description (*)
This PR replaces the O(n × m) nested-loop implementation of
separateExistingAndNewItemswith an O(n + m) hash-map-based approach.Problem
The original implementation used two nested
foreachloops to match input source items against stored database rows. The inner loop has no early exit (break), so it iteratesthrough 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 (*)