Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast: cache ident lookups for consts in ast Expr str #22101

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading