From 8417b136b25da2ad24c4f9fce5362f18242ded85 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 29 Nov 2026 15:36:58 +0000 Subject: [PATCH] Flatten in linear time --- CHANGELOG.md | 1 + src/TreeCompiler.php | 33 ++++++++++++++++++++++----------- src/TreeInterpreter.php | 29 +++++++++++++++++++++-------- tests/CompilerRuntimeTest.php | 2 ++ 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c51eacd..d02a227 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Fixed empty array-like objects to be falsy. * Fixed length() and reverse() to read objects with __toString() as strings. * Changed array-like objects that are not Traversable to error when their elements are listed. +* Improved the performance of flatten on large arrays. ## 2.10.0 - 2026-11-17 diff --git a/src/TreeCompiler.php b/src/TreeCompiler.php index f34e76d..5f0d3f6 100644 --- a/src/TreeCompiler.php +++ b/src/TreeCompiler.php @@ -335,7 +335,8 @@ private function visit_expref(array $node) private function visit_flatten(array $node) { $this->dispatch($node['children'][0]); - $merged = $this->makeVar('merged'); + $lists = $this->makeVar('lists'); + $run = $this->makeVar('run'); $val = $this->makeVar('val'); $this @@ -347,33 +348,43 @@ private function visit_flatten(array $node) ->write('} else {') ->indent() ->write('if (!is_array($value)) { $value = Utils::toArray($value); }') - ->write('%s = [];', $merged) + ->write('%s = [];', $lists) + ->write('%s = [];', $run) ->write('foreach ($value as %s) {', $val) ->indent() ->write('if (is_array(%s)) {', $val) ->indent() - ->write('if (array_key_exists(0, %s)) {', $val) + ->write('if (!array_key_exists(0, %s)) {', $val) ->indent() - ->write('%s = array_merge(%s, %s);', $merged, $merged, $val) - ->outdent() - ->write('} elseif (%s !== []) {', $val) - ->indent() - ->write('%s[] = %s;', $merged, $val) + ->write('if (%s !== []) { %s[] = %s; }', $val, $run, $val) + ->write('continue;') ->outdent() ->write('}') ->outdent() ->write('} elseif (%s instanceof \\ArrayAccess && Utils::isArray(%s)) {', $val, $val) ->indent() - ->write('%s = array_merge(%s, Utils::toArray(%s));', $merged, $merged, $val) + ->write('%s = Utils::toArray(%s);', $val, $val) ->outdent() ->write('} else {') ->indent() - ->write('%s[] = %s;', $merged, $val) + ->write('%s[] = %s;', $run, $val) + ->write('continue;') ->outdent() ->write('}') + ->write('if (%s !== []) { %s[] = %s; %s = []; }', $run, $lists, $run, $run) + ->write('%s[] = %s;', $lists, $val) + ->outdent() + ->write('}') + ->write('if (%s === []) {', $lists) + ->indent() + ->write('$value = %s;', $run) + ->outdent() + ->write('} else {') + ->indent() + ->write('%s[] = %s;', $lists, $run) + ->write('$value = array_merge(...%s);', $lists) ->outdent() ->write('}') - ->write('$value = %s;', $merged) ->outdent() ->write('}'); diff --git a/src/TreeInterpreter.php b/src/TreeInterpreter.php index afd5155..62921e8 100644 --- a/src/TreeInterpreter.php +++ b/src/TreeInterpreter.php @@ -132,23 +132,36 @@ private function dispatch(array $node, $value) $value = Utils::toArray($value); } - $merged = []; + $lists = []; + $run = []; foreach ($value as $values) { // Only merge up arrays lists and not hashes if (is_array($values)) { - if (array_key_exists(0, $values)) { - $merged = array_merge($merged, $values); - } elseif ($values !== $skipElement) { - $merged[] = $values; + if (!array_key_exists(0, $values)) { + if ($values !== $skipElement) { + $run[] = $values; + } + continue; } } elseif ($values instanceof \ArrayAccess && Utils::isArray($values)) { - $merged = array_merge($merged, Utils::toArray($values)); + $values = Utils::toArray($values); } else { - $merged[] = $values; + $run[] = $values; + continue; } + + if ($run !== []) { + $lists[] = $run; + $run = []; + } + $lists[] = $values; + } + if ($lists === []) { + return $run; } + $lists[] = $run; - return $merged; + return array_merge(...$lists); case 'literal': return $node['value']; diff --git a/tests/CompilerRuntimeTest.php b/tests/CompilerRuntimeTest.php index df4d506..1fde25a 100644 --- a/tests/CompilerRuntimeTest.php +++ b/tests/CompilerRuntimeTest.php @@ -361,12 +361,14 @@ public function testRuntimesFlattenArrayLikeObjects(): void $data = [ 'lists' => new _TestArrayLike([new _TestArrayLike([1, 2]), [3], new _TestArrayLike(), 4]), 'hashes' => new _TestArrayLike([$hash]), + 'mixed' => [1, $hash, [], [2, 3], 4, 5, new _TestArrayLike([6]), 7], ]; try { foreach ([new AstRuntime(), new CompilerRuntime($dir)] as $runtime) { $this->assertSame([1, 2, 3, 4], $runtime('lists[]', $data)); $this->assertSame([$hash], $runtime('hashes[]', $data)); + $this->assertSame([1, $hash, 2, 3, 4, 5, 6, 7], $runtime('mixed[]', $data)); } } finally { $this->removeTempDir($dir);