1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

Don't use recursion when transferring a DOM internal document pointer (#16180)

Recursion is typically slower than iteration, and furthermore, this can
cause problems in theory with deep trees.
This commit is contained in:
Niels Dossche
2024-10-03 07:56:12 +02:00
committed by GitHub
parent 8b8a6733d1
commit c77a1291d6

View File

@@ -1074,7 +1074,7 @@ PHP_METHOD(DOMDocument, getElementById)
}
/* }}} end dom_document_get_element_by_id */
static zend_always_inline void php_dom_transfer_document_ref_single_node(xmlNodePtr node, php_libxml_ref_obj *new_document)
static void php_dom_transfer_document_ref_single_node(xmlNodePtr node, php_libxml_ref_obj *new_document)
{
php_libxml_node_ptr *iteration_object_ptr = node->_private;
if (iteration_object_ptr) {
@@ -1087,21 +1087,25 @@ static zend_always_inline void php_dom_transfer_document_ref_single_node(xmlNode
}
}
static void php_dom_transfer_document_ref_single_aux(xmlNodePtr node, php_libxml_ref_obj *new_document)
{
php_dom_transfer_document_ref_single_node(node, new_document);
if (node->type == XML_ELEMENT_NODE) {
for (xmlAttrPtr attr = node->properties; attr != NULL; attr = attr->next) {
php_dom_transfer_document_ref_single_node((xmlNodePtr) attr, new_document);
}
}
}
static void php_dom_transfer_document_ref(xmlNodePtr node, php_libxml_ref_obj *new_document)
{
if (node->children) {
php_dom_transfer_document_ref(node->children, new_document);
}
xmlNodePtr base = node;
php_dom_transfer_document_ref_single_aux(base, new_document);
while (node) {
if (node->type == XML_ELEMENT_NODE) {
for (xmlAttrPtr attr = node->properties; attr != NULL; attr = attr->next) {
php_dom_transfer_document_ref_single_node((xmlNodePtr) attr, new_document);
}
}
php_dom_transfer_document_ref_single_node(node, new_document);
node = node->next;
node = node->children;
while (node != NULL) {
php_dom_transfer_document_ref_single_aux(node, new_document);
node = php_dom_next_in_tree_order(node, base);
}
}