Skip to content

Conversation

@eytan-starkware
Copy link
Contributor

@eytan-starkware eytan-starkware commented Oct 8, 2025

TL;DR

Optimize SyntaxNode parent lookups by caching parent data to reduce database queries.

What changed?

  • Modified SyntaxNode to cache parent data, avoiding repeated database lookups
  • Added new helper methods like is_root() and grandparent() to improve code readability
  • Optimized nth_parent() to use cached parent data when possible
  • Improved parent-related lookups in various methods like file_id(), parent_kind().
  • Replaced multiple chained parent(db) calls with more efficient alternatives throughout the codebase

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Contributor Author

eytan-starkware commented Oct 8, 2025

@eytan-starkware eytan-starkware marked this pull request as ready for review October 8, 2025 13:34
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/keeping_syntaxnode_parent_s_data_id_for_optimizations branch from 6b047cc to 49347d0 Compare October 8, 2025 13:45
Copy link
Collaborator

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

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

:lgtm:

@TomerStarkware reviewed 4 of 4 files at r1, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @orizi)

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

@orizi reviewed 4 of 4 files at r1, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @eytan-starkware)


crates/cairo-lang-syntax/src/node/mod.rs line 188 at r1 (raw file):

            ptr = ptr.and_then(|p| p.parent(db));
        }
        ptr.unwrap_or_else(|| {

Suggestion:

        let mut result = Some(*self);
        while let Some(p) = result && n > 1 {
            result = p.grandparent(db);
            n -= 2;
        }
        if let Some(p) = result && n == 1 {
            result = p.parent(db);
        }
        result.unwrap_or_else(|| {

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/making_stable_syntax_pointers_just_a_wrapper_around_syntax_nodes branch from 148a496 to b8f0747 Compare October 15, 2025 11:37
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/keeping_syntaxnode_parent_s_data_id_for_optimizations branch from 49347d0 to 010cd59 Compare October 15, 2025 11:37
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/keeping_syntaxnode_parent_s_data_id_for_optimizations branch from 010cd59 to c071884 Compare October 15, 2025 13:10
Copy link
Contributor Author

@eytan-starkware eytan-starkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: 2 of 4 files reviewed, 1 unresolved discussion (waiting on @orizi and @TomerStarkware)


crates/cairo-lang-syntax/src/node/mod.rs line 188 at r1 (raw file):

            ptr = ptr.and_then(|p| p.parent(db));
        }
        ptr.unwrap_or_else(|| {

Done.

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

:lgtm:

@orizi reviewed 2 of 2 files at r2, 1 of 1 files at r3, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @eytan-starkware)

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/making_stable_syntax_pointers_just_a_wrapper_around_syntax_nodes branch from b8f0747 to 9dc334b Compare October 27, 2025 16:14
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/keeping_syntaxnode_parent_s_data_id_for_optimizations branch from c071884 to 8dfde2b Compare October 27, 2025 16:14
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/keeping_syntaxnode_parent_s_data_id_for_optimizations branch from 8dfde2b to a6b60a0 Compare October 28, 2025 12:28
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/making_stable_syntax_pointers_just_a_wrapper_around_syntax_nodes branch from 9dc334b to 487304f Compare October 28, 2025 12:28
Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

@orizi reviewed 1 of 1 files at r4, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @eytan-starkware)

@graphite-app graphite-app bot changed the base branch from eytan_graphite/making_stable_syntax_pointers_just_a_wrapper_around_syntax_nodes to graphite-base/8513 October 28, 2025 14:02
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/keeping_syntaxnode_parent_s_data_id_for_optimizations branch from a6b60a0 to d60d5d2 Compare October 28, 2025 14:18
@graphite-app graphite-app bot changed the base branch from graphite-base/8513 to main October 28, 2025 14:19
@graphite-app
Copy link

graphite-app bot commented Oct 28, 2025

Merge activity

  • Oct 28, 2:19 PM UTC: Graphite rebased this pull request, because this pull request is set to merge when ready.

@eytan-starkware eytan-starkware added this pull request to the merge queue Oct 28, 2025
Merged via the queue into main with commit 291a855 Oct 28, 2025
54 checks passed
@eytan-starkware eytan-starkware deleted the eytan_graphite/keeping_syntaxnode_parent_s_data_id_for_optimizations branch October 28, 2025 15:16
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.

5 participants