json icon indicating copy to clipboard operation
json copied to clipboard

Make JSON::Parser initialization faster

Open luke-gru opened this issue 2 years ago • 11 comments

Remove calls to rb_funcall, don't go back in to interpreter if not necessary. Also remove unnecessary calls to rb_respond_to(val) when val is nil. Speedup of JSON.parse when parsing small amount of data in a tight loop is up to 40%.

luke-gru avatar Jan 29 '23 14:01 luke-gru

Here's the benchmark code: https://gist.github.com/luke-gru/adf7468fbc0a3807b5f9a875fcfce116

On my machine:

Rehearsal ----------------------------------------------

json parse (old)  4.140547   0.001651   4.142198 (  4.262071)

------------------------------------- total: 4.142198sec



                 user     system      total        real

json parse (old) 4.099210   0.011923   4.111133 (  4.246992)

Rehearsal ----------------------------------------------

json parse (new)   3.163645   0.003476   3.167121 (  3.290742)

------------------------------------- total: 3.167121sec



                 user     system      total        real

json parse (new)  3.121472   0.004022   3.125494 (  3.198764)

luke-gru avatar Jan 29 '23 14:01 luke-gru

Speedup of JSON.parse when parsing small amount of data in a tight loop is up to 40%.

:open_mouth:

mensfeld avatar Jan 29 '23 14:01 mensfeld

@luke-gru Can you keep the current indent style? We can view only code changes with https://github.com/flori/json/pull/512/files?w=1. But style changes may causes code-conflict when I backport this to ruby/ruby.

hsbt avatar Jan 30 '23 01:01 hsbt

HI @hsbt, for the JSON init function, it's a bit subtle but I had to remove the check if (!NIL_P(opts)) and move it below where I could assure that opts was a Hash, so I could use RHASH_SIZE() on it. So for that function it's not just a style change. For parser.c, I think I used a different version of ragel and all the whitespace changed. I could fix this, do you want me to look into it?

EDIT: my ragel is Ragel State Machine Compiler version 6.10 March 2017

luke-gru avatar Jan 30 '23 19:01 luke-gru

So for that function it's not just a style change.

I know that. I say parser.rl and parser.h. I hope you make your change is only https://github.com/flori/json/pull/512/files?w=1, not https://github.com/flori/json/pull/512/files.

hsbt avatar Jan 30 '23 21:01 hsbt

Understood, sorry for the confusion. Just updated it.

luke-gru avatar Jan 30 '23 22:01 luke-gru

I accept this proposal. But I'm not familiar with C internals. I requested additional review for @nobu

hsbt avatar Feb 07 '23 06:02 hsbt

@luke-gru @hsbt @nobu any ETA on this being released?

mensfeld avatar Mar 21 '23 09:03 mensfeld

Yeah, I'll try to get this updated some time today. Thanks for the reminder 😄

luke-gru avatar Mar 21 '23 13:03 luke-gru

I finally got around to working on this. I added a 2nd commit so you can see the changes from the first. When approved, I will squash them.

luke-gru avatar Apr 03 '23 16:04 luke-gru

@luke-gru @hsbt @nobu any ETA on this being released? :D (it's more than a year from the last time I asked so hope you don't mind)

mensfeld avatar May 08 '24 09:05 mensfeld