Fix nesting iterations over the same Accumulate instance#37
Merged
Conversation
* develop: bump minimum version of static analysis fix psalm errors
* develop: specify next release CS fix loading an extra element when not necessary
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #37 +/- ##
=============================================
+ Coverage 97.92% 98.40% +0.48%
- Complexity 1050 1084 +34
=============================================
Files 72 76 +4
Lines 4235 4768 +533
=============================================
+ Hits 4147 4692 +545
+ Misses 88 76 -12 ☔ View full report in Codecov by Sentry. |
…exist but the source generator is share elsewhere
…y changing the underlying value being fetched
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.
Problem
In a closed source project a bug was found with deferred
Sets. The scenario is :SetAAis composed withmaps andfilters producing aSetBBis partially loaded via a->find()->match()Bis composed withremove,addandmaps producing aSetCCis compared againstAviaequalsThis results in duplicated values inside
C. It seems to be linked to the fact when iterating overCit moves the cursor onA, butCdepends onA's cursor to correctly iterate its values.When trying to reproduce this bug outside the project with a dumbed down example it fails to reproduce the bug. Since the
Sets are produced via multiple abstractions it's hard to track down the missing critical step to reproduce the bug.However it showed that the cursor inside
Accumulateis not correctly handled when multiple loops moves the cursor on the same instance.When working on the problem the smallest bug can be expressed via :
As one would expect it produces :
[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3].When the inner loop exits PHP knows where the cursor on
$ashould be for the next iteration on the outer loop.However if
$ais wrapped vianew ArrayIterator([1, 2, 3])it produces an infinite loop because PHP doesn't seek the cursor on$awhen exiting the inner loop.The whole problem boils down to this, seeking the correct cursor position when exiting a loop.
Solution
Map\DoubleIndexnow usesSequenceinstead ofSequence\Implementationto make sure it doesn't rely on the underlying iterator, as it may result in the iterator cursor not being correctly positioned after a method call.Set\Implementation::iterator()has been removed to make sure it doesn't rely on the underlying iterator of sequences.->rewind()before using iterators (such as inAggregate). This wasn't problematic because the iterators were in the correct state. This is mainly to be coherent throughout the implementation.Sequence\namespace are now explicitly called via their methods instead of implicit calls via theforeachstatement. This is to make it explicit what happens and introduce the cleanup below.Sequence\Implementation::iterator()now returns a newIteratorclass that has acleanupmethod to correctly leave the iterator in a good state when the loop it's in is partially iterated over.array) the cleanup does nothing (same state as if it was completely iterated over)rewindcall)Set/Sequence, in this case nothing happensSequencedoesn't hold all intermediary values if it's not used elsewhere to avoid keeping too many values in memory. The optimisation didn't take into account that the source monads may no longer exist in memory by the time one, or multiple, ones that rely on it are consumed.Note
To simplify the implementation the iterators are decorated by 2 objects each time (
IteratorandIterator\Lazy|Defer|Primitive). This complexifies stack traces and has a minor impact on memory.This should be negligible but is worth mentioning.