mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-08-30 14:36:06 +02:00
FIX: pre-cache loop content within SSViewer.
SSViewer iterates on Iterators that it receives twice: first to get the total number of items, then to actually render each item. This necessitates a rewind. In order to make more use of generators, which are not rewindable, I’d like to remove the need for a rewind. I’ve done this by caching the content of the iterator as an array within SSViewer_Scope. Although this means a bit of memory usage, there are no cases in which code will get to this point without iterating on all items, which would use the memory anyway. It would only create onerous impacts in cases where you are iterating on very long iterators, which would mean you’re rendering a very large page anyway, and probably have other performance issues.
This commit is contained in:
parent
1b8f601023
commit
6ef5785fc5
@ -289,14 +289,20 @@ class SSViewer_Scope
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->itemIterator) {
|
if (!$this->itemIterator) {
|
||||||
|
// Turn the iterator into an array. This lets us get the count and iterate on it, even if it's a generator.
|
||||||
if (is_array($this->item)) {
|
if (is_array($this->item)) {
|
||||||
$this->itemIterator = new ArrayIterator($this->item);
|
$arrayVersion = $this->item;
|
||||||
} else {
|
} else {
|
||||||
$this->itemIterator = $this->item->getIterator();
|
$arrayVersion = [];
|
||||||
|
foreach ($this->item as $record) {
|
||||||
|
$arrayVersion[] = $record;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->itemIterator = new ArrayIterator($arrayVersion);
|
||||||
|
|
||||||
$this->itemStack[$this->localIndex][SSViewer_Scope::ITEM_ITERATOR] = $this->itemIterator;
|
$this->itemStack[$this->localIndex][SSViewer_Scope::ITEM_ITERATOR] = $this->itemIterator;
|
||||||
$this->itemIteratorTotal = iterator_count($this->itemIterator); // Count the total number of items
|
$this->itemIteratorTotal = count($arrayVersion); // Count the total number of items
|
||||||
$this->itemStack[$this->localIndex][SSViewer_Scope::ITEM_ITERATOR_TOTAL] = $this->itemIteratorTotal;
|
$this->itemStack[$this->localIndex][SSViewer_Scope::ITEM_ITERATOR_TOTAL] = $this->itemIteratorTotal;
|
||||||
$this->itemIterator->rewind();
|
$this->itemIterator->rewind();
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user