Skip to content

Commit b385e0d

Browse files
authored
ext/posix: preallocate arrays and/or change to packed ones. (php#18370)
1 parent 68d5403 commit b385e0d

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

ext/posix/posix.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,11 @@ PHP_FUNCTION(posix_getgroups)
236236
RETURN_FALSE;
237237
}
238238

239-
array_init(return_value);
239+
array_init_size(return_value, result);
240+
zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
240241

241242
for (i=0; i<result; i++) {
242-
add_next_index_long(return_value, gidlist[i]);
243+
add_index_long(return_value, i, gidlist[i]);
243244
}
244245
efree(gidlist);
245246
}
@@ -380,7 +381,7 @@ PHP_FUNCTION(posix_times)
380381
RETURN_FALSE;
381382
}
382383

383-
array_init(return_value);
384+
array_init_size(return_value, 5);
384385

385386
add_assoc_long(return_value, "ticks", ticks); /* clock ticks */
386387
add_assoc_long(return_value, "utime", t.tms_utime); /* user time */
@@ -670,6 +671,7 @@ int php_posix_group_to_array(struct group *g, zval *array_group) /* {{{ */
670671
return 0;
671672

672673
array_init(&array_members);
674+
zend_hash_real_init_packed(Z_ARRVAL(array_members));
673675

674676
add_assoc_string(array_group, "name", g->gr_name);
675677
if (g->gr_passwd) {
@@ -1174,7 +1176,8 @@ PHP_FUNCTION(posix_getrlimit)
11741176
RETURN_FALSE;
11751177
}
11761178

1177-
array_init(return_value);
1179+
array_init_size(return_value, 2);
1180+
zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
11781181
if (rl.rlim_cur == RLIM_INFINITY) {
11791182
add_next_index_stringl(return_value, UNLIMITED_STRING, sizeof(UNLIMITED_STRING)-1);
11801183
} else {

0 commit comments

Comments
 (0)