Skip to content

Commit ba35960

Browse files
Avoid closure allocation in safeFilter / implodeFiltered
Pass a static array callable to array_filter instead of allocating a fresh closure on every call. Required promoting safeFilterFunc from protected to public so array_filter (external scope) can resolve it. Measured on cloudinary_php URL workload (12-run alternating A/B, 5000 iterations each): (string) new Image() -8.2% (26.67 -> 24.49 us/op) $image->toUrl() -15.5% (23.08 -> 19.50 us/op)
1 parent 564e06d commit ba35960

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

src/Utils/ArrayUtils.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ public static function implodeFiltered(
133133
callable|null $filterCallback = null,
134134
int $flag = 0
135135
): string {
136-
$filterCallback ??= fn($value) => ArrayUtils::safeFilterFunc($value);
137-
138136
return self::safeImplode($glue, self::safeFilter($pieces, $filterCallback, $flag));
139137
}
140138

@@ -188,7 +186,7 @@ public static function escapedImplode(string|array $glue, array|null $pieces): s
188186
* @see strlen
189187
* @see empty
190188
*/
191-
protected static function safeFilterFunc(mixed $value): bool|int
189+
public static function safeFilterFunc(mixed $value): bool|int
192190
{
193191
if ($value === null) {
194192
return 0;
@@ -224,9 +222,11 @@ public static function safeFilter(
224222
callable|null $callback = null,
225223
int $flag = 0
226224
): ?array {
227-
$callback ??= fn($value) => ArrayUtils::safeFilterFunc($value);
225+
if ($input === null) {
226+
return null;
227+
}
228228

229-
return is_null($input) ? $input : array_filter($input, $callback, $flag);
229+
return array_filter($input, $callback ?? [self::class, 'safeFilterFunc'], $flag);
230230
}
231231

232232
/**

0 commit comments

Comments
 (0)