-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix nesting iterations over the same Accumulate instance #37
Merged
Conversation
This file contains 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
* 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
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
Set
s. The scenario is :Set
A
A
is composed withmap
s andfilter
s producing aSet
B
B
is partially loaded via a->find()->match()
B
is composed withremove
,add
andmap
s producing aSet
C
C
is compared againstA
viaequals
This results in duplicated values inside
C
. It seems to be linked to the fact when iterating overC
it moves the cursor onA
, butC
depends 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
Set
s 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
Accumulate
is 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
$a
should be for the next iteration on the outer loop.However if
$a
is wrapped vianew ArrayIterator([1, 2, 3])
it produces an infinite loop because PHP doesn't seek the cursor on$a
when exiting the inner loop.The whole problem boils down to this, seeking the correct cursor position when exiting a loop.
Solution
Map\DoubleIndex
now usesSequence
instead ofSequence\Implementation
to 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 theforeach
statement. This is to make it explicit what happens and introduce the cleanup below.Sequence\Implementation::iterator()
now returns a newIterator
class that has acleanup
method 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)rewind
call)Set
/Sequence
, in this case nothing happensSequence
doesn'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 (
Iterator
andIterator\Lazy|Defer|Primitive
). This complexifies stack traces and has a minor impact on memory.This should be negligible but is worth mentioning.