add parent field to sexp_node for O(1) parent lookups - #7714
Open
Goober5000 wants to merge 1 commit into
Open
Conversation
The sexp_node struct used first/rest pointers but had no parent pointer, requiring O(n) linear scans of the entire Sexp_nodes array to find a node's parent. Add an int parent field (-1 for root/unlinked) and maintain it at all assignment sites: alloc_sexp, free_sexp, get_sexp parse loop, and save_branch. Refactor find_sexp_list(), find_parent_operator(), and is_sexp_top_level() to use the parent field directly; and add find_sexp_antecedent() and find_sexp_root(). Also add parent_node guards to several functions to allow them to gracefully fail on nodes with no parents. Also add documentation on how SEXP nodes and lists work, particularly the gotchas for top-level nodes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Goober5000
force-pushed
the
sexp_node_parent
branch
from
August 17, 2026 04:02
4ce2044 to
53ce391
Compare
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.
The sexp_node struct used first/rest pointers but had no parent pointer, requiring O(n) linear scans of the entire Sexp_nodes array to find a node's parent. Add an int parent field (-1 for root/unlinked) and maintain it at all assignment sites: alloc_sexp, free_sexp, get_sexp parse loop, and save_branch. Refactor find_sexp_list(), find_parent_operator(), and is_sexp_top_level() to use the parent field directly; and add find_sexp_antecedent() and find_sexp_root().
Also add parent_node guards to several functions to allow them to gracefully fail on nodes with no parents.
Also add documentation on how SEXP nodes and lists work, particularly the gotchas for top-level nodes.