From 02e04238a2c44c9aabdc3ff06deec667557dc58e Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Fri, 4 Sep 2020 20:28:27 +0900 Subject: [PATCH] Add keepRefs field --- encode.go | 3 ++- encode_context.go | 4 +++- encode_vm.go | 10 +++++----- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/encode.go b/encode.go index f139c5c..e192e34 100644 --- a/encode.go +++ b/encode.go @@ -196,7 +196,8 @@ func (e *Encoder) encode(v interface{}) error { ctx: sync.Pool{ New: func() interface{} { return &encodeRuntimeContext{ - ptrs: make([]uintptr, codeLength), + ptrs: make([]uintptr, codeLength), + keepRefs: make([]unsafe.Pointer, 8), } }, }, diff --git a/encode_context.go b/encode_context.go index 0b30e45..ce52702 100644 --- a/encode_context.go +++ b/encode_context.go @@ -85,11 +85,13 @@ func (c *encodeCompileContext) decPtrIndex() { } type encodeRuntimeContext struct { - ptrs []uintptr + ptrs []uintptr + keepRefs []unsafe.Pointer } func (c *encodeRuntimeContext) init(p uintptr) { c.ptrs[0] = p + c.keepRefs = c.keepRefs[:0] } func (c *encodeRuntimeContext) ptr() uintptr { diff --git a/encode_vm.go b/encode_vm.go index 5f45b04..29ddb30 100644 --- a/encode_vm.go +++ b/encode_vm.go @@ -21,10 +21,6 @@ func store(base uintptr, idx uintptr, p uintptr) { *(*uintptr)(unsafe.Pointer(base + idx)) = p } -var ( - globalArray = []interface{}{} -) - func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { recursiveLevel := 0 seenPtr := map[uintptr]struct{}{} @@ -434,7 +430,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { mlen := maplen(unsafe.Pointer(ptr)) if mlen > 0 { iter := mapiterinit(code.typ, unsafe.Pointer(ptr)) - globalArray = append(globalArray, iter) + ctx.keepRefs = append(ctx.keepRefs, iter) store(ctxptr, code.elemIdx, 0) store(ctxptr, code.length, uintptr(mlen)) store(ctxptr, code.mapIter, uintptr(iter)) @@ -458,6 +454,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { mlen := maplen(unsafe.Pointer(ptr)) if mlen > 0 { iter := mapiterinit(code.typ, unsafe.Pointer(ptr)) + ctx.keepRefs = append(ctx.keepRefs, iter) store(ctxptr, code.elemIdx, 0) store(ctxptr, code.length, uintptr(mlen)) store(ctxptr, code.mapIter, uintptr(iter)) @@ -502,6 +499,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if mlen > 0 { e.encodeBytes([]byte{'{', '\n'}) iter := mapiterinit(code.typ, unsafe.Pointer(ptr)) + ctx.keepRefs = append(ctx.keepRefs, iter) store(ctxptr, code.elemIdx, 0) store(ctxptr, code.length, uintptr(mlen)) store(ctxptr, code.mapIter, uintptr(iter)) @@ -528,6 +526,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if mlen > 0 { e.encodeBytes([]byte{'{', '\n'}) iter := mapiterinit(code.typ, unsafe.Pointer(ptr)) + ctx.keepRefs = append(ctx.keepRefs, iter) store(ctxptr, code.elemIdx, 0) store(ctxptr, code.length, uintptr(mlen)) store(ctxptr, code.mapIter, uintptr(iter)) @@ -552,6 +551,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if mlen > 0 { e.encodeBytes([]byte{'{', '\n'}) iter := mapiterinit(code.typ, unsafe.Pointer(ptr)) + ctx.keepRefs = append(ctx.keepRefs, iter) store(ctxptr, code.elemIdx, 0) store(ctxptr, code.length, uintptr(mlen)) store(ctxptr, code.mapIter, uintptr(iter))