From 579d4217b71aeec3152064584cad474cf068ac58 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sat, 2 Sep 2023 01:20:17 +0200 Subject: [PATCH] Preallocate result array size in xpath 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 62e11f6b99bfb..5c6e5d3307e6e 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;