Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
52df7a11
Unverified
Commit
52df7a11
authored
Nov 11, 2019
by
Andreas Braun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-501: Use IteratorIterator to prevent memory leak with generators
parent
32cfb9bb
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
19 deletions
+9
-19
CachingIterator.php
src/Model/CachingIterator.php
+9
-19
No files found.
src/Model/CachingIterator.php
View file @
52df7a11
...
@@ -18,8 +18,8 @@
...
@@ -18,8 +18,8 @@
namespace
MongoDB\Model
;
namespace
MongoDB\Model
;
use
Countable
;
use
Countable
;
use
Generator
;
use
Iterator
;
use
Iterator
;
use
IteratorIterator
;
use
Traversable
;
use
Traversable
;
use
function
count
;
use
function
count
;
use
function
current
;
use
function
current
;
...
@@ -41,7 +41,7 @@ class CachingIterator implements Countable, Iterator
...
@@ -41,7 +41,7 @@ class CachingIterator implements Countable, Iterator
/** @var array */
/** @var array */
private
$items
=
[];
private
$items
=
[];
/** @var
Gen
erator */
/** @var
IteratorIt
erator */
private
$iterator
;
private
$iterator
;
/** @var boolean */
/** @var boolean */
...
@@ -61,7 +61,9 @@ class CachingIterator implements Countable, Iterator
...
@@ -61,7 +61,9 @@ class CachingIterator implements Countable, Iterator
*/
*/
public
function
__construct
(
Traversable
$traversable
)
public
function
__construct
(
Traversable
$traversable
)
{
{
$this
->
iterator
=
$this
->
wrapTraversable
(
$traversable
);
$this
->
iterator
=
new
IteratorIterator
(
$traversable
);
$this
->
iterator
->
rewind
();
$this
->
storeCurrentItem
();
$this
->
storeCurrentItem
();
}
}
...
@@ -101,8 +103,12 @@ class CachingIterator implements Countable, Iterator
...
@@ -101,8 +103,12 @@ class CachingIterator implements Countable, Iterator
public
function
next
()
public
function
next
()
{
{
if
(
!
$this
->
iteratorExhausted
)
{
if
(
!
$this
->
iteratorExhausted
)
{
$this
->
iteratorAdvanced
=
true
;
$this
->
iterator
->
next
();
$this
->
iterator
->
next
();
$this
->
storeCurrentItem
();
$this
->
storeCurrentItem
();
$this
->
iteratorExhausted
=
!
$this
->
iterator
->
valid
();
}
}
next
(
$this
->
items
);
next
(
$this
->
items
);
...
@@ -156,20 +162,4 @@ class CachingIterator implements Countable, Iterator
...
@@ -156,20 +162,4 @@ class CachingIterator implements Countable, Iterator
$this
->
items
[
$key
]
=
$this
->
iterator
->
current
();
$this
->
items
[
$key
]
=
$this
->
iterator
->
current
();
}
}
/**
* Wraps the Traversable with a Generator.
*
* @param Traversable $traversable
* @return Generator
*/
private
function
wrapTraversable
(
Traversable
$traversable
)
{
foreach
(
$traversable
as
$key
=>
$value
)
{
yield
$key
=>
$value
;
$this
->
iteratorAdvanced
=
true
;
}
$this
->
iteratorExhausted
=
true
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment