tau.patch
Raw
diff --git a/internal/vm/frame.go b/internal/vm/frame.go
index 4d40d22..d18e159 100644
--- a/internal/vm/frame.go
+++ b/internal/vm/frame.go
@@ -1,10 +1,14 @@
package vm
import (
+ "fmt"
+
"github.com/NicoNex/tau/internal/code"
"github.com/NicoNex/tau/internal/obj"
)
+var cache = make(map[string]*Frame)
+
type Frame struct {
cl *obj.Closure
ip int
@@ -12,11 +16,22 @@ type Frame struct {
}
func NewFrame(cl *obj.Closure, basePointer int) *Frame {
- return &Frame{
+ key := fmt.Sprintf("%p:%d", cl, basePointer)
+ if frame, ok := cache[key]; ok {
+ frame.Reset()
+ return frame
+ }
+ frame := &Frame{
cl: cl,
ip: -1,
basePointer: basePointer,
}
+ cache[key] = frame
+ return frame
+}
+
+func (f *Frame) Reset() {
+ f.ip = -1
}
func (f *Frame) Instructions() code.Instructions {
1 | diff --git a/internal/vm/frame.go b/internal/vm/frame.go |
2 | index 4d40d22..d18e159 100644 |
3 | --- a/internal/vm/frame.go |
4 | +++ b/internal/vm/frame.go |
5 | @@ -1,10 +1,14 @@ |
6 | package vm |
7 | |
8 | import ( |
9 | + "fmt" |
10 | + |
11 | "github.com/NicoNex/tau/internal/code" |
12 | "github.com/NicoNex/tau/internal/obj" |
13 | ) |
14 | |
15 | +var cache = make(map[string]*Frame) |
16 | + |
17 | type Frame struct { |
18 | cl *obj.Closure |
19 | ip int |
20 | @@ -12,11 +16,22 @@ type Frame struct { |
21 | } |
22 | |
23 | func NewFrame(cl *obj.Closure, basePointer int) *Frame { |
24 | - return &Frame{ |
25 | + key := fmt.Sprintf("%p:%d", cl, basePointer) |
26 | + if frame, ok := cache[key]; ok { |
27 | + frame.Reset() |
28 | + return frame |
29 | + } |
30 | + frame := &Frame{ |
31 | cl: cl, |
32 | ip: -1, |
33 | basePointer: basePointer, |
34 | } |
35 | + cache[key] = frame |
36 | + return frame |
37 | +} |
38 | + |
39 | +func (f *Frame) Reset() { |
40 | + f.ip = -1 |
41 | } |
42 | |
43 | func (f *Frame) Instructions() code.Instructions { |
44 |