refactor: convert json_encode_item() from recursive to iterative#19839
Closed
mattn wants to merge 2 commits intovim:masterfrom
Closed
refactor: convert json_encode_item() from recursive to iterative#19839mattn wants to merge 2 commits intovim:masterfrom
mattn wants to merge 2 commits intovim:masterfrom
Conversation
Replace recursive json_encode_item() with an explicit stack-based iterative implementation. This prevents stack overflow on deeply nested JSON structures and improves safety for LSP, ch_listen(), and other external data sources. The explicit stack (json_enc_frame_T) tracks list/tuple/dict iterator state. On error, all pending copyIDs are properly cleaned up, which the recursive version did not do.
Member
|
Thanks, but this |
Replace encode_value/item_done goto labels with for(;;) loops. The outer loop replaces goto encode_value (via continue), and the inner loop replaces goto item_done (via an advance flag to break back to the outer loop when the next value is ready).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Convert
json_encode_item()from a recursive implementation to an iterative one using an explicit stack (garray_Tofjson_enc_frame_Tframes).The recursive version uses C call stack proportional to nesting depth, which can cause a stack overflow (segfault) on deeply nested structures even when
maxfuncdepthis set high enough to allow it. The iterative version uses heap-allocated frames, so nesting depth is limited only by available memory.Benchmark comparison (average of 3 runs, ops/sec):
No performance regression. Complex structures (mixed, dict_100) show improvement.