Skip to content

Replace validation visitor stacks with single-slot variables#5587

Open
swalkinshaw wants to merge 1 commit intomasterfrom
refactor-validation-visitor-stacks
Open

Replace validation visitor stacks with single-slot variables#5587
swalkinshaw wants to merge 1 commit intomasterfrom
refactor-validation-visitor-stacks

Conversation

@swalkinshaw
Copy link
Collaborator

Extracted #5578

Eliminates array push/pop operations per validation pass by replacing array-based stacks with single-slot variables that use the Ruby call stack for save/restore. Pre-allocates path array indexed by depth counter.

In our schema, this saves about ~5500 push/pop operations and resulted in a 5-6% perf improvement.

I'll defer to you if you think this makes the code more complex and harder to manage but I think it's about the same.

@field_definitions = []
@argument_definitions = []
@directive_definitions = []
@path = Array.new(32) # pre-allocated, indexed by @path_depth
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is interesting: IIUC, instead of dynamically growing and shrinking the Array, we're using a big array with the expectation that only the first @path_depth items are correct. Instead of appending a new item to @path, call sites assign a value in @path at the current @path_depth then increment @path_depth.

Just curious, is there any reason for 32 in particular?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in isolation this had a tiny improvement but in the context of removing the push/pop operations already I think going back to [] is just simpler. Ruby doubles the array size as it grows so really this might avoid like 3-4 resizes (a realloc + memcpy of a few pointers?) which is nothing. Plus for small queries it's over allocating technically.

Removed!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely didn't mean to argue against starting at 32 slots, I was just genuinely curious why 32. I thought I might learn some Ruby secrets! Either way is fine for me.

Eliminates array push/pop operations per validation pass by replacing
array-based stacks with single-slot variables that use the Ruby call stack
for save/restore. Pre-allocates path array indexed by depth counter.
@swalkinshaw swalkinshaw force-pushed the refactor-validation-visitor-stacks branch from befea5a to 91de7fd Compare March 25, 2026 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants