Skip to content

Conversation

@eytan-starkware
Copy link
Contributor

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

Optimize Syntax Node Representation for Better Performance

TL;DR

Refactored the syntax node representation to eliminate the separate SyntaxStablePtr structure, reducing indirection and improving performance.

What changed?

  • Replaced the SyntaxStablePtr enum with a direct reference to SyntaxNode
  • Embedded key fields and index information directly in the SyntaxNodeData structure
  • Modified the node creation and lookup process to use this more efficient representation
  • Changed the parent representation to use Either<SyntaxNode, FileId> for clearer root node handling

NOTE
I introduced a semantic change:
Root is now actually a Node of SyntaxFile. This means:
a. parent can return a node that usually would panic
b. kind can return SyntaxFile
c. SyntaxFile key fields can be called

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Contributor Author

eytan-starkware commented Oct 5, 2025

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@eytan-starkware eytan-starkware marked this pull request as ready for review October 5, 2025 15:07
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/making_stable_syntax_pointers_just_a_wrapper_around_syntax_nodes branch from 1136962 to ef2bd29 Compare October 5, 2025 15:09
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 7 of 10 files at r1, all commit messages.
Reviewable status: 7 of 10 files reviewed, 3 unresolved discussions (waiting on @TomerStarkware)


crates/cairo-lang-defs/src/cache/mod.rs line 1569 at r1 (raw file):

}

#[derive(Serialize, Deserialize, Clone, Copy, Hash, Eq, PartialEq, salsa::Update)]

Probably not required - just need to remove the rest.

Suggestion:

#[derive(Serialize, Deserialize, Clone, Copy, Hash, Eq, PartialEq)]

crates/cairo-lang-defs/src/cache/mod.rs line 1576 at r1 (raw file):

    green: GreenIdCached,
    offset: TextOffset,
    // TODO(eytan-starkware): Using result for serde implementation. Change this to Either.

add serde for it - or just map to another serde function.

Code quote:

    // TODO(eytan-starkware): Using result for serde implementation. Change this to Either.

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

        offset: TextOffset,
        parent: Either<SyntaxNode<'db>, FileId<'db>>,
        index: usize,

f2f

Code quote:

        parent: Either<SyntaxNode<'db>, FileId<'db>>,
        index: usize,

crates/cairo-lang-defs/src/ids.rs line 790 at r1 (raw file):

    pub fn name(&self, db: &'db dyn Database) -> Option<SmolStrId<'db>> {
        let node = self.1.0.0;
        assert!(node.parent(db).is_some());

consider removing these - as these are extra queries not really needed.

Code quote:

        assert!(node.parent(db).is_some());

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 2 of 10 files at r1.
Reviewable status: 9 of 10 files reviewed, 3 unresolved discussions (waiting on @eytan-starkware and @TomerStarkware)

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/tracking_syntaxnode_and_interning_through_a_tracked_function branch from 3b0503b to 899e353 Compare October 8, 2025 10:50
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/making_stable_syntax_pointers_just_a_wrapper_around_syntax_nodes branch from ef2bd29 to 5e44fa2 Compare October 8, 2025 10:50
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: 9 of 10 files reviewed, 3 unresolved discussions (waiting on @orizi and @TomerStarkware)


crates/cairo-lang-defs/src/ids.rs line 790 at r1 (raw file):

Previously, orizi wrote…

consider removing these - as these are extra queries not really needed.

Will change to a non query accessor in the future. Added todo.


crates/cairo-lang-defs/src/cache/mod.rs line 1569 at r1 (raw file):

Previously, orizi wrote…

Probably not required - just need to remove the rest.

Some salsa::Update implementations are necessary for the loading data context to be salsa::Update. Removed what I could


crates/cairo-lang-defs/src/cache/mod.rs line 1576 at r1 (raw file):

Previously, orizi wrote…

add serde for it - or just map to another serde function.

Using my own enum now


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

Previously, orizi wrote…

f2f

Using a new enum for the unique identifier of the syntax node

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/tracking_syntaxnode_and_interning_through_a_tracked_function branch from 899e353 to d813a17 Compare October 8, 2025 13:32
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/making_stable_syntax_pointers_just_a_wrapper_around_syntax_nodes branch from 5e44fa2 to 148a496 Compare October 8, 2025 13:32
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 10 of 10 files at r1, 10 of 10 files at r2, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (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.

:lgtm:

@orizi reviewed 10 of 10 files at r2, 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/tracking_syntaxnode_and_interning_through_a_tracked_function branch from d813a17 to a78806e Compare October 15, 2025 11:37
@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
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 3 of 3 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/tracking_syntaxnode_and_interning_through_a_tracked_function branch from a78806e to 25b9606 Compare October 27, 2025 16:14
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.

4 participants