Skip to content

Commit

Permalink
ast: cache ident lookups for consts in ast Expr str (#22101)
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman authored Aug 23, 2024
1 parent c788c08 commit 2bf59b1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,7 @@ pub mut:
mod string
name string
full_name string
cached_name string
kind IdentKind
info IdentInfo
is_mut bool // if mut *token* is before name. Use `is_mut()` to lookup mut variable
Expand Down
15 changes: 12 additions & 3 deletions vlib/v/ast/str.v
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ __global nested_expr_str_calls = i64(0)
const max_nested_expr_str_calls = 300

// string representation of expr
pub fn (x Expr) str() string {
pub fn (x &Expr) str() string {
str_calls := stdatomic.add_i64(&nested_expr_str_calls, 1)
if str_calls > ast.max_nested_expr_str_calls {
$if panic_on_deeply_nested_expr_str_calls ? {
Expand Down Expand Up @@ -515,13 +515,22 @@ pub fn (x Expr) str() string {
return 'spawn ${x.call_expr}'
}
Ident {
if x.cached_name != '' {
return x.cached_name
}
if obj := x.scope.find('${x.mod}.${x.name}') {
if obj is ConstField && x.mod != 'main' {
last_mod := x.mod.all_after_last('.')
return '${last_mod}.${x.name}'
unsafe {
x.cached_name = '${last_mod}.${x.name}'
}
return x.cached_name
}
}
return x.name.clone()
unsafe {
x.cached_name = x.name.clone()
}
return x.cached_name
}
IfExpr {
mut parts := []string{}
Expand Down

0 comments on commit 2bf59b1

Please sign in to comment.