Skip to content

Commit f2aaea0

Browse files
authored
Use faster string construction in ext/dom in some places (#20005)
Many tag names are single characters, so we can use the fast string construction. In cases where a NULL name is used we can also use the empty string. This avoids some allocations and some work.
1 parent f73bedb commit f2aaea0

File tree

5 files changed

+19
-8
lines changed

5 files changed

+19
-8
lines changed

ext/dom/attr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ zend_result dom_attr_name_read(dom_object *obj, zval *retval)
8383

8484
if (php_dom_follow_spec_intern(obj)) {
8585
zend_string *str = dom_node_get_node_name_attribute_or_element((xmlNodePtr) attrp, false);
86-
ZVAL_NEW_STR(retval, str);
86+
ZVAL_STR(retval, str);
8787
} else {
8888
ZVAL_STRING(retval, (char *) attrp->name);
8989
}

ext/dom/documenttype.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-
3434
zend_result dom_documenttype_name_read(dom_object *obj, zval *retval)
3535
{
3636
DOM_PROP_NODE(xmlDtdPtr, dtdptr, obj);
37-
ZVAL_STRING(retval, dtdptr->name ? (char *) (dtdptr->name) : "");
37+
if (dtdptr->name) {
38+
ZVAL_STRING(retval, (const char *) dtdptr->name);
39+
} else {
40+
ZVAL_EMPTY_STRING(retval);
41+
}
3842
return SUCCESS;
3943
}
4044

ext/dom/dom_iterators.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ static void php_dom_iterator_current_key(zend_object_iterator *iter, zval *key)
127127
if (intern->ptr != NULL) {
128128
xmlNodePtr curnode = ((php_libxml_node_ptr *)intern->ptr)->node;
129129
if (curnode->type == XML_ATTRIBUTE_NODE && php_dom_follow_spec_intern(intern)) {
130-
ZVAL_NEW_STR(key, dom_node_get_node_name_attribute_or_element(curnode, false));
130+
ZVAL_STR(key, dom_node_get_node_name_attribute_or_element(curnode, false));
131131
} else {
132-
ZVAL_STRINGL(key, (const char *) curnode->name, xmlStrlen(curnode->name));
132+
ZVAL_STRINGL_FAST(key, (const char *) curnode->name, xmlStrlen(curnode->name));
133133
}
134134
} else {
135135
ZVAL_NULL(key);

ext/dom/element.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ zend_result dom_element_tag_name_read(dom_object *obj, zval *retval)
122122
bool uppercase = php_dom_follow_spec_intern(obj) && php_dom_ns_is_html_and_document_is_html(nodep);
123123

124124
zend_string *result = dom_node_get_node_name_attribute_or_element((const xmlNode *) nodep, uppercase);
125-
ZVAL_NEW_STR(retval, result);
125+
ZVAL_STR(retval, result);
126126

127127
return SUCCESS;
128128
}
@@ -375,7 +375,7 @@ PHP_METHOD(DOMElement, getAttributeNames)
375375
}
376376

377377
for (xmlAttrPtr attr = nodep->properties; attr; attr = attr->next) {
378-
ZVAL_NEW_STR(&tmp, dom_node_get_node_name_attribute_or_element((const xmlNode *) attr, false));
378+
ZVAL_STR(&tmp, dom_node_get_node_name_attribute_or_element((const xmlNode *) attr, false));
379379
zend_hash_next_index_insert(ht, &tmp);
380380
}
381381
}

ext/dom/node.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ zend_string *dom_node_get_node_name_attribute_or_element(const xmlNode *nodep, b
5252
if (nodep->ns != NULL && nodep->ns->prefix != NULL) {
5353
ret = dom_node_concatenated_name_helper(name_len, (const char *) nodep->name, strlen((const char *) nodep->ns->prefix), (const char *) nodep->ns->prefix);
5454
} else {
55+
if (name_len == 1) {
56+
if (uppercase) {
57+
return ZSTR_CHAR(zend_toupper_ascii(*nodep->name));
58+
} else {
59+
return ZSTR_CHAR((zend_uchar) *nodep->name);
60+
}
61+
}
5562
ret = zend_string_init((const char *) nodep->name, name_len, false);
5663
}
5764
if (uppercase) {
@@ -89,7 +96,7 @@ zend_result dom_node_node_name_read(dom_object *obj, zval *retval)
8996
uppercase = php_dom_follow_spec_intern(obj) && php_dom_ns_is_html_and_document_is_html(nodep);
9097
ZEND_FALLTHROUGH;
9198
case XML_ATTRIBUTE_NODE:
92-
ZVAL_NEW_STR(retval, dom_node_get_node_name_attribute_or_element(nodep, uppercase));
99+
ZVAL_STR(retval, dom_node_get_node_name_attribute_or_element(nodep, uppercase));
93100
break;
94101
case XML_NAMESPACE_DECL: {
95102
xmlNsPtr ns = nodep->ns;
@@ -635,7 +642,7 @@ zend_result dom_node_local_name_read(dom_object *obj, zval *retval)
635642
DOM_PROP_NODE(xmlNodePtr, nodep, obj);
636643

637644
if (nodep->type == XML_ELEMENT_NODE || nodep->type == XML_ATTRIBUTE_NODE || nodep->type == XML_NAMESPACE_DECL) {
638-
ZVAL_STRING(retval, (char *) (nodep->name));
645+
ZVAL_STRING_FAST(retval, (const char *) (nodep->name));
639646
} else {
640647
ZVAL_NULL(retval);
641648
}

0 commit comments

Comments
 (0)