From 447f07cd28494caf6c9f08640bc6d355f93ed2f2 Mon Sep 17 00:00:00 2001 From: Tyson Andre Date: Fri, 25 Oct 2019 19:57:39 -0400 Subject: [PATCH] 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 methods.object_create(parser, &$$); + if ((parser->scanner.options & PHP_JSON_OBJECT_AS_ARRAY) && parser->methods.object_create == php_json_parser_object_create) { + ZVAL_EMPTY_ARRAY(&$$); + } else { + parser->methods.object_create(parser, &$$); + } } | member ; @@ -180,7 +186,11 @@ array_end: elements: /* empty */ { - parser->methods.array_create(parser, &$$); + if (parser->methods.array_create == php_json_parser_array_create) { + ZVAL_EMPTY_ARRAY(&$$); + } else { + parser->methods.array_create(parser, &$$); + } } | element ; -- 2.40.0