Reduced memory usage

This commit is contained in:
Eduard Urbach 2024-07-22 17:49:44 +02:00
parent 1cee7fcaa0
commit 504111734f
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
5 changed files with 26 additions and 18 deletions

View File

@ -11,13 +11,5 @@ func (f *Function) AddVariable(variable *scope.Variable) {
f.Comment("%s = %s (%d uses)", variable.Name, variable.Register, variable.Alive) f.Comment("%s = %s (%d uses)", variable.Name, variable.Register, variable.Alive)
} }
s := f.CurrentScope() f.CurrentScope().AddVariable(variable)
variable.Scope = s
if s.Variables == nil {
s.Variables = map[string]*scope.Variable{}
}
s.Variables[variable.Name] = variable
s.Use(variable.Register)
} }

View File

@ -4,5 +4,5 @@ import "git.akyoto.dev/cli/q/src/build/scope"
// VariableByName returns the variable with the given name or `nil` if it doesn't exist. // VariableByName returns the variable with the given name or `nil` if it doesn't exist.
func (f *Function) VariableByName(name string) *scope.Variable { func (f *Function) VariableByName(name string) *scope.Variable {
return f.CurrentScope().Variables[name] return f.CurrentScope().VariableByName(name)
} }

View File

@ -11,7 +11,7 @@ func (f *Function) useVariable(variable *scope.Variable) {
continue continue
} }
local := scope.Variables[variable.Name] local := scope.VariableByName(variable.Name)
if local == nil { if local == nil {
continue continue

View File

@ -6,7 +6,25 @@ import (
// Scope represents an independent code block. // Scope represents an independent code block.
type Scope struct { type Scope struct {
Variables map[string]*Variable Variables []*Variable
InLoop bool InLoop bool
cpu.State cpu.State
} }
// AddVariable adds a new variable to the current scope.
func (s *Scope) AddVariable(variable *Variable) {
variable.Scope = s
s.Variables = append(s.Variables, variable)
s.Use(variable.Register)
}
// VariableByName returns the variable with the given name or `nil` if it doesn't exist.
func (s *Scope) VariableByName(name string) *Variable {
for _, v := range s.Variables {
if v.Name == name {
return v
}
}
return nil
}

View File

@ -26,25 +26,23 @@ func (stack *Stack) PushScope(body ast.AST, buffer []byte) *Scope {
if len(stack.Scopes) > 0 { if len(stack.Scopes) > 0 {
lastScope := stack.Scopes[len(stack.Scopes)-1] lastScope := stack.Scopes[len(stack.Scopes)-1]
s.State = lastScope.State s.State = lastScope.State
s.Variables = make(map[string]*Variable, len(lastScope.Variables)) s.Variables = make([]*Variable, 0, len(lastScope.Variables))
s.InLoop = lastScope.InLoop s.InLoop = lastScope.InLoop
for k, v := range lastScope.Variables { for _, v := range lastScope.Variables {
count := ast.Count(body, buffer, token.Identifier, v.Name) count := ast.Count(body, buffer, token.Identifier, v.Name)
if count == 0 { if count == 0 {
continue continue
} }
s.Variables[k] = &Variable{ s.Variables = append(s.Variables, &Variable{
Name: v.Name, Name: v.Name,
Register: v.Register, Register: v.Register,
Alive: count, Alive: count,
})
} }
} }
} else {
s.Variables = map[string]*Variable{}
}
stack.Scopes = append(stack.Scopes, s) stack.Scopes = append(stack.Scopes, s)
return s return s