From c451dd87bb01637f089cdfadfbab1efebb9a0dd9 Mon Sep 17 00:00:00 2001 From: Watson Date: Sun, 25 Feb 2018 22:31:28 +0900 Subject: [PATCH] Improve JSON_parse_string() performance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `rb_str_resize()` might reallocate the heap area in String object and it might cause to downgrade the performance. When use `rb_str_new()` instead of `rb_str_buf_new()`, it does not allocate extra heap area internally. After then, it will not need to resize the object. This patch will be 36.6 % faster for parsing string value. ## Before ``` $ ruby bench.rb Warming up -------------------------------------- json 14.000 i/100ms Calculating ------------------------------------- json 144.882 (± 1.4%) i/s - 728.000 in 5.025682s ** Memory usage 1105348 ``` ## After ``` $ ruby bench.rb Warming up -------------------------------------- json 19.000 i/100ms Calculating ------------------------------------- json 197.928 (± 4.5%) i/s - 988.000 in 5.005914s ** Memory usage 1138428 ``` ## Test code ``` require 'json' require 'objspace' require 'securerandom' require 'benchmark/ips' obj = [] 1000.times do |i| obj << { "id": i, "uuid": SecureRandom.uuid, "created_at": Time.now } end json = obj.to_json Benchmark.ips do |x| x.report "json" do |iter| count = 0 while count < iter JSON.parse(json) count += 1 end end end puts "** Memory usage" p ObjectSpace.memsize_of_all String ``` --- ext/json/ext/parser/parser.c | 20 +++++++++----------- ext/json/ext/parser/parser.rl | 4 +--- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index d2e4eb66..49c01561 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -1514,7 +1514,7 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu int cs = EVIL; VALUE match_string; - *result = rb_str_buf_new(0); + *result = rb_str_new(NULL, 0); #line 1520 "parser.c" { @@ -1658,8 +1658,6 @@ case 7: if (json->symbolize_names && json->parsing_name) { *result = rb_str_intern(*result); - } else { - rb_str_resize(*result, RSTRING_LEN(*result)); } if (cs >= JSON_string_first_final) { return p + 1; @@ -1830,7 +1828,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) } -#line 1834 "parser.c" +#line 1832 "parser.c" enum {JSON_start = 1}; enum {JSON_first_final = 10}; enum {JSON_error = 0}; @@ -1838,7 +1836,7 @@ enum {JSON_error = 0}; enum {JSON_en_main = 1}; -#line 742 "parser.rl" +#line 740 "parser.rl" /* @@ -1855,16 +1853,16 @@ static VALUE cParser_parse(VALUE self) GET_PARSER; -#line 1859 "parser.c" +#line 1857 "parser.c" { cs = JSON_start; } -#line 758 "parser.rl" +#line 756 "parser.rl" p = json->source; pe = p + json->len; -#line 1868 "parser.c" +#line 1866 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1898,7 +1896,7 @@ case 1: cs = 0; goto _out; tr2: -#line 734 "parser.rl" +#line 732 "parser.rl" { char *np = JSON_parse_value(json, p, pe, &result, 0); if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;} @@ -1908,7 +1906,7 @@ cs = 0; if ( ++p == pe ) goto _test_eof10; case 10: -#line 1912 "parser.c" +#line 1910 "parser.c" switch( (*p) ) { case 13: goto st10; case 32: goto st10; @@ -1997,7 +1995,7 @@ case 9: _out: {} } -#line 761 "parser.rl" +#line 759 "parser.rl" if (cs >= JSON_first_final && p == pe) { return result; diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl index 29900a4a..1a0e0be7 100644 --- a/ext/json/ext/parser/parser.rl +++ b/ext/json/ext/parser/parser.rl @@ -535,7 +535,7 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu int cs = EVIL; VALUE match_string; - *result = rb_str_buf_new(0); + *result = rb_str_new(NULL, 0); %% write init; json->memo = p; %% write exec; @@ -553,8 +553,6 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu if (json->symbolize_names && json->parsing_name) { *result = rb_str_intern(*result); - } else { - rb_str_resize(*result, RSTRING_LEN(*result)); } if (cs >= JSON_string_first_final) { return p + 1;