-
Notifications
You must be signed in to change notification settings - Fork 265
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
PHPLIB-501: Prevent memory leaks from generators #694
Conversation
e1fc8be
to
6c40fd7
Compare
@@ -61,8 +64,7 @@ class CachingIterator implements Countable, Iterator | |||
*/ | |||
public function __construct(Traversable $traversable) | |||
{ | |||
$this->iterator = $this->wrapTraversable($traversable); | |||
$this->storeCurrentItem(); | |||
$this->iterator = new IteratorIterator($traversable); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to just prepare the iterator here and store the current item? That might allow you to remove the prepareIterator()
calls from other methods.
As-is, there might be a subtle behavioral change, since the original implementation did rewind the traversable, create the generator, and iterate once to store the current item?
Renamed the issue to reflect the actual change and title in JIRA. Also, re: your comment in doctrine/mongodb-odm#2100 (comment), can you elaborate on the subtle behavioral change by switching to IteratorIterator? Does it relate to my earlier comment about removing the |
That is correct. When using the generator, we immediately start iterating the wrapped traversable. When switching to IteratorIterator, we can still do that (by calling |
a75cc5a
to
0015e6d
Compare
0015e6d
to
964954e
Compare
PR adapted to replicate previous behaviour: iteration of the traversable starts when |
https://jira.mongodb.org/browse/PHPLIB-501
This was discovered while investigating doctrine/mongodb-odm#2092. The different behaviour can be seen in action here: https://3v4l.org/UIfIb. Note that when we don't specifically unset the generator, it stays around much longer even though the wrapping iterator has already been garbage collected.