Skip to content

Commit 08826cf

Browse files
committed
Various minor performance improvements.
1 parent 0468624 commit 08826cf

3 files changed

Lines changed: 89 additions & 87 deletions

File tree

Changelog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Versioning guidelines for SemVer can be found at: https://semver.org/
1111

1212
- [2025.11.21; Maikuolan]: Added PHP 8.5 to workflows.
1313

14+
- [2026.03.17; Maikuolan]: Various minor performance improvements.
15+
1416
=== Version/Release 1.3.5 ===
1517
PATCH RELEASE.
1618

src/Aggregator.php

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Aggregator v1.3.5 (last modified: 2024.10.16).
3+
* Aggregator v1.3.5 (last modified: 2026.03.17).
44
* @link https://github.com/CIDRAM/Aggregator
55
*
66
* Description: A stand-alone class implementation of the IPv4+IPv6 IP+CIDR
@@ -114,11 +114,11 @@ public function __construct($Mode = 0)
114114
public function aggregate($In)
115115
{
116116
/** Guard. */
117-
if (!is_string($In) && !is_array($In)) {
117+
if (!\is_string($In) && !\is_array($In)) {
118118
return ($this->Mode === 2 || $this->Mode === 3) ? [] : '';
119119
}
120120

121-
$Begin = microtime(true);
121+
$Begin = \microtime(true);
122122
$this->Output = $In;
123123
$this->stripInvalidCharactersAndSort();
124124
$this->stripInvalidRangesAndSubs();
@@ -127,9 +127,9 @@ public function aggregate($In)
127127
$this->convertToNetmasks();
128128
}
129129
if ($this->Mode === 2 || $this->Mode === 3) {
130-
$this->Output = explode("\n", $this->Output);
130+
$this->Output = \explode("\n", $this->Output);
131131
}
132-
$this->ProcessingTime += microtime(true) - $Begin;
132+
$this->ProcessingTime += \microtime(true) - $Begin;
133133
return $this->Output;
134134
}
135135

@@ -157,8 +157,8 @@ private function constructTables()
157157
{
158158
$CIDR = 32;
159159
for ($Octet = 4; $Octet > 0; $Octet--) {
160-
$Base = str_repeat('255.', $Octet - 1);
161-
$End = str_repeat('.0', 4 - $Octet);
160+
$Base = \str_repeat('255.', $Octet - 1);
161+
$End = \str_repeat('.0', 4 - $Octet);
162162
for ($Addresses = 1, $Iterate = 0; $Iterate < 8; $Iterate++, $Addresses *= 2, $CIDR--) {
163163
$Netmask = $Base . (256 - $Addresses) . $End;
164164
$this->TableNetmaskIPv4[$CIDR] = $Netmask;
@@ -167,10 +167,10 @@ private function constructTables()
167167
}
168168
$CIDR = 128;
169169
for ($Octet = 8; $Octet > 0; $Octet--) {
170-
$Base = str_repeat('ffff:', $Octet - 1);
170+
$Base = \str_repeat('ffff:', $Octet - 1);
171171
$End = ($Octet === 8) ? '' : '::';
172172
for ($Addresses = 1, $Iterate = 0; $Iterate < 16; $Iterate++, $Addresses *= 2, $CIDR--) {
173-
$Netmask = $Base . (dechex(65536 - $Addresses)) . $End;
173+
$Netmask = $Base . (\dechex(65536 - $Addresses)) . $End;
174174
$this->TableNetmaskIPv6[$CIDR] = $Netmask;
175175
$this->TableIPv6Netmask[$Netmask] = $CIDR;
176176
}
@@ -184,28 +184,28 @@ private function constructTables()
184184
*/
185185
private function stripInvalidCharactersAndSort()
186186
{
187-
if (!is_array($this->Output)) {
188-
$this->Output = explode("\n", strtolower(trim(str_replace("\r", '', $this->Output))));
187+
if (!\is_array($this->Output)) {
188+
$this->Output = \explode("\n", \strtolower(\trim(\str_replace("\r", '', $this->Output))));
189189
}
190190
$Count = count($this->Output);
191-
if (isset($this->callbacks['newParse']) && is_callable($this->callbacks['newParse'])) {
191+
if (isset($this->callbacks['newParse']) && \is_callable($this->callbacks['newParse'])) {
192192
$this->callbacks['newParse']($Count);
193193
}
194194
if (!empty($this->Results)) {
195195
$this->NumberEntered += $Count;
196196
}
197197
unset($Count);
198-
$this->Output = array_filter(array_unique(array_map(function ($Line) {
199-
$Line = preg_replace('~^(?:(?:#| \*|/\*).*|[^\dA-Fa-f:./]*)|(?:[ \t].*|[^\dA-Fa-f:./]*)$~', '', $Line);
200-
if (isset($this->callbacks['newTick']) && is_callable($this->callbacks['newTick'])) {
198+
$this->Output = \array_filter(\array_unique(\array_map(function ($Line) {
199+
$Line = \preg_replace('~^(?:(?:#| \*|/\*).*|[^\dA-Fa-f:./]*)|(?:[ \t].*|[^\dA-Fa-f:./]*)$~', '', $Line);
200+
if (isset($this->callbacks['newTick']) && \is_callable($this->callbacks['newTick'])) {
201201
$this->callbacks['newTick']();
202202
}
203-
return ($Line === '' || preg_match('~[^\da-f:./]+~i', $Line)) ? '' : $Line;
203+
return ($Line === '' || \preg_match('~[^\da-f:./]+~i', $Line)) ? '' : $Line;
204204
}, $this->Output)));
205-
usort($this->Output, function ($A, $B) {
206-
if (($Pos = strpos($A, '/')) !== false) {
207-
$ASize = substr($A, $Pos + 1);
208-
$A = substr($A, 0, $Pos);
205+
\usort($this->Output, function ($A, $B) {
206+
if (($Pos = \strpos($A, '/')) !== false) {
207+
$ASize = \substr($A, $Pos + 1);
208+
$A = \substr($A, 0, $Pos);
209209
} else {
210210
$ASize = 0;
211211
}
@@ -215,7 +215,7 @@ private function stripInvalidCharactersAndSort()
215215
} elseif ($this->expandIpv6($A, true)) {
216216
$AType = 6;
217217
}
218-
$A = $AType ? inet_pton($A) : false;
218+
$A = $AType ? \inet_pton($A) : false;
219219
if ($AType === 4 && isset($this->TableIPv4Netmask[$ASize])) {
220220
$ASize = $this->TableIPv4Netmask[$ASize];
221221
} elseif ($AType === 6 && isset($this->TableIPv6Netmask[$ASize])) {
@@ -226,9 +226,9 @@ private function stripInvalidCharactersAndSort()
226226
if ($ASize === 0) {
227227
$ASize = ($AType === 4) ? 32 : 128;
228228
}
229-
if (($Pos = strpos($B, '/')) !== false) {
230-
$BSize = substr($B, $Pos + 1);
231-
$B = substr($B, 0, $Pos);
229+
if (($Pos = \strpos($B, '/')) !== false) {
230+
$BSize = \substr($B, $Pos + 1);
231+
$B = \substr($B, 0, $Pos);
232232
} else {
233233
$BSize = 0;
234234
}
@@ -238,7 +238,7 @@ private function stripInvalidCharactersAndSort()
238238
} elseif ($this->expandIpv6($B, true)) {
239239
$BType = 6;
240240
}
241-
$B = $BType ? inet_pton($B) : false;
241+
$B = $BType ? \inet_pton($B) : false;
242242
if ($BType === 4 && isset($this->TableIPv4Netmask[$BSize])) {
243243
$BSize = $this->TableIPv4Netmask[$BSize];
244244
} elseif ($BType === 6 && isset($this->TableIPv6Netmask[$BSize])) {
@@ -267,7 +267,7 @@ private function stripInvalidCharactersAndSort()
267267
}
268268
return $Compare < 0 ? -1 : 1;
269269
});
270-
$this->Output = implode("\n", $this->Output);
270+
$this->Output = \implode("\n", $this->Output);
271271
}
272272

273273
/**
@@ -277,8 +277,8 @@ private function stripInvalidCharactersAndSort()
277277
*/
278278
private function stripInvalidRangesAndSubs()
279279
{
280-
if (isset($this->callbacks['newParse']) && is_callable($this->callbacks['newParse'])) {
281-
$this->callbacks['newParse'](substr_count($this->Output, "\n"));
280+
if (isset($this->callbacks['newParse']) && \is_callable($this->callbacks['newParse'])) {
281+
$this->callbacks['newParse'](\substr_count($this->Output, "\n"));
282282
}
283283
$this->Output = $Out = "\n" . $this->Output . "\n";
284284
$Offset = 0;
@@ -297,35 +297,35 @@ private function stripInvalidRangesAndSubs()
297297
] as $Lows) {
298298
for ($Iterant = 1; $Iterant < $Lows[2]; $Iterant++) {
299299
$Low[$Lows[0]] = $Iterant;
300-
if (preg_match('~\n' . $Lows[1] . '/' . $Iterant . '(?:$|\D)~i', $this->Output)) {
300+
if (\preg_match('~\n' . $Lows[1] . '/' . $Iterant . '(?:$|\D)~i', $this->Output)) {
301301
break;
302302
}
303303
}
304304
}
305305
unset($Lows);
306-
while (($NewLine = strpos($this->Output, "\n", $Offset)) !== false) {
307-
if (isset($this->callbacks['newTick']) && is_callable($this->callbacks['newTick'])) {
306+
while (($NewLine = \strpos($this->Output, "\n", $Offset)) !== false) {
307+
if (isset($this->callbacks['newTick']) && \is_callable($this->callbacks['newTick'])) {
308308
$this->callbacks['newTick']();
309309
}
310-
$Line = substr($this->Output, $Offset, $NewLine - $Offset);
310+
$Line = \substr($this->Output, $Offset, $NewLine - $Offset);
311311
$Offset = $NewLine + 1;
312312
if (!$Line) {
313313
continue;
314314
}
315-
if (($RangeSep = strpos($Line, '/')) !== false) {
316-
$Size = substr($Line, $RangeSep + 1);
317-
if (strpos($Size, '.') !== false) {
315+
if (($RangeSep = \strpos($Line, '/')) !== false) {
316+
$Size = \substr($Line, $RangeSep + 1);
317+
if (\strpos($Size, '.') !== false) {
318318
$Size = isset($this->TableIPv4Netmask[$Size]) ? $this->TableIPv4Netmask[$Size] : 0;
319-
} elseif (strpos($Size, ':') !== false) {
319+
} elseif (\strpos($Size, ':') !== false) {
320320
$Size = isset($this->TableIPv6Netmask[$Size]) ? $this->TableIPv6Netmask[$Size] : 0;
321321
} else {
322322
$Size = (int)$Size;
323323
}
324-
$CIDR = substr($Line, 0, $RangeSep);
324+
$CIDR = \substr($Line, 0, $RangeSep);
325325
} else {
326-
if (strpos($Line, '.') !== false) {
326+
if (\strpos($Line, '.') !== false) {
327327
$Size = 32;
328-
} elseif (strpos($Line, ':') !== false) {
328+
} elseif (\strpos($Line, ':') !== false) {
329329
$Size = 128;
330330
} else {
331331
$Size = 0;
@@ -337,28 +337,28 @@ private function stripInvalidRangesAndSubs()
337337
} elseif (($Size > 0 && $Size <= 128) && ($CIDRs = $this->expandIpv6($CIDR, false, $Size - 1))) {
338338
$Type = 6;
339339
} else {
340-
$Out = str_replace("\n" . $Line . "\n", "\n", $Out);
340+
$Out = \str_replace("\n" . $Line . "\n", "\n", $Out);
341341
continue;
342342
}
343343
if (!isset($CIDRs[$Size - 1]) || $CIDRs[$Size - 1] !== $CIDR . '/' . $Size) {
344-
$Out = str_replace("\n" . $Line . "\n", "\n", $Out);
344+
$Out = \str_replace("\n" . $Line . "\n", "\n", $Out);
345345
continue;
346346
}
347-
$Out = str_replace("\n" . $Line . "\n", "\n" . $CIDRs[$Size - 1] . "\n", $Out);
347+
$Out = \str_replace("\n" . $Line . "\n", "\n" . $CIDRs[$Size - 1] . "\n", $Out);
348348
$ThisLow = ($Type === 4 ? $Low[4] : $Low[6]) - 1;
349349
for ($Range = $Size - 2; $Range >= $ThisLow; $Range--) {
350-
if (isset($CIDRs[$Range]) && strpos($Out, "\n" . $CIDRs[$Range] . "\n") !== false) {
351-
$Out = str_replace("\n" . $CIDRs[$Size - 1] . "\n", "\n", $Out);
350+
if (isset($CIDRs[$Range]) && \strpos($Out, "\n" . $CIDRs[$Range] . "\n") !== false) {
351+
$Out = \str_replace("\n" . $CIDRs[$Size - 1] . "\n", "\n", $Out);
352352
if (!empty($this->Results)) {
353353
$this->NumberMerged++;
354354
}
355355
break;
356356
}
357357
}
358358
}
359-
$this->Output = trim($Out);
359+
$this->Output = \trim($Out);
360360
if (!empty($this->Results)) {
361-
$this->NumberReturned += empty($this->Output) ? 0 : substr_count($this->Output, "\n") + 1;
361+
$this->NumberReturned += empty($this->Output) ? 0 : \substr_count($this->Output, "\n") + 1;
362362
$this->NumberRejected = $this->NumberEntered - $this->NumberReturned - $this->NumberMerged;
363363
$this->NumberAccepted = $this->NumberEntered - $this->NumberRejected;
364364
}
@@ -373,38 +373,38 @@ private function mergeRanges()
373373
{
374374
while (true) {
375375
$Step = $this->Output;
376-
if (isset($this->callbacks['newParse']) && is_callable($this->callbacks['newParse'])) {
377-
$this->callbacks['newParse'](substr_count($Step, "\n"));
376+
if (isset($this->callbacks['newParse']) && \is_callable($this->callbacks['newParse'])) {
377+
$this->callbacks['newParse'](\substr_count($Step, "\n"));
378378
}
379379
$this->Output = $Out = "\n" . $this->Output . "\n";
380380
$Size = $Offset = 0;
381381
$Line = '';
382382
$CIDRs = false;
383-
while (($NewLine = strpos($this->Output, "\n", $Offset)) !== false) {
384-
if (isset($this->callbacks['newTick']) && is_callable($this->callbacks['newTick'])) {
383+
while (($NewLine = \strpos($this->Output, "\n", $Offset)) !== false) {
384+
if (isset($this->callbacks['newTick']) && \is_callable($this->callbacks['newTick'])) {
385385
$this->callbacks['newTick']();
386386
}
387387
$PrevLine = $Line;
388388
$PrevSize = $Size;
389389
$PrevCIDRs = $CIDRs;
390-
$Line = substr($this->Output, $Offset, $NewLine - $Offset);
390+
$Line = \substr($this->Output, $Offset, $NewLine - $Offset);
391391
$Offset = $NewLine + 1;
392-
$RangeSep = strpos($Line, '/');
393-
$Size = (int)substr($Line, $RangeSep + 1);
394-
$CIDR = substr($Line, 0, $RangeSep);
392+
$RangeSep = \strpos($Line, '/');
393+
$Size = (int)\substr($Line, $RangeSep + 1);
394+
$CIDR = \substr($Line, 0, $RangeSep);
395395
if (!$CIDRs = $this->expandIpv4($CIDR, false, $Size - 1)) {
396396
$CIDRs = $this->expandIpv6($CIDR, false, $Size - 1);
397397
}
398398
if ($Line === $PrevLine) {
399-
$Out = str_replace("\n" . $PrevLine . "\n" . $Line . "\n", "\n" . $Line . "\n", $Out);
399+
$Out = \str_replace("\n" . $PrevLine . "\n" . $Line . "\n", "\n" . $Line . "\n", $Out);
400400
} elseif (
401401
!empty($CIDRs[$Size - 1]) &&
402402
!empty($PrevCIDRs[$PrevSize - 1]) &&
403403
!empty($CIDRs[$Size - 2]) &&
404404
!empty($PrevCIDRs[$PrevSize - 2]) &&
405405
$CIDRs[$Size - 2] === $PrevCIDRs[$PrevSize - 2]
406406
) {
407-
$Out = str_replace("\n" . $PrevLine . "\n" . $Line . "\n", "\n" . $CIDRs[$Size - 2] . "\n", $Out);
407+
$Out = \str_replace("\n" . $PrevLine . "\n" . $Line . "\n", "\n" . $CIDRs[$Size - 2] . "\n", $Out);
408408
$Line = $CIDRs[$Size - 2];
409409
$Size--;
410410
if (!empty($this->Results)) {
@@ -413,7 +413,7 @@ private function mergeRanges()
413413
}
414414
}
415415
}
416-
$this->Output = trim($Out);
416+
$this->Output = \trim($Out);
417417
if ($Step === $this->Output) {
418418
break;
419419
}
@@ -427,30 +427,30 @@ private function mergeRanges()
427427
*/
428428
private function convertToNetmasks()
429429
{
430-
if (isset($this->callbacks['newParse']) && is_callable($this->callbacks['newParse'])) {
431-
$this->callbacks['newParse'](substr_count($this->Output, "\n"));
430+
if (isset($this->callbacks['newParse']) && \is_callable($this->callbacks['newParse'])) {
431+
$this->callbacks['newParse'](\substr_count($this->Output, "\n"));
432432
}
433433
$this->Output = $Out = "\n" . $this->Output . "\n";
434434
$Offset = 0;
435-
while (($NewLine = strpos($this->Output, "\n", $Offset)) !== false) {
436-
if (isset($this->callbacks['newTick']) && is_callable($this->callbacks['newTick'])) {
435+
while (($NewLine = \strpos($this->Output, "\n", $Offset)) !== false) {
436+
if (isset($this->callbacks['newTick']) && \is_callable($this->callbacks['newTick'])) {
437437
$this->callbacks['newTick']();
438438
}
439-
$Line = substr($this->Output, $Offset, $NewLine - $Offset);
439+
$Line = \substr($this->Output, $Offset, $NewLine - $Offset);
440440
$Offset = $NewLine + 1;
441-
if (!$Line || ($RangeSep = strpos($Line, '/')) === false) {
441+
if (!$Line || ($RangeSep = \strpos($Line, '/')) === false) {
442442
continue;
443443
}
444-
$Size = (int)substr($Line, $RangeSep + 1);
445-
$CIDR = substr($Line, 0, $RangeSep);
444+
$Size = (int)\substr($Line, $RangeSep + 1);
445+
$CIDR = \substr($Line, 0, $RangeSep);
446446
$Type = ($this->expandIpv4($CIDR, true)) ? 4 : 6;
447447
if ($Type === 4 && isset($this->TableNetmaskIPv4[$Size])) {
448448
$Size = $this->TableNetmaskIPv4[$Size];
449449
} elseif ($Type === 6 && isset($this->TableNetmaskIPv6[$Size])) {
450450
$Size = $this->TableNetmaskIPv6[$Size];
451451
}
452-
$Out = str_replace("\n" . $Line . "\n", "\n" . $CIDR . '/' . $Size . "\n", $Out);
452+
$Out = \str_replace("\n" . $Line . "\n", "\n" . $CIDR . '/' . $Size . "\n", $Out);
453453
}
454-
$this->Output = trim($Out);
454+
$this->Output = \trim($Out);
455455
}
456456
}

0 commit comments

Comments
 (0)