Skip to content

Commit

Permalink
Avoid copying green nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyalesokhin-starkware committed Oct 11, 2023
1 parent 734a288 commit 8dd3ce2
Show file tree
Hide file tree
Showing 16 changed files with 1,638 additions and 1,302 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl SemanticTokensTraverser {
) {
let syntax_db = db.upcast();
let green_node = node.green_node(syntax_db);
match green_node.details {
match &green_node.details {
syntax::node::green::GreenNodeDetails::Token(text) => {
if green_node.kind == SyntaxKind::TokenNewline {
self.encoder.next_line();
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-parser/src/colored_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ struct ColoredPrinter<'a> {
impl<'a> ColoredPrinter<'a> {
fn print(&mut self, syntax_node: &SyntaxNode) {
let node = syntax_node.green_node(self.db);
match node.details {
match &node.details {
GreenNodeDetails::Token(text) => {
if self.verbose && node.kind == SyntaxKind::TokenMissing {
self.result.push_str(format!("{}", "<m>".red()).as_str());
} else {
self.result.push_str(set_color(text, node.kind).to_string().as_str());
self.result.push_str(set_color(text.clone(), node.kind).to_string().as_str());
}
}
GreenNodeDetails::Node { .. } => {
Expand Down
9 changes: 5 additions & 4 deletions crates/cairo-lang-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ impl<'a> Parser<'a> {
let GreenNode {
kind: SyntaxKind::ExprPath,
details: GreenNodeDetails::Node { children: children0, .. },
} = &self.db.lookup_intern_green(expr.0)
} = &*self.db.lookup_intern_green(expr.0)
else {
return None;
};
Expand All @@ -1209,7 +1209,7 @@ impl<'a> Parser<'a> {
let GreenNode {
kind: SyntaxKind::PathSegmentSimple,
details: GreenNodeDetails::Node { children: children1, .. },
} = self.db.lookup_intern_green(path_segment)
} = &*self.db.lookup_intern_green(path_segment)
else {
return None;
};
Expand All @@ -1221,7 +1221,7 @@ impl<'a> Parser<'a> {

// Check that it is indeed `TerminalIdentifier`.
let GreenNode { kind: SyntaxKind::TerminalIdentifier, .. } =
self.db.lookup_intern_green(ident)
self.db.lookup_intern_green(ident).as_ref()
else {
return None;
};
Expand Down Expand Up @@ -1478,7 +1478,8 @@ impl<'a> Parser<'a> {
PatternEnum::new_green(self.db, path, inner_pattern.into()).into()
}
_ => {
let children = match self.db.lookup_intern_green(path.0).details {
let green_node = self.db.lookup_intern_green(path.0);
let children = match &green_node.details {
GreenNodeDetails::Node { children, width: _ } => children,
_ => return Err(TryParseFailure::SkipToken),
};
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-parser/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ impl<'a> Printer<'a> {
) {
let extra_head_indent = if is_last { "└── " } else { "├── " };
let green_node = syntax_node.green_node(self.db);
match green_node.details {
match &green_node.details {
syntax::node::green::GreenNodeDetails::Token(text) => {
if under_top_level {
self.print_token_node(
field_description,
indent,
extra_head_indent,
text,
text.clone(),
green_node.kind,
)
}
Expand Down
42 changes: 22 additions & 20 deletions crates/cairo-lang-syntax-codegen/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ fn generate_key_fields_code() -> rust::Tokens {
$("/// Gets the vector of children ids that are the indexing key for this SyntaxKind.\n")
$("/// Each SyntaxKind has some children that are defined in the spec to be its indexing key\n")
$("/// for its stable pointer. See [super::stable_ptr].\n")
pub fn get_key_fields(kind: SyntaxKind, children: Vec<GreenId>) -> Vec<GreenId> {
// TODO(spapini): Implement this.
pub fn get_key_fields(kind: SyntaxKind, children: &[GreenId]) -> Vec<GreenId> {
match kind {
$arms
}
Expand All @@ -193,6 +192,7 @@ fn generate_ast_code() -> rust::Tokens {
#![allow(dead_code)]
#![allow(unused_variables)]
use std::ops::Deref;
use std::sync::Arc;

use cairo_lang_filesystem::span::TextWidth;
use cairo_lang_utils::extract_matches;
Expand Down Expand Up @@ -249,13 +249,13 @@ fn gen_list_code(name: String, element_type: String) -> rust::Tokens {
) -> $(&green_name) {
let width = children.iter().map(|id|
db.lookup_intern_green(id.0).width()).sum();
$(&green_name)(db.intern_green(GreenNode {
$(&green_name)(db.intern_green(Arc::new(GreenNode {
kind: SyntaxKind::$(&name),
details: GreenNodeDetails::Node {
children: children.iter().map(|x| x.0).collect(),
width,
},
}))
})))
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
Expand Down Expand Up @@ -300,13 +300,13 @@ fn gen_separated_list_code(
) -> $(&green_name) {
let width = children.iter().map(|id|
db.lookup_intern_green(id.id()).width()).sum();
$(&green_name)(db.intern_green(GreenNode {
$(&green_name)(db.intern_green(Arc::new(GreenNode {
kind: SyntaxKind::$(&name),
details: GreenNodeDetails::Node {
children: children.iter().map(|x| x.id()).collect(),
width,
},
}))
})))
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
Expand Down Expand Up @@ -355,12 +355,12 @@ fn gen_common_list_code(name: &str, green_name: &str, ptr_name: &str) -> rust::T
type StablePtr = $ptr_name;
type Green = $green_name;
fn missing(db: &dyn SyntaxGroup) -> Self::Green {
$green_name(db.intern_green(
$green_name(db.intern_green(Arc::new(
GreenNode {
kind: SyntaxKind::$name,
details: GreenNodeDetails::Node { children: vec![], width: TextWidth::default() },
})
)
))
}
fn from_syntax_node(db: &dyn SyntaxGroup, node: SyntaxNode) -> Self {
Self(ElementList::new(node))
Expand Down Expand Up @@ -490,13 +490,14 @@ fn gen_token_code(name: String) -> rust::Tokens {
}
impl Token for $(&name) {
fn new_green(db: &dyn SyntaxGroup, text: SmolStr) -> Self::Green {
$(&green_name)(db.intern_green(GreenNode {
$(&green_name)(db.intern_green(Arc::new(GreenNode {
kind: SyntaxKind::$(&name),
details: GreenNodeDetails::Token(text),
}))
})))
}
fn text(&self, db: &dyn SyntaxGroup) -> SmolStr {
extract_matches!(db.lookup_intern_green(self.node.0.green).details, GreenNodeDetails::Token)
extract_matches!(&db.lookup_intern_green(
self.node.0.green).details, GreenNodeDetails::Token).clone()
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
Expand All @@ -513,18 +514,19 @@ fn gen_token_code(name: String) -> rust::Tokens {
pub struct $(&green_name)(pub GreenId);
impl $(&green_name) {
pub fn text(&self, db: &dyn SyntaxGroup) -> SmolStr {
extract_matches!(db.lookup_intern_green(self.0).details, GreenNodeDetails::Token)
extract_matches!(
&db.lookup_intern_green(self.0).details, GreenNodeDetails::Token).clone()
}
}
impl TypedSyntaxNode for $(&name){
const OPTIONAL_KIND: Option<SyntaxKind> = Some(SyntaxKind::$(&name));
type StablePtr = $(&ptr_name);
type Green = $(&green_name);
fn missing(db: &dyn SyntaxGroup) -> Self::Green {
$(&green_name)(db.intern_green(GreenNode {
$(&green_name)(db.intern_green(Arc::new(GreenNode {
kind: SyntaxKind::TokenMissing,
details: GreenNodeDetails::Token("".into()),
}))
})))
}
fn from_syntax_node(db: &dyn SyntaxGroup, node: SyntaxNode) -> Self {
match db.lookup_intern_green(node.0.green).details {
Expand Down Expand Up @@ -602,10 +604,10 @@ fn gen_struct_code(name: String, members: Vec<Member>, is_terminal: bool) -> rus
let children: Vec<GreenId> = vec![$args];
let width = children.iter().copied().map(|id|
db.lookup_intern_green(id).width()).sum();
$(&green_name)(db.intern_green(GreenNode {
$(&green_name)(db.intern_green(Arc::new(GreenNode {
kind: SyntaxKind::$(&name),
details: GreenNodeDetails::Node { children, width },
}))
})))
}
fn text(&self, db: &dyn SyntaxGroup) -> SmolStr {
self.token(db).text(db)
Expand All @@ -620,10 +622,10 @@ fn gen_struct_code(name: String, members: Vec<Member>, is_terminal: bool) -> rus
let children: Vec<GreenId> = vec![$args];
let width = children.iter().copied().map(|id|
db.lookup_intern_green(id).width()).sum();
$(&green_name)(db.intern_green(GreenNode {
$(&green_name)(db.intern_green(Arc::new(GreenNode {
kind: SyntaxKind::$(&name),
details: GreenNodeDetails::Node { children, width },
}))
})))
}
}
}
Expand Down Expand Up @@ -659,13 +661,13 @@ fn gen_struct_code(name: String, members: Vec<Member>, is_terminal: bool) -> rus
fn missing(db: &dyn SyntaxGroup) -> Self::Green {
// Note: A missing syntax element should result in an internal green node
// of width 0, with as much structure as possible.
$(&green_name)(db.intern_green(GreenNode {
$(&green_name)(db.intern_green(Arc::new(GreenNode {
kind: SyntaxKind::$(&name),
details: GreenNodeDetails::Node {
children: vec![$arg_missings],
width: TextWidth::default(),
},
}))
})))
}
fn from_syntax_node(db: &dyn SyntaxGroup, node: SyntaxNode) -> Self {
let kind = node.kind(db);
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-lang-syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description = "Cairo syntax representation."
cairo-lang-debug = { path = "../cairo-lang-debug", version = "2.3.0-rc0" }
cairo-lang-filesystem = { path = "../cairo-lang-filesystem", version = "2.3.0-rc0" }
cairo-lang-utils = { path = "../cairo-lang-utils", version = "2.3.0-rc0" }
itertools.workspace = true
num-bigint.workspace = true
num-traits.workspace = true
salsa.workspace = true
Expand Down
Loading

0 comments on commit 8dd3ce2

Please sign in to comment.