ast: reduce memory usage of ast.ScopeObject and ast.Ident instances (#24704)

This commit is contained in:
Delyan Angelov 2025-06-12 17:14:05 +03:00 committed by GitHub
parent 9e2462a876
commit b6ccca23a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 32 additions and 11 deletions

View File

@ -385,8 +385,8 @@ fn (t Tree) objects(so map[string]ast.ScopeObject) &Node {
} }
fn (t Tree) scope_object(node ast.ScopeObject) &Node { fn (t Tree) scope_object(node ast.ScopeObject) &Node {
mut obj := create_object() obj := match node {
match node { ast.EmptyScopeObject { create_object() }
ast.ConstField { t.const_field(node) } ast.ConstField { t.const_field(node) }
ast.GlobalField { t.global_field(node) } ast.GlobalField { t.global_field(node) }
ast.Var { t.var(node) } ast.Var { t.var(node) }

View File

@ -117,7 +117,13 @@ pub type Stmt = AsmStmt
| StructDecl | StructDecl
| TypeDecl | TypeDecl
pub type ScopeObject = AsmRegister | ConstField | GlobalField | Var pub struct EmptyScopeObject {
pub mut:
name string
typ Type
}
pub type ScopeObject = EmptyScopeObject | AsmRegister | ConstField | GlobalField | Var
// TODO: replace Param // TODO: replace Param
pub type Node = CallArg pub type Node = CallArg
@ -209,6 +215,7 @@ pub:
pub const empty_expr = Expr(EmptyExpr(0)) pub const empty_expr = Expr(EmptyExpr(0))
pub const empty_stmt = Stmt(EmptyStmt{}) pub const empty_stmt = Stmt(EmptyStmt{})
pub const empty_node = Node(EmptyNode{}) pub const empty_node = Node(EmptyNode{})
pub const empty_scope_object = ScopeObject(EmptyScopeObject{'empty_scope_object', 0})
pub const empty_comptime_const_value = ComptTimeConstValue(EmptyExpr(0)) pub const empty_comptime_const_value = ComptTimeConstValue(EmptyExpr(0))
// `{stmts}` or `unsafe {stmts}` // `{stmts}` or `unsafe {stmts}`
@ -1077,7 +1084,7 @@ pub:
comptime bool comptime bool
pub mut: pub mut:
scope &Scope = unsafe { nil } scope &Scope = unsafe { nil }
obj ScopeObject obj ScopeObject = empty_scope_object
mod string mod string
name string name string
full_name string full_name string
@ -1115,7 +1122,7 @@ pub fn (i &Ident) is_auto_heap() bool {
pub fn (i &Ident) is_mut() bool { pub fn (i &Ident) is_mut() bool {
match i.obj { match i.obj {
Var { return i.obj.is_mut } Var { return i.obj.is_mut }
ConstField { return false } ConstField, EmptyScopeObject { return false }
AsmRegister, GlobalField { return true } AsmRegister, GlobalField { return true }
} }
} }
@ -2403,7 +2410,7 @@ pub fn (node Node) pos() token.Pos {
ConstField, GlobalField, Var { ConstField, GlobalField, Var {
return node.pos return node.pos
} }
AsmRegister { EmptyScopeObject, AsmRegister {
return token.Pos{ return token.Pos{
len: -1 len: -1
line_nr: -1 line_nr: -1
@ -2551,7 +2558,7 @@ pub fn (node Node) children() []Node {
} else if node is ScopeObject { } else if node is ScopeObject {
match node { match node {
GlobalField, ConstField, Var { children << node.expr } GlobalField, ConstField, Var { children << node.expr }
AsmRegister {} AsmRegister, EmptyScopeObject {}
} }
} else { } else {
match node { match node {

View File

@ -111,8 +111,21 @@ pub fn (s &Scope) find_const(name string) ?&ConstField {
} }
pub fn (s &Scope) known_var(name string) bool { pub fn (s &Scope) known_var(name string) bool {
s.find_var(name) or { return false } if s == unsafe { nil } {
return false
}
for sc := unsafe { s }; true; sc = sc.parent {
if name in sc.objects {
obj := unsafe { sc.objects[name] or { empty_scope_object } }
if obj is Var {
return true return true
}
}
if sc.dont_lookup_parent() {
break
}
}
return false
} }
pub fn (s &Scope) known_global(name string) bool { pub fn (s &Scope) known_global(name string) bool {

View File

@ -4639,7 +4639,7 @@ fn (mut c Checker) find_obj_definition(obj ast.ScopeObject) !ast.Expr {
// TODO: remove once we have better type inference // TODO: remove once we have better type inference
mut name := '' mut name := ''
match obj { match obj {
ast.Var, ast.ConstField, ast.GlobalField, ast.AsmRegister { name = obj.name } ast.EmptyScopeObject, ast.Var, ast.ConstField, ast.GlobalField, ast.AsmRegister { name = obj.name }
} }
mut expr := ast.empty_expr mut expr := ast.empty_expr
if obj is ast.Var { if obj is ast.Var {

View File

@ -906,6 +906,7 @@ fn (mut g Gen) is_fp_type(typ ast.Type) bool {
fn (mut g Gen) get_sizeof_ident(ident ast.Ident) i32 { fn (mut g Gen) get_sizeof_ident(ident ast.Ident) i32 {
typ := match ident.obj { typ := match ident.obj {
ast.EmptyScopeObject { ident.obj.typ }
ast.AsmRegister { ast.i64_type } ast.AsmRegister { ast.i64_type }
ast.ConstField { ident.obj.typ } ast.ConstField { ident.obj.typ }
ast.GlobalField { ident.obj.typ } ast.GlobalField { ident.obj.typ }