From 2bf59b14d12c9b4abccaf74ef269813091db091e Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 23 Aug 2024 13:40:38 +0300 Subject: [PATCH] ast: cache ident lookups for consts in ast Expr str (#22101) --- vlib/v/ast/ast.v | 1 + vlib/v/ast/str.v | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 009b5f7c9b..14cfe95606 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -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 diff --git a/vlib/v/ast/str.v b/vlib/v/ast/str.v index 1b1063cc00..fdbc6560da 100644 --- a/vlib/v/ast/str.v +++ b/vlib/v/ast/str.v @@ -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 ? { @@ -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{}