Add keepRefs field

This commit is contained in:
Masaaki Goshima 2020-09-04 20:28:27 +09:00
parent 3e4e83bf34
commit 02e04238a2
3 changed files with 10 additions and 7 deletions

View File

@ -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),
}
},
},

View File

@ -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 {

View File

@ -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))