-
Notifications
You must be signed in to change notification settings - Fork 275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce stack usage when parsing JSON selection #6526
Conversation
CI performance tests
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In doing stack analysis, I feel like we should identify where we want the bottleneck. Like, certainly we never want a stack overflow at runtime during ApplyTo execution, it'd be much better to fail at composition (parsing or validation).
After this change, is the parser still the bottleneck? Or is it possible to parse selections which then overflow the stack when executing? Maybe this requires setting up fuzzing of some sort...
@dylan-apollo - the goal here is just to make the parser code more efficient in its use of the stack space. This is not attempting to solve the larger problem of limiting the input selection size to avoid stack overflow generally. In the cases I've tested, the parser is still the first point of failure, but that is far from conclusive. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks okay to me, and actually is easier to read to me, too! But that's because I'm not comfortable with nom
, so maybe wait for a second ✅ 😅
)), | ||
let (input, _) = spaces_or_comments(input)?; | ||
alt(( | ||
Self::parse_primitive, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is moving this just about code organization? I'd assume adding another function increases stack usage, not decreases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This actually does help considerably. My (possibly incorrect) understanding is that this reduces the number of parsers on the stack at this level. It adds a level for primitives, but that path is a dead end in terms of stack depth - primitives are processed with a very shallow depth of parsers. In contrast, the parse_object
path can be very deep, so when we go down that path, anything on the alt
parameter list at this level is amplified because it stays on the stack as we go down that branch.
✅ Docs preview has no changesThe preview was not built because there were no changes. Build ID: 83886935cc02f1d2286be4ff |
fede284
to
7966fb2
Compare
The
tuple
andalt
methods innom
can result in high stack usage because they can take a number of parsers as parameters, which are all on the stack at once. Breaking upalt
calls into smaller chunks of parsers and eliminatingtuple
in favor of incrementally calling parsers results in a large reduction in the amount of data that needs to be on the stack all at once.This allows parsing a greater depth of nested JSON selection syntax in a given stack size (from a depth 33 to 46 on my local machine for the test case I was using, a 40% improvement).
Checklist
Complete the checklist (and note appropriate exceptions) before the PR is marked ready-for-review.
Exceptions
Note any exceptions here
Notes
Footnotes
It may be appropriate to bring upcoming changes to the attention of other (impacted) groups. Please endeavour to do this before seeking PR approval. The mechanism for doing this will vary considerably, so use your judgement as to how and when to do this. ↩
Configuration is an important part of many changes. Where applicable please try to document configuration examples. ↩
Tick whichever testing boxes are applicable. If you are adding Manual Tests, please document the manual testing (extensively) in the Exceptions. ↩