Tune parser for query with lots of nested fields #2572
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I saw that @DmitryTsepelev gave a really good talk about graphql-ruby's internals, and it included some detailed benchmarks. I decided to see if I could improve them 😈
They're here: https://gist.github.com/DmitryTsepelev/36e290cf64b4ec0b18294d0a57fb26ff
Specifically, I looked at the parsing benchmark. I was able to reduce the memory by about half, but the time is about the same.
Before
After
The optimizations were:
Token.new
, to avoid allocating a Hash (😢 I wish that wasn't necessary!)Field#initialize_node
.line_and_column
which allocates a needless array (hilariously, this is a carryover from the very first parser, written in parslet. Great gem.)next_token
, use the same array over and over. This one seems kind of risky -- there's no guarantee thatracc
isn't mutating the array in its own way!