Commit 540fd6e
authored
dom: Optimize splitText() (php#20371)
This avoids duplicating the intermediate strings, by transferring
ownership.
It's hard to measure the improvement in a reliable way, as we have to
operate on the same node. The following benchmark shows a nice
improvement (although not perfect as a benchmark):
```php
<?php
$dom = new DOMDocument;
$dom->loadXML('<root>testabcdef</root>');
$text = $dom->documentElement->firstChild;
for ($i = 0; $i < 1000000; $i++) {
$text2 = clone $text;
$text2->splitText(5);
}
```
Only tested on my desktop i7-4790:
```
Benchmark 1: ./sapi/cli/php x.php
Time (mean ± σ): 284.1 ms ± 2.8 ms [User: 280.0 ms, System: 3.0 ms]
Range (min … max): 281.4 ms … 291.3 ms 10 runs
Benchmark 2: ./sapi/cli/php_old x.php
Time (mean ± σ): 314.0 ms ± 7.8 ms [User: 309.2 ms, System: 2.9 ms]
Range (min … max): 306.5 ms … 328.0 ms 10 runs
Summary
./sapi/cli/php x.php ran
1.11 ± 0.03 times faster than ./sapi/cli/php_old x.php
```1 parent bda37c1 commit 540fd6e
2 files changed
+9
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
120 | 123 | | |
121 | 124 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
135 | 133 | | |
136 | 134 | | |
| 135 | + | |
137 | 136 | | |
138 | 137 | | |
139 | 138 | | |
140 | 139 | | |
| 140 | + | |
| 141 | + | |
141 | 142 | | |
142 | 143 | | |
143 | 144 | | |
| |||
0 commit comments