Commit 18d99ee
authored
json: Improve performance of
Instead of using a boolean flag to check for each element whether or not it is
the first, we just unconditionally append a comma after each element and then
remove the last comma at the end.
For:
<?php
$len = 0;
for ($i = 0; $i < 3_000_000; $i++) {
$len += strlen(json_encode(array_fill(0, 20, [])));
}
var_dump($len);
This is ~1.06 faster for a gcc 13.3 release build on a Intel(R) Core(TM)
i7-1365U.
Benchmark 1: /tmp/bench/before /tmp/bench/test6.php
Time (mean ± σ): 819.6 ms ± 2.8 ms [User: 816.4 ms, System: 2.4 ms]
Range (min … max): 816.9 ms … 825.0 ms 10 runs
Benchmark 2: /tmp/bench/after /tmp/bench/test6.php
Time (mean ± σ): 770.8 ms ± 5.8 ms [User: 766.6 ms, System: 2.9 ms]
Range (min … max): 765.3 ms … 785.8 ms 10 runs
Summary
/tmp/bench/after /tmp/bench/test6.php ran
1.06 ± 0.01 times faster than /tmp/bench/before /tmp/bench/test6.phpphp_json_encode_array() (#20092)1 parent f905950 commit 18d99ee
2 files changed
+22
-26
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
112 | | - | |
113 | 112 | | |
114 | 113 | | |
115 | 114 | | |
| |||
161 | 160 | | |
162 | 161 | | |
163 | 162 | | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | 163 | | |
171 | 164 | | |
172 | 165 | | |
| |||
186 | 179 | | |
187 | 180 | | |
188 | 181 | | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
189 | 190 | | |
190 | 191 | | |
191 | 192 | | |
| |||
197 | 198 | | |
198 | 199 | | |
199 | 200 | | |
200 | | - | |
| 201 | + | |
201 | 202 | | |
202 | 203 | | |
203 | 204 | | |
| |||
235 | 236 | | |
236 | 237 | | |
237 | 238 | | |
| 239 | + | |
238 | 240 | | |
239 | 241 | | |
240 | 242 | | |
| |||
247 | 249 | | |
248 | 250 | | |
249 | 251 | | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | 252 | | |
257 | 253 | | |
258 | 254 | | |
| |||
276 | 272 | | |
277 | 273 | | |
278 | 274 | | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | 275 | | |
285 | 276 | | |
286 | 277 | | |
| |||
293 | 284 | | |
294 | 285 | | |
295 | 286 | | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | 287 | | |
303 | 288 | | |
304 | 289 | | |
| |||
319 | 304 | | |
320 | 305 | | |
321 | 306 | | |
| 307 | + | |
| 308 | + | |
322 | 309 | | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
323 | 316 | | |
324 | 317 | | |
325 | 318 | | |
| |||
334 | 327 | | |
335 | 328 | | |
336 | 329 | | |
337 | | - | |
| 330 | + | |
338 | 331 | | |
339 | 332 | | |
340 | 333 | | |
| |||
0 commit comments