Revert map context processing

This commit is contained in:
Masaaki Goshima 2021-01-24 04:35:45 +09:00
parent 9a1bd6e7ab
commit 669f910303
1 changed files with 38 additions and 24 deletions

View File

@ -348,9 +348,11 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcod
store(ctxptr, code.length, uintptr(mlen)) store(ctxptr, code.length, uintptr(mlen))
store(ctxptr, code.mapIter, uintptr(iter)) store(ctxptr, code.mapIter, uintptr(iter))
if !e.unorderedMap { if !e.unorderedMap {
mapCtx := newMapContext(mlen) pos := make([]int, 0, mlen)
mapCtx.pos = append(mapCtx.pos, len(b)) pos = append(pos, len(b))
store(ctxptr, code.end.mapPos, uintptr(unsafe.Pointer(mapCtx))) posPtr := unsafe.Pointer(&pos)
ctx.keepRefs = append(ctx.keepRefs, posPtr)
store(ctxptr, code.end.mapPos, uintptr(posPtr))
} }
key := mapiterkey(iter) key := mapiterkey(iter)
store(ctxptr, code.next.idx, uintptr(key)) store(ctxptr, code.next.idx, uintptr(key))
@ -387,9 +389,11 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcod
key := mapiterkey(iter) key := mapiterkey(iter)
store(ctxptr, code.next.idx, uintptr(key)) store(ctxptr, code.next.idx, uintptr(key))
if !e.unorderedMap { if !e.unorderedMap {
mapCtx := newMapContext(mlen) pos := make([]int, 0, mlen)
mapCtx.pos = append(mapCtx.pos, len(b)) pos = append(pos, len(b))
store(ctxptr, code.end.mapPos, uintptr(unsafe.Pointer(mapCtx))) posPtr := unsafe.Pointer(&pos)
ctx.keepRefs = append(ctx.keepRefs, posPtr)
store(ctxptr, code.end.mapPos, uintptr(posPtr))
} }
code = code.next code = code.next
} else { } else {
@ -417,8 +421,9 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcod
} }
} else { } else {
ptr := load(ctxptr, code.end.mapPos) ptr := load(ctxptr, code.end.mapPos)
mapCtx := (*encodeMapContext)(e.ptrToUnsafePtr(ptr)) posPtr := (*[]int)(*(*unsafe.Pointer)(unsafe.Pointer(&ptr)))
mapCtx.pos = append(mapCtx.pos, len(b)) *posPtr = append(*posPtr, len(b))
if idx < length { if idx < length {
ptr := load(ctxptr, code.mapIter) ptr := load(ctxptr, code.mapIter)
iter := e.ptrToUnsafePtr(ptr) iter := e.ptrToUnsafePtr(ptr)
@ -436,8 +441,8 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcod
b[last] = ':' b[last] = ':'
} else { } else {
ptr := load(ctxptr, code.end.mapPos) ptr := load(ctxptr, code.end.mapPos)
mapCtx := (*encodeMapContext)(e.ptrToUnsafePtr(ptr)) posPtr := (*[]int)(*(*unsafe.Pointer)(unsafe.Pointer(&ptr)))
mapCtx.pos = append(mapCtx.pos, len(b)) *posPtr = append(*posPtr, len(b))
} }
ptr := load(ctxptr, code.mapIter) ptr := load(ctxptr, code.mapIter)
iter := e.ptrToUnsafePtr(ptr) iter := e.ptrToUnsafePtr(ptr)
@ -449,8 +454,14 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcod
// this operation only used by sorted map. // this operation only used by sorted map.
length := int(load(ctxptr, code.length)) length := int(load(ctxptr, code.length))
ptr := load(ctxptr, code.mapPos) ptr := load(ctxptr, code.mapPos)
mapCtx := (*encodeMapContext)(e.ptrToUnsafePtr(ptr)) posPtr := e.ptrToUnsafePtr(ptr)
pos := mapCtx.pos pos := *(*[]int)(posPtr)
type mapKV struct {
key string
value string
}
kvs := make([]mapKV, 0, length)
for i := 0; i < length; i++ { for i := 0; i < length; i++ {
startKey := pos[i*2] startKey := pos[i*2]
startValue := pos[i*2+1] startValue := pos[i*2+1]
@ -460,22 +471,25 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcod
} else { } else {
endValue = len(b) endValue = len(b)
} }
mapCtx.slice.items = append(mapCtx.slice.items, mapItem{ kvs = append(kvs, mapKV{
key: b[startKey:startValue], key: string(b[startKey:startValue]),
value: b[startValue:endValue], value: string(b[startValue:endValue]),
}) })
} }
sort.Sort(mapCtx.slice) sort.Slice(kvs, func(i, j int) bool {
for _, item := range mapCtx.slice.items { return kvs[i].key < kvs[j].key
mapCtx.buf = append(mapCtx.buf, item.key...) })
mapCtx.buf[len(mapCtx.buf)-1] = ':' buf := b[pos[0]:]
mapCtx.buf = append(mapCtx.buf, item.value...) buf = buf[:0]
for _, kv := range kvs {
buf = append(buf, []byte(kv.key)...)
buf[len(buf)-1] = ':'
buf = append(buf, []byte(kv.value)...)
} }
mapCtx.buf[len(mapCtx.buf)-1] = '}' buf[len(buf)-1] = '}'
mapCtx.buf = append(mapCtx.buf, ',') buf = append(buf, ',')
b = b[:pos[0]] b = b[:pos[0]]
b = append(b, mapCtx.buf...) b = append(b, buf...)
releaseMapContext(mapCtx)
code = code.next code = code.next
case opStructFieldPtrAnonymousHeadRecursive: case opStructFieldPtrAnonymousHeadRecursive:
store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx))) store(ctxptr, code.idx, e.ptrToPtr(load(ctxptr, code.idx)))