Skip to content

Commit 1faf17b

Browse files
authored
Use packed fill in scandir() (#20737)
By preallocating the array to the right size and using a packed fill, we can avoid reallocations and use the fastest way to fill the array.
1 parent 9fffe41 commit 1faf17b

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

ext/standard/dir.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,11 +558,15 @@ PHP_FUNCTION(scandir)
558558
RETURN_FALSE;
559559
}
560560

561-
array_init(return_value);
561+
array_init_size(return_value, n);
562+
zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
562563

563-
for (i = 0; i < n; i++) {
564-
add_next_index_str(return_value, namelist[i]);
565-
}
564+
ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
565+
for (i = 0; i < n; i++) {
566+
ZEND_HASH_FILL_SET_STR(namelist[i]);
567+
ZEND_HASH_FILL_NEXT();
568+
}
569+
} ZEND_HASH_FILL_END();
566570

567571
if (n) {
568572
efree(namelist);

0 commit comments

Comments
 (0)