Skip to content

Commit b6ccca2

Browse files
authored
ast: reduce memory usage of ast.ScopeObject and ast.Ident instances (#24704)
1 parent 9e2462a commit b6ccca2

5 files changed

Lines changed: 32 additions & 11 deletions

File tree

‎cmd/tools/vast/vast.v‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,8 @@ fn (t Tree) objects(so map[string]ast.ScopeObject) &Node {
385385
}
386386

387387
fn (t Tree) scope_object(node ast.ScopeObject) &Node {
388-
mut obj := create_object()
389-
match node {
388+
obj := match node {
389+
ast.EmptyScopeObject { create_object() }
390390
ast.ConstField { t.const_field(node) }
391391
ast.GlobalField { t.global_field(node) }
392392
ast.Var { t.var(node) }

‎vlib/v/ast/ast.v‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,13 @@ pub type Stmt = AsmStmt
117117
| StructDecl
118118
| TypeDecl
119119

120-
pub type ScopeObject = AsmRegister | ConstField | GlobalField | Var
120+
pub struct EmptyScopeObject {
121+
pub mut:
122+
name string
123+
typ Type
124+
}
125+
126+
pub type ScopeObject = EmptyScopeObject | AsmRegister | ConstField | GlobalField | Var
121127

122128
// TODO: replace Param
123129
pub type Node = CallArg
@@ -209,6 +215,7 @@ pub:
209215
pub const empty_expr = Expr(EmptyExpr(0))
210216
pub const empty_stmt = Stmt(EmptyStmt{})
211217
pub const empty_node = Node(EmptyNode{})
218+
pub const empty_scope_object = ScopeObject(EmptyScopeObject{'empty_scope_object', 0})
212219
pub const empty_comptime_const_value = ComptTimeConstValue(EmptyExpr(0))
213220

214221
// `{stmts}` or `unsafe {stmts}`
@@ -1076,8 +1083,8 @@ pub:
10761083
mut_pos token.Pos
10771084
comptime bool
10781085
pub mut:
1079-
scope &Scope = unsafe { nil }
1080-
obj ScopeObject
1086+
scope &Scope = unsafe { nil }
1087+
obj ScopeObject = empty_scope_object
10811088
mod string
10821089
name string
10831090
full_name string
@@ -1115,7 +1122,7 @@ pub fn (i &Ident) is_auto_heap() bool {
11151122
pub fn (i &Ident) is_mut() bool {
11161123
match i.obj {
11171124
Var { return i.obj.is_mut }
1118-
ConstField { return false }
1125+
ConstField, EmptyScopeObject { return false }
11191126
AsmRegister, GlobalField { return true }
11201127
}
11211128
}
@@ -2403,7 +2410,7 @@ pub fn (node Node) pos() token.Pos {
24032410
ConstField, GlobalField, Var {
24042411
return node.pos
24052412
}
2406-
AsmRegister {
2413+
EmptyScopeObject, AsmRegister {
24072414
return token.Pos{
24082415
len: -1
24092416
line_nr: -1
@@ -2551,7 +2558,7 @@ pub fn (node Node) children() []Node {
25512558
} else if node is ScopeObject {
25522559
match node {
25532560
GlobalField, ConstField, Var { children << node.expr }
2554-
AsmRegister {}
2561+
AsmRegister, EmptyScopeObject {}
25552562
}
25562563
} else {
25572564
match node {

‎vlib/v/ast/scope.v‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,21 @@ pub fn (s &Scope) find_const(name string) ?&ConstField {
111111
}
112112

113113
pub fn (s &Scope) known_var(name string) bool {
114-
s.find_var(name) or { return false }
115-
return true
114+
if s == unsafe { nil } {
115+
return false
116+
}
117+
for sc := unsafe { s }; true; sc = sc.parent {
118+
if name in sc.objects {
119+
obj := unsafe { sc.objects[name] or { empty_scope_object } }
120+
if obj is Var {
121+
return true
122+
}
123+
}
124+
if sc.dont_lookup_parent() {
125+
break
126+
}
127+
}
128+
return false
116129
}
117130

118131
pub fn (s &Scope) known_global(name string) bool {

‎vlib/v/checker/checker.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4639,7 +4639,7 @@ fn (mut c Checker) find_obj_definition(obj ast.ScopeObject) !ast.Expr {
46394639
// TODO: remove once we have better type inference
46404640
mut name := ''
46414641
match obj {
4642-
ast.Var, ast.ConstField, ast.GlobalField, ast.AsmRegister { name = obj.name }
4642+
ast.EmptyScopeObject, ast.Var, ast.ConstField, ast.GlobalField, ast.AsmRegister { name = obj.name }
46434643
}
46444644
mut expr := ast.empty_expr
46454645
if obj is ast.Var {

‎vlib/v/gen/native/gen.v‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ fn (mut g Gen) is_fp_type(typ ast.Type) bool {
906906

907907
fn (mut g Gen) get_sizeof_ident(ident ast.Ident) i32 {
908908
typ := match ident.obj {
909+
ast.EmptyScopeObject { ident.obj.typ }
909910
ast.AsmRegister { ast.i64_type }
910911
ast.ConstField { ident.obj.typ }
911912
ast.GlobalField { ident.obj.typ }

0 commit comments

Comments
 (0)