From 83f544c32ba8670cfdf9d55fa5fcb57b6fb7f1f3 Mon Sep 17 00:00:00 2001 From: Dylan Carruthers Date: Fri, 29 May 2026 10:43:43 +1200 Subject: [PATCH] Replace O(n^2) array_merge accumulators with append array_merge reallocates the accumulator on every call. Replacing the in-loop accumulator pattern with native append makes pagination and transform linear in the row count. - get(): array_merge($results, $page) -> array_push(..., ...$page) - splitRows(): array_merge($output, [$item]) -> $output[] = $item (x2) - transform(): same shape for $fnew, $mnew, and one helper output Inner per-row array_merge calls on small lists (tags, munge keys) are unchanged. --- library/Netbox/Netbox.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/library/Netbox/Netbox.php b/library/Netbox/Netbox.php index 40b3e3c..532c331 100644 --- a/library/Netbox/Netbox.php +++ b/library/Netbox/Netbox.php @@ -117,7 +117,7 @@ private function get(string $resource) if (empty($response->results)) { throw new \Exception("no results field in response"); } - $results = array_merge($results, $response->results); + array_push($results, ...$response->results); } return $results; } @@ -362,12 +362,12 @@ private function makeHelperKeys(array $in) { } } - $output = array_merge($output, [(object)$row]); + $output[] = (object)$row; } return $output; } - // This is a function that you can use to add complex rules to define + // This is a function that you can use to add complex rules to define // host zones based on netbox data, eg: ip range mapping to zone. // While possible using Import modifiers and Sync rule property filters // forking this repo and writing your own function reduces the number require in complex setups. @@ -390,10 +390,10 @@ private function splitRows(array $in, $key) { foreach ($row->{$key} as $ip) { $row->primary_ip_address = $ip; $row->description = $description . " " . $ip; - $output = array_merge($output, [(object)clone($row)]); + $output[] = (object)clone($row); } } else { - $output = array_merge($output, [(object)$row]); + $output[] = (object)$row; } } return $output; @@ -465,7 +465,7 @@ private function transform(array $in) } } $this->flattenRecursive($out, '', $in, $this->flattenseparator); - $fnew = array_merge($fnew, [(object)$out]); + $fnew[] = (object)$out; } $output = $fnew; } @@ -484,7 +484,7 @@ private function transform(array $in) } } $row->{$mungeheading} = implode("_", $mungevalue); - $mnew = array_merge($mnew, [(object)$row]); + $mnew[] = (object)$row; } $output = $mnew; }