php-src/ext/json
Tyson Andre 447f07cd28 Optimize creation of empty arrays in json_decode
Use the shared empty array from ZVAL_EMPTY_ARRAY

For code that created an 10 arrays of 100000 empty arrays
(has the same result with `$assoc=true` and `{}`)

- This is the worst-case comparison, but I'd expect 0-length arrays to be fairly
  common in regular data for json_decode
- The parser implementation was using function pointers so that third party
  extension developers could reuse the json parser for their own
  data structures, etc. (I think).

  This PR is meant to let those third party extensions continue working
  without changes.

Before this patch: In 0.126 seconds: added 97.99 MiB
After this patch:  In 0.096 seconds: added 41.99 MiB

```php
<?php
$json = '[' . str_repeat('[],', 100000) . "null]";
$start_memory = memory_get_usage();
$start_time = microtime(true);
$result = [];
for ($i = 0; $i < 10; $i++) {
    $result[] = json_decode($json);
}
$end_memory = memory_get_usage();
$end_time = microtime(true);
// Before this patch: In 0.126 seconds: added 97.99 MiB
// After this patch:  In 0.096 seconds: added 41.99 MiB
printf("In %.3f seconds: added %.2f MiB\n", $end_time - $start_time, ($end_memory - $start_memory)/1000000);

// For objects
$json = '[' . str_repeat('{},', 100000) . "null]";
$start_memory = memory_get_usage();
$start_time = microtime(true);
for ($i = 0; $i < 10; $i++) {
    $result[] = json_decode($json, true);
}
$end_memory = memory_get_usage();
$end_time = microtime(true);
// Before this patch: In 0.126 seconds: added 97.99 MiB
// After this patch:  In 0.096 seconds: added 41.99 MiB
printf("In %.3f seconds: added %.2f MiB (objects decoded as arrays) \n", $end_time - $start_time, ($end_memory - $start_memory)/1000000);
```

Closes GH-4861.
2019-10-30 11:06:46 +01:00
..
tests Add the last missing SKIPIF 2019-09-04 08:53:35 +02:00
config.m4 Normalize comments in *nix build system m4 files 2019-05-12 18:43:03 +02:00
config.w32 Sync leading and final newlines in source code files 2018-10-14 12:56:38 +02:00
CREDITS Initial import of jsond 2014-11-13 20:20:46 +00:00
json.c Bump PHP_JSON_VERSION to PHP_VERSION 2019-07-24 22:19:33 +02:00
json_encoder.c Merge branch 'PHP-7.3' into PHP-7.4 2019-04-23 12:43:37 +02:00
json_parser.y Optimize creation of empty arrays in json_decode 2019-10-30 11:06:46 +01:00
json_scanner.re Remove yearly range from copyright notice 2019-01-30 11:03:12 +02:00
Makefile.frag Remove ext/json parser files generated by bison 2018-10-24 20:13:04 +02:00
Makefile.frag.w32 Sync leading and final newlines in source code files 2018-10-14 12:56:38 +02:00
php_json.h Bump PHP_JSON_VERSION to PHP_VERSION 2019-07-24 22:19:33 +02:00
php_json_encoder.h Remove yearly range from copyright notice 2019-01-30 11:03:12 +02:00
php_json_parser.h Remove yearly range from copyright notice 2019-01-30 11:03:12 +02:00
php_json_scanner.h Remove yearly range from copyright notice 2019-01-30 11:03:12 +02:00