Skip to content

ext/dom: Avoid fetching the class attribute value twice - #23417

Closed
LamentXU123 wants to merge 1 commit into
php:masterfrom
LamentXU123:dom-opt
Closed

ext/dom: Avoid fetching the class attribute value twice#23417
LamentXU123 wants to merge 1 commit into
php:masterfrom
LamentXU123:dom-opt

Conversation

@LamentXU123

Copy link
Copy Markdown
Member

DOMTokenList already retrieves the class attribute value when checking whether its cached token set is stale. And When rebuilding the set, the same value was fetched again.

So here, For attributes consisting of multiple child nodes, fetching the value calls xmlNodeGetContent and we can actually optimize this process by removing this redundant allocation.

Comment thread UPGRADING

- DOM:
. Made splitText() faster and consume less memory.
. Improved performance and reduced memory usage when updating Dom\TokenList

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The premise doesn't hold. php_libxml_attr_value() only calls xmlNodeGetContent() when the attribute has more than one child; with a single text child it returns a borrowed pointer and allocates nothing. A class attribute is only split across children straight out of the parser with an unsubstituted entity reference, and every write collapses it back via xmlSetNsProp() or a single xmlNewDocText(). Verified at runtime on libxml2 2.15.3.

The constructor already fetched once and still does. The duplicate removed here is in ensure_set_up_to_date, which only runs once the value changed, by which point the attribute is collapsed. So this saves one xmlHasNsProp() lookup, not an allocation. Two million forced rebuilds showed no difference outside noise.

No allocation is avoided and no performance change is measurable.

@LamentXU123

Copy link
Copy Markdown
Member Author

This seems reasonable. I will take a closer look later. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants