Skip to content

Commit ed91621

Browse files
authored
Avoid additional allocation in Document\createElementNS (#14071)
For the following benchmark code: ```php $dom = DOM\XMLDocument::createEmpty(); for ($i = 0; $i < 1000*100; $i++) $dom->createElementNS("urn:a", "thisisaveryverylongname"); ``` We obtain the following on an i7-4790: ``` Benchmark 1: ./sapi/cli/php bench.php Time (mean ± σ): 34.5 ms ± 1.2 ms [User: 31.4 ms, System: 2.9 ms] Range (min … max): 32.4 ms … 39.3 ms 84 runs Benchmark 2: ./sapi/cli/php_old bench.php Time (mean ± σ): 36.6 ms ± 1.6 ms [User: 33.6 ms, System: 2.9 ms] Range (min … max): 34.3 ms … 45.3 ms 80 runs Summary ./sapi/cli/php bench.php ran 1.06 ± 0.06 times faster than ./sapi/cli/php_old bench.php ```
1 parent 956c3c2 commit ed91621

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

ext/dom/document.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,17 +913,17 @@ PHP_METHOD(DOM_Document, createElementNS)
913913
if (errorcode == 0) {
914914
php_dom_libxml_ns_mapper *ns_mapper = php_dom_get_ns_mapper(intern);
915915
xmlNsPtr ns = php_dom_libxml_ns_mapper_get_ns_raw_prefix_string(ns_mapper, prefix, xmlStrlen(prefix), uri);
916-
xmlNodePtr nodep = xmlNewDocNode(docp, ns, localname, NULL);
916+
xmlNodePtr nodep = xmlNewDocNodeEatName(docp, ns, localname, NULL);
917917
if (UNEXPECTED(nodep == NULL)) {
918918
php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
919919
} else {
920920
DOM_RET_OBJ(nodep, intern);
921921
}
922922
} else {
923923
php_dom_throw_error(errorcode, dom_get_strict_error(intern->document));
924+
xmlFree(localname);
924925
}
925926

926-
xmlFree(localname);
927927
xmlFree(prefix);
928928
}
929929
/* }}} end dom_document_create_element_ns */

0 commit comments

Comments
 (0)