Skip to content

Cleanup and optimize attribute value reading #13897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2024

Conversation

nielsdos
Copy link
Member

@nielsdos nielsdos commented Apr 6, 2024

When the attribute has a single text child, we can avoid an allocating call to libxml2 and read the contents directly.

On my i7-4790, I tested the optimization with both the $value and $nodeValue property.

Summary
  ./sapi/cli/php bench_value.php ran
    1.82 ± 0.09 times faster than ./sapi/cli/php_old bench_value.php

Summary
  ./sapi/cli/php bench_nodeValue.php ran
    1.78 ± 0.10 times faster than ./sapi/cli/php_old bench_nodeValue.php

Test code:

$dom = new DOMDocument;
$dom->loadXML('<root attrib="this is a relatively short text"/>');
$attrib = $dom->documentElement->attributes[0];

for ($i=0; $i<1000*1000; $i++) {
	$attrib->value; // or nodeValue
}

When the attribute has a single text child, we can avoid an allocating
call to libxml2 and read the contents directly.

On my i7-4790, I tested the optimization with both the $value and
$nodeValue property.

```
Summary
  ./sapi/cli/php bench_value.php ran
    1.82 ± 0.09 times faster than ./sapi/cli/php_old bench_value.php

Summary
  ./sapi/cli/php bench_nodeValue.php ran
    1.78 ± 0.10 times faster than ./sapi/cli/php_old bench_nodeValue.php
```

Test code:
```
$dom = new DOMDocument;
$dom->loadXML('<root attrib="this is a relatively short text"/>');
$attrib = $dom->documentElement->attributes[0];

for ($i=0; $i<1000*1000; $i++) {
	$attrib->value; // or nodeValue
}
```
@nielsdos nielsdos merged commit a0da32a into php:master Apr 7, 2024
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