Skip to content

Commit

Permalink
defer varg
Browse files Browse the repository at this point in the history
  • Loading branch information
ltzmaxwell committed Jan 16, 2025
1 parent b935904 commit 4248a29
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
14 changes: 8 additions & 6 deletions gnovm/pkg/gnolang/alloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,16 @@ func (alloc *Allocator) GC() {
}
// defer func
for _, dfr := range fr.Defers {
debug2.Println2("has defer")
fv := dfr.Func
ft := fv.GetType(alloc.m.Store)
numParams := len(ft.Params)
numArgs := len(dfr.Args)
nvar := numArgs - (numParams - 1)
throwaway.AllocateSlice()
throwaway.AllocateListArray(int64(nvar))
if ft.HasVarg() {
debug2.Println2("has defer, has varg")
numParams := len(ft.Params)
numArgs := len(dfr.Args)
nvar := numArgs - (numParams - 1)
throwaway.AllocateSlice()
throwaway.AllocateListArray(int64(nvar))
}
}
}

Expand Down
15 changes: 14 additions & 1 deletion gnovm/tests/files/alloc1.gno
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ var m = map[string]int{"a": 1}

type Foo struct{ name string }

func xyz(args ...int) {
runtime.GC() // in this case, frame contains this one and main.
}

func xyz2() {
defer func(args ...int) {
//
}()
runtime.GC() // in this case, frame contains this one and main.
}

// after new block: 3483
func bar() {
f := &Foo{"foo"} // + field 40 + struct 184 + pointer 24 + heapItem 72 + NewType: *main.Foo 232 = 4305
Expand All @@ -36,12 +47,14 @@ func main() {
}
f()
runtime.GC()
xyz(1, 2, 3)
xyz2()
}

// Output:
// MemStats 1: Allocator{maxBytes:10000000000, bytes:1944}
// &(struct{("foo" string)} main.Foo)
// MemStats 2: Allocator{maxBytes:10000000000, bytes:6019}
// MemStats before GC: Allocator{maxBytes:10000000000, bytes:7518}
// MemStats after GC: Allocator{maxBytes:10000000000, bytes:2901}
// MemStats after GC: Allocator{maxBytes:10000000000, bytes:2981}
// func lit

0 comments on commit 4248a29

Please sign in to comment.