Skip to content

Commit 4dea42a

Browse files
authored
Preallocate result array size in xpath (#12105)
We know what size they're going to be, and we know they are packed arrays. Prevent reallocations and initialisation overhead by setting the initial size and initializing it as packed from the start.
1 parent 7be4795 commit 4dea42a

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

ext/dom/xpath.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ static void dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs,
9595
} else if (type == 2) {
9696
int j;
9797
if (obj->nodesetval && obj->nodesetval->nodeNr > 0) {
98-
array_init(&fci.params[i]);
98+
array_init_size(&fci.params[i], obj->nodesetval->nodeNr);
99+
zend_hash_real_init_packed(Z_ARRVAL_P(&fci.params[i]));
99100
for (j = 0; j < obj->nodesetval->nodeNr; j++) {
100101
xmlNodePtr node = obj->nodesetval->nodeTab[j];
101102
zval child;
@@ -408,8 +409,8 @@ static void php_xpath_eval(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */
408409
xmlNodeSetPtr nodesetp;
409410

410411
if (xpathobjp->type == XPATH_NODESET && NULL != (nodesetp = xpathobjp->nodesetval) && nodesetp->nodeNr) {
411-
412-
array_init(&retval);
412+
array_init_size(&retval, nodesetp->nodeNr);
413+
zend_hash_real_init_packed(Z_ARRVAL_P(&retval));
413414
for (i = 0; i < nodesetp->nodeNr; i++) {
414415
xmlNodePtr node = nodesetp->nodeTab[i];
415416
zval child;

0 commit comments

Comments
 (0)