Skip to content

Cleanup and optimize attribute value reading#13897

Merged
ndossche merged 1 commit intophp:masterfrom
ndossche:dom-attr-opt
Apr 7, 2024
Merged

Cleanup and optimize attribute value reading#13897
ndossche merged 1 commit intophp:masterfrom
ndossche:dom-attr-opt

Conversation

@ndossche
Copy link
Member

@ndossche ndossche 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
}
```
@ndossche ndossche 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