From 4dea42a3f8f40ce4480a598d1672e2b2d71dc84a Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sat, 2 Sep 2023 16:29:21 +0200 Subject: [PATCH] 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. --- ext/dom/xpath.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ext/dom/xpath.c b/ext/dom/xpath.c index 62e11f6b99b..5c6e5d3307e 100644 --- a/ext/dom/xpath.c +++ b/ext/dom/xpath.c @@ -95,7 +95,8 @@ static void dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, } else if (type == 2) { int j; if (obj->nodesetval && obj->nodesetval->nodeNr > 0) { - array_init(&fci.params[i]); + array_init_size(&fci.params[i], obj->nodesetval->nodeNr); + zend_hash_real_init_packed(Z_ARRVAL_P(&fci.params[i])); for (j = 0; j < obj->nodesetval->nodeNr; j++) { xmlNodePtr node = obj->nodesetval->nodeTab[j]; zval child; @@ -408,8 +409,8 @@ static void php_xpath_eval(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */ xmlNodeSetPtr nodesetp; if (xpathobjp->type == XPATH_NODESET && NULL != (nodesetp = xpathobjp->nodesetval) && nodesetp->nodeNr) { - - array_init(&retval); + array_init_size(&retval, nodesetp->nodeNr); + zend_hash_real_init_packed(Z_ARRVAL_P(&retval)); for (i = 0; i < nodesetp->nodeNr; i++) { xmlNodePtr node = nodesetp->nodeTab[i]; zval child;