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

This commit is contained in:
Delyan Angelov 2024-08-23 13:40:38 +03:00 committed by GitHub
parent c788c08d36
commit 2bf59b14d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 3 deletions

View File

@ -1025,6 +1025,7 @@ pub mut:
mod string mod string
name string name string
full_name string full_name string
cached_name string
kind IdentKind kind IdentKind
info IdentInfo info IdentInfo
is_mut bool // if mut *token* is before name. Use `is_mut()` to lookup mut variable is_mut bool // if mut *token* is before name. Use `is_mut()` to lookup mut variable

View File

@ -395,7 +395,7 @@ __global nested_expr_str_calls = i64(0)
const max_nested_expr_str_calls = 300 const max_nested_expr_str_calls = 300
// string representation of expr // 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) str_calls := stdatomic.add_i64(&nested_expr_str_calls, 1)
if str_calls > ast.max_nested_expr_str_calls { if str_calls > ast.max_nested_expr_str_calls {
$if panic_on_deeply_nested_expr_str_calls ? { $if panic_on_deeply_nested_expr_str_calls ? {
@ -515,13 +515,22 @@ pub fn (x Expr) str() string {
return 'spawn ${x.call_expr}' return 'spawn ${x.call_expr}'
} }
Ident { Ident {
if x.cached_name != '' {
return x.cached_name
}
if obj := x.scope.find('${x.mod}.${x.name}') { if obj := x.scope.find('${x.mod}.${x.name}') {
if obj is ConstField && x.mod != 'main' { if obj is ConstField && x.mod != 'main' {
last_mod := x.mod.all_after_last('.') 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 { IfExpr {
mut parts := []string{} mut parts := []string{}