Skip to content

Commit c0ce3e7

Browse files
authored
Get rid of some unnecessary string conversion (#11733)
For typed properties that are of type "string", we don't need to do any conversion as the zval will already be a string. Removing this simplifies code and avoids unnecessary refcounting.
1 parent 6988973 commit c0ce3e7

File tree

4 files changed

+15
-33
lines changed

4 files changed

+15
-33
lines changed

ext/dom/attr.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,23 +135,20 @@ int dom_attr_value_read(dom_object *obj, zval *retval)
135135

136136
int dom_attr_value_write(dom_object *obj, zval *newval)
137137
{
138-
zend_string *str;
139138
xmlAttrPtr attrp = (xmlAttrPtr) dom_object_get_node(obj);
140139

141140
if (attrp == NULL) {
142141
php_dom_throw_error(INVALID_STATE_ERR, 1);
143142
return FAILURE;
144143
}
145144

146-
str = zval_try_get_string(newval);
147-
if (UNEXPECTED(!str)) {
148-
return FAILURE;
149-
}
145+
/* Typed property, this is already a string */
146+
ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING);
147+
zend_string *str = Z_STR_P(newval);
150148

151149
dom_remove_all_children((xmlNodePtr) attrp);
152150
xmlNodeSetContentLen((xmlNodePtr) attrp, (xmlChar *) ZSTR_VAL(str), ZSTR_LEN(str));
153151

154-
zend_string_release_ex(str, 0);
155152
return SUCCESS;
156153
}
157154

ext/dom/characterdata.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,18 @@ int dom_characterdata_data_read(dom_object *obj, zval *retval)
5252
int dom_characterdata_data_write(dom_object *obj, zval *newval)
5353
{
5454
xmlNode *nodep = dom_object_get_node(obj);
55-
zend_string *str;
5655

5756
if (nodep == NULL) {
5857
php_dom_throw_error(INVALID_STATE_ERR, 1);
5958
return FAILURE;
6059
}
6160

62-
str = zval_try_get_string(newval);
63-
if (UNEXPECTED(!str)) {
64-
return FAILURE;
65-
}
61+
/* Typed property, this is already a string */
62+
ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING);
63+
zend_string *str = Z_STR_P(newval);
6664

6765
xmlNodeSetContentLen(nodep, (xmlChar *) ZSTR_VAL(str), ZSTR_LEN(str));
6866

69-
zend_string_release_ex(str, 0);
7067
return SUCCESS;
7168
}
7269

ext/dom/node.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -669,10 +669,9 @@ int dom_node_prefix_write(dom_object *obj, zval *newval)
669669
nsnode = xmlDocGetRootElement(nodep->doc);
670670
}
671671
}
672-
prefix_str = zval_try_get_string(newval);
673-
if (UNEXPECTED(!prefix_str)) {
674-
return FAILURE;
675-
}
672+
/* Typed property, this is already a string */
673+
ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING);
674+
prefix_str = Z_STR_P(newval);
676675

677676
prefix = ZSTR_VAL(prefix_str);
678677
if (nsnode && nodep->ns != NULL && !xmlStrEqual(nodep->ns->prefix, (xmlChar *)prefix)) {
@@ -698,14 +697,12 @@ int dom_node_prefix_write(dom_object *obj, zval *newval)
698697
}
699698

700699
if (ns == NULL) {
701-
zend_string_release_ex(prefix_str, 0);
702700
php_dom_throw_error(NAMESPACE_ERR, dom_get_strict_error(obj->document));
703701
return FAILURE;
704702
}
705703

706704
xmlSetNs(nodep, ns);
707705
}
708-
zend_string_release_ex(prefix_str, 0);
709706
break;
710707
default:
711708
break;
@@ -791,21 +788,17 @@ int dom_node_text_content_read(dom_object *obj, zval *retval)
791788
int dom_node_text_content_write(dom_object *obj, zval *newval)
792789
{
793790
xmlNode *nodep = dom_object_get_node(obj);
794-
zend_string *str;
795791

796792
if (nodep == NULL) {
797793
php_dom_throw_error(INVALID_STATE_ERR, 1);
798794
return FAILURE;
799795
}
800796

801-
str = zval_try_get_string(newval);
802-
if (UNEXPECTED(!str)) {
803-
return FAILURE;
804-
}
805-
806797
php_libxml_invalidate_node_list_cache_from_doc(nodep->doc);
807798

808-
const xmlChar *xmlChars = (const xmlChar *) ZSTR_VAL(str);
799+
/* Typed property, this is already a string */
800+
ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING);
801+
const xmlChar *xmlChars = (const xmlChar *) Z_STRVAL_P(newval);
809802
int type = nodep->type;
810803

811804
/* We can't directly call xmlNodeSetContent, because it might encode the string through
@@ -822,8 +815,6 @@ int dom_node_text_content_write(dom_object *obj, zval *newval)
822815
xmlNodeSetContent(nodep, xmlChars);
823816
}
824817

825-
zend_string_release_ex(str, 0);
826-
827818
return SUCCESS;
828819
}
829820

ext/dom/processinginstruction.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,20 @@ int dom_processinginstruction_data_read(dom_object *obj, zval *retval)
108108
int dom_processinginstruction_data_write(dom_object *obj, zval *newval)
109109
{
110110
xmlNode *nodep = dom_object_get_node(obj);
111-
zend_string *str;
112111

113112
if (nodep == NULL) {
114113
php_dom_throw_error(INVALID_STATE_ERR, 1);
115114
return FAILURE;
116115
}
117116

118-
str = zval_try_get_string(newval);
119-
if (UNEXPECTED(!str)) {
120-
return FAILURE;
121-
}
117+
/* Typed property, this is already a string */
118+
ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING);
119+
zend_string *str = Z_STR_P(newval);
122120

123121
php_libxml_invalidate_node_list_cache_from_doc(nodep->doc);
124122

125123
xmlNodeSetContentLen(nodep, (xmlChar *) ZSTR_VAL(str), ZSTR_LEN(str));
126124

127-
zend_string_release_ex(str, 0);
128125
return SUCCESS;
129126
}
130127

0 commit comments

Comments
 (0)