Merge pull request #310 from goccy/feature/improve-map-encoding-performance

Improve map encoding performance
This commit is contained in:
Masaaki Goshima 2021-12-27 18:17:01 +09:00 committed by GitHub
commit 5686ae09f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 178 additions and 319 deletions

View File

@ -403,48 +403,41 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
break break
} }
b = appendStructHead(ctx, b) b = appendStructHead(ctx, b)
iter := mapiterinit(code.Type, uptr) mapCtx := encoder.NewMapContext(mlen)
ctx.KeepRefs = append(ctx.KeepRefs, iter) mapiterinit(code.Type, uptr, &mapCtx.Iter)
store(ctxptr, code.ElemIdx, 0) store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
store(ctxptr, code.Length, uintptr(mlen)) ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
store(ctxptr, code.MapIter, uintptr(iter))
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
b = appendMapKeyIndent(ctx, code.Next, b) b = appendMapKeyIndent(ctx, code.Next, b)
} else { } else {
mapCtx := encoder.NewMapContext(mlen) mapCtx.Start = len(b)
mapCtx.Pos = append(mapCtx.Pos, len(b)) mapCtx.First = len(b)
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
store(ctxptr, code.End.MapPos, uintptr(unsafe.Pointer(mapCtx)))
} }
key := mapiterkey(iter) key := mapiterkey(&mapCtx.Iter)
store(ctxptr, code.Next.Idx, uintptr(key)) store(ctxptr, code.Next.Idx, uintptr(key))
code = code.Next code = code.Next
case encoder.OpMapKey: case encoder.OpMapKey:
idx := load(ctxptr, code.ElemIdx) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
length := load(ctxptr, code.Length) idx := mapCtx.Idx
idx++ idx++
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
if idx < length { if idx < mapCtx.Len {
b = appendMapKeyIndent(ctx, code, b) b = appendMapKeyIndent(ctx, code, b)
store(ctxptr, code.ElemIdx, idx) mapCtx.Idx = int(idx)
ptr := load(ctxptr, code.MapIter) key := mapiterkey(&mapCtx.Iter)
iter := ptrToUnsafePtr(ptr)
key := mapiterkey(iter)
store(ctxptr, code.Next.Idx, uintptr(key)) store(ctxptr, code.Next.Idx, uintptr(key))
code = code.Next code = code.Next
} else { } else {
b = appendObjectEnd(ctx, code, b) b = appendObjectEnd(ctx, code, b)
encoder.ReleaseMapContext(mapCtx)
code = code.End.Next code = code.End.Next
} }
} else { } else {
ptr := load(ctxptr, code.End.MapPos) mapCtx.Slice.Items[mapCtx.Idx].Value = b[mapCtx.Start:len(b)]
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) if idx < mapCtx.Len {
mapCtx.Pos = append(mapCtx.Pos, len(b)) mapCtx.Idx = int(idx)
if idx < length { mapCtx.Start = len(b)
ptr := load(ctxptr, code.MapIter) key := mapiterkey(&mapCtx.Iter)
iter := ptrToUnsafePtr(ptr)
store(ctxptr, code.ElemIdx, idx)
key := mapiterkey(iter)
store(ctxptr, code.Next.Idx, uintptr(key)) store(ctxptr, code.Next.Idx, uintptr(key))
code = code.Next code = code.Next
} else { } else {
@ -452,46 +445,27 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
} }
case encoder.OpMapValue: case encoder.OpMapValue:
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
b = appendColon(ctx, b) b = appendColon(ctx, b)
} else { } else {
ptr := load(ctxptr, code.End.MapPos) mapCtx.Slice.Items[mapCtx.Idx].Key = b[mapCtx.Start:len(b)]
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) mapCtx.Start = len(b)
mapCtx.Pos = append(mapCtx.Pos, len(b))
} }
ptr := load(ctxptr, code.MapIter) value := mapitervalue(&mapCtx.Iter)
iter := ptrToUnsafePtr(ptr)
value := mapitervalue(iter)
store(ctxptr, code.Next.Idx, uintptr(value)) store(ctxptr, code.Next.Idx, uintptr(value))
mapiternext(iter) mapiternext(&mapCtx.Iter)
code = code.Next code = code.Next
case encoder.OpMapEnd: case encoder.OpMapEnd:
// this operation only used by sorted map. // this operation only used by sorted map.
length := int(load(ctxptr, code.Length)) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
ptr := load(ctxptr, code.MapPos)
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr))
pos := mapCtx.Pos
for i := 0; i < length; i++ {
startKey := pos[i*2]
startValue := pos[i*2+1]
var endValue int
if i+1 < length {
endValue = pos[i*2+2]
} else {
endValue = len(b)
}
mapCtx.Slice.Items = append(mapCtx.Slice.Items, encoder.MapItem{
Key: b[startKey:startValue],
Value: b[startValue:endValue],
})
}
sort.Sort(mapCtx.Slice) sort.Sort(mapCtx.Slice)
buf := mapCtx.Buf buf := mapCtx.Buf
for _, item := range mapCtx.Slice.Items { for _, item := range mapCtx.Slice.Items {
buf = appendMapKeyValue(ctx, code, buf, item.Key, item.Value) buf = appendMapKeyValue(ctx, code, buf, item.Key, item.Value)
} }
buf = appendMapEnd(ctx, code, buf) buf = appendMapEnd(ctx, code, buf)
b = b[:pos[0]] b = b[:mapCtx.First]
b = append(b, buf...) b = append(b, buf...)
mapCtx.Buf = buf mapCtx.Buf = buf
encoder.ReleaseMapContext(mapCtx) encoder.ReleaseMapContext(mapCtx)

View File

@ -222,33 +222,54 @@ func (m *Mapslice) Swap(i, j int) {
m.Items[i], m.Items[j] = m.Items[j], m.Items[i] m.Items[i], m.Items[j] = m.Items[j], m.Items[i]
} }
//nolint:structcheck,unused
type mapIter struct {
key unsafe.Pointer
elem unsafe.Pointer
t unsafe.Pointer
h unsafe.Pointer
buckets unsafe.Pointer
bptr unsafe.Pointer
overflow unsafe.Pointer
oldoverflow unsafe.Pointer
startBucket uintptr
offset uint8
wrapped bool
B uint8
i uint8
bucket uintptr
checkBucket uintptr
}
type MapContext struct { type MapContext struct {
Pos []int Start int
First int
Idx int
Slice *Mapslice Slice *Mapslice
Buf []byte Buf []byte
Len int
Iter mapIter
} }
var mapContextPool = sync.Pool{ var mapContextPool = sync.Pool{
New: func() interface{} { New: func() interface{} {
return &MapContext{} return &MapContext{
Slice: &Mapslice{},
}
}, },
} }
func NewMapContext(mapLen int) *MapContext { func NewMapContext(mapLen int) *MapContext {
ctx := mapContextPool.Get().(*MapContext) ctx := mapContextPool.Get().(*MapContext)
if ctx.Slice == nil { if len(ctx.Slice.Items) < mapLen {
ctx.Slice = &Mapslice{ ctx.Slice.Items = make([]MapItem, mapLen)
Items: make([]MapItem, 0, mapLen),
}
}
if cap(ctx.Pos) < (mapLen*2 + 1) {
ctx.Pos = make([]int, 0, mapLen*2+1)
ctx.Slice.Items = make([]MapItem, 0, mapLen)
} else { } else {
ctx.Pos = ctx.Pos[:0] ctx.Slice.Items = ctx.Slice.Items[:mapLen]
ctx.Slice.Items = ctx.Slice.Items[:0]
} }
ctx.Buf = ctx.Buf[:0] ctx.Buf = ctx.Buf[:0]
ctx.Iter = mapIter{}
ctx.Idx = 0
ctx.Len = mapLen
return ctx return ctx
} }
@ -256,17 +277,17 @@ func ReleaseMapContext(c *MapContext) {
mapContextPool.Put(c) mapContextPool.Put(c)
} }
//go:linkname MapIterInit reflect.mapiterinit //go:linkname MapIterInit runtime.mapiterinit
//go:noescape //go:noescape
func MapIterInit(mapType *runtime.Type, m unsafe.Pointer) unsafe.Pointer func MapIterInit(mapType *runtime.Type, m unsafe.Pointer, it *mapIter)
//go:linkname MapIterKey reflect.mapiterkey //go:linkname MapIterKey reflect.mapiterkey
//go:noescape //go:noescape
func MapIterKey(it unsafe.Pointer) unsafe.Pointer func MapIterKey(it *mapIter) unsafe.Pointer
//go:linkname MapIterNext reflect.mapiternext //go:linkname MapIterNext reflect.mapiternext
//go:noescape //go:noescape
func MapIterNext(it unsafe.Pointer) func MapIterNext(it *mapIter)
//go:linkname MapLen reflect.maplen //go:linkname MapLen reflect.maplen
//go:noescape //go:noescape

View File

@ -1,3 +1,4 @@
//go:build !go1.13
// +build !go1.13 // +build !go1.13
package encoder package encoder
@ -5,4 +6,4 @@ package encoder
import "unsafe" import "unsafe"
//go:linkname MapIterValue reflect.mapitervalue //go:linkname MapIterValue reflect.mapitervalue
func MapIterValue(it unsafe.Pointer) unsafe.Pointer func MapIterValue(it *mapIter) unsafe.Pointer

View File

@ -1,3 +1,4 @@
//go:build go1.13
// +build go1.13 // +build go1.13
package encoder package encoder
@ -5,4 +6,4 @@ package encoder
import "unsafe" import "unsafe"
//go:linkname MapIterValue reflect.mapiterelem //go:linkname MapIterValue reflect.mapiterelem
func MapIterValue(it unsafe.Pointer) unsafe.Pointer func MapIterValue(it *mapIter) unsafe.Pointer

View File

@ -39,10 +39,8 @@ type Opcode struct {
Type *runtime.Type // go type Type *runtime.Type // go type
Jmp *CompiledCode // for recursive call Jmp *CompiledCode // for recursive call
ElemIdx uint32 // offset to access array/slice/map elem ElemIdx uint32 // offset to access array/slice elem
Length uint32 // offset to access slice/map length or array length Length uint32 // offset to access slice length or array length
MapIter uint32 // offset to access map iterator
MapPos uint32 // offset to access position list for sorted map
Indent uint32 // indent number Indent uint32 // indent number
Size uint32 // array/slice elem size Size uint32 // array/slice elem size
DisplayIdx uint32 // opcode index DisplayIdx uint32 // opcode index
@ -91,8 +89,6 @@ func (c *Opcode) MaxIdx() uint32 {
c.Idx, c.Idx,
c.ElemIdx, c.ElemIdx,
c.Length, c.Length,
c.MapIter,
c.MapPos,
c.Size, c.Size,
} { } {
if max < value { if max < value {
@ -341,8 +337,6 @@ func copyOpcode(code *Opcode) *Opcode {
DisplayKey: c.DisplayKey, DisplayKey: c.DisplayKey,
ElemIdx: c.ElemIdx, ElemIdx: c.ElemIdx,
Length: c.Length, Length: c.Length,
MapIter: c.MapIter,
MapPos: c.MapPos,
Size: c.Size, Size: c.Size,
Indent: c.Indent, Indent: c.Indent,
Jmp: c.Jmp, Jmp: c.Jmp,
@ -448,26 +442,21 @@ func (c *Opcode) dumpHead(code *Opcode) string {
func (c *Opcode) dumpMapHead(code *Opcode) string { func (c *Opcode) dumpMapHead(code *Opcode) string {
return fmt.Sprintf( return fmt.Sprintf(
`[%03d]%s%s ([idx:%d][elemIdx:%d][length:%d][mapIter:%d])`, `[%03d]%s%s ([idx:%d])`,
code.DisplayIdx, code.DisplayIdx,
strings.Repeat("-", int(code.Indent)), strings.Repeat("-", int(code.Indent)),
code.Op, code.Op,
code.Idx/uintptrSize, code.Idx/uintptrSize,
code.ElemIdx/uintptrSize,
code.Length/uintptrSize,
code.MapIter/uintptrSize,
) )
} }
func (c *Opcode) dumpMapEnd(code *Opcode) string { func (c *Opcode) dumpMapEnd(code *Opcode) string {
return fmt.Sprintf( return fmt.Sprintf(
`[%03d]%s%s ([idx:%d][mapPos:%d][length:%d])`, `[%03d]%s%s ([idx:%d])`,
code.DisplayIdx, code.DisplayIdx,
strings.Repeat("-", int(code.Indent)), strings.Repeat("-", int(code.Indent)),
code.Op, code.Op,
code.Idx/uintptrSize, code.Idx/uintptrSize,
code.MapPos/uintptrSize,
code.Length/uintptrSize,
) )
} }
@ -504,25 +493,21 @@ func (c *Opcode) dumpField(code *Opcode) string {
func (c *Opcode) dumpKey(code *Opcode) string { func (c *Opcode) dumpKey(code *Opcode) string {
return fmt.Sprintf( return fmt.Sprintf(
`[%03d]%s%s ([idx:%d][elemIdx:%d][length:%d][mapIter:%d])`, `[%03d]%s%s ([idx:%d])`,
code.DisplayIdx, code.DisplayIdx,
strings.Repeat("-", int(code.Indent)), strings.Repeat("-", int(code.Indent)),
code.Op, code.Op,
code.Idx/uintptrSize, code.Idx/uintptrSize,
code.ElemIdx/uintptrSize,
code.Length/uintptrSize,
code.MapIter/uintptrSize,
) )
} }
func (c *Opcode) dumpValue(code *Opcode) string { func (c *Opcode) dumpValue(code *Opcode) string {
return fmt.Sprintf( return fmt.Sprintf(
`[%03d]%s%s ([idx:%d][mapIter:%d])`, `[%03d]%s%s ([idx:%d])`,
code.DisplayIdx, code.DisplayIdx,
strings.Repeat("-", int(code.Indent)), strings.Repeat("-", int(code.Indent)),
code.Op, code.Op,
code.Idx/uintptrSize, code.Idx/uintptrSize,
code.MapIter/uintptrSize,
) )
} }
@ -629,19 +614,11 @@ func newArrayElemCode(ctx *compileContext, typ *runtime.Type, head *Opcode, leng
func newMapHeaderCode(ctx *compileContext, typ *runtime.Type) *Opcode { func newMapHeaderCode(ctx *compileContext, typ *runtime.Type) *Opcode {
idx := opcodeOffset(ctx.ptrIndex) idx := opcodeOffset(ctx.ptrIndex)
ctx.incPtrIndex() ctx.incPtrIndex()
elemIdx := opcodeOffset(ctx.ptrIndex)
ctx.incPtrIndex()
length := opcodeOffset(ctx.ptrIndex)
ctx.incPtrIndex()
mapIter := opcodeOffset(ctx.ptrIndex)
return &Opcode{ return &Opcode{
Op: OpMap, Op: OpMap,
Type: typ, Type: typ,
Idx: idx, Idx: idx,
DisplayIdx: ctx.opcodeIndex, DisplayIdx: ctx.opcodeIndex,
ElemIdx: elemIdx,
Length: length,
MapIter: mapIter,
Indent: ctx.indent, Indent: ctx.indent,
} }
} }
@ -650,11 +627,8 @@ func newMapKeyCode(ctx *compileContext, typ *runtime.Type, head *Opcode) *Opcode
return &Opcode{ return &Opcode{
Op: OpMapKey, Op: OpMapKey,
Type: typ, Type: typ,
Idx: opcodeOffset(ctx.ptrIndex), Idx: head.Idx,
DisplayIdx: ctx.opcodeIndex, DisplayIdx: ctx.opcodeIndex,
ElemIdx: head.ElemIdx,
Length: head.Length,
MapIter: head.MapIter,
Indent: ctx.indent, Indent: ctx.indent,
} }
} }
@ -663,28 +637,20 @@ func newMapValueCode(ctx *compileContext, typ *runtime.Type, head *Opcode) *Opco
return &Opcode{ return &Opcode{
Op: OpMapValue, Op: OpMapValue,
Type: typ, Type: typ,
Idx: opcodeOffset(ctx.ptrIndex), Idx: head.Idx,
DisplayIdx: ctx.opcodeIndex, DisplayIdx: ctx.opcodeIndex,
ElemIdx: head.ElemIdx,
Length: head.Length,
MapIter: head.MapIter,
Indent: ctx.indent, Indent: ctx.indent,
} }
} }
func newMapEndCode(ctx *compileContext, typ *runtime.Type, head *Opcode) *Opcode { func newMapEndCode(ctx *compileContext, typ *runtime.Type, head *Opcode) *Opcode {
mapPos := opcodeOffset(ctx.ptrIndex)
ctx.incPtrIndex()
idx := opcodeOffset(ctx.ptrIndex)
return &Opcode{ return &Opcode{
Op: OpMapEnd, Op: OpMapEnd,
Type: typ, Type: typ,
Idx: idx, Idx: head.Idx,
Next: newEndOp(ctx, typ),
DisplayIdx: ctx.opcodeIndex, DisplayIdx: ctx.opcodeIndex,
Length: head.Length,
MapPos: mapPos,
Indent: ctx.indent, Indent: ctx.indent,
Next: newEndOp(ctx, typ),
} }
} }

View File

@ -403,48 +403,41 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
break break
} }
b = appendStructHead(ctx, b) b = appendStructHead(ctx, b)
iter := mapiterinit(code.Type, uptr) mapCtx := encoder.NewMapContext(mlen)
ctx.KeepRefs = append(ctx.KeepRefs, iter) mapiterinit(code.Type, uptr, &mapCtx.Iter)
store(ctxptr, code.ElemIdx, 0) store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
store(ctxptr, code.Length, uintptr(mlen)) ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
store(ctxptr, code.MapIter, uintptr(iter))
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
b = appendMapKeyIndent(ctx, code.Next, b) b = appendMapKeyIndent(ctx, code.Next, b)
} else { } else {
mapCtx := encoder.NewMapContext(mlen) mapCtx.Start = len(b)
mapCtx.Pos = append(mapCtx.Pos, len(b)) mapCtx.First = len(b)
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
store(ctxptr, code.End.MapPos, uintptr(unsafe.Pointer(mapCtx)))
} }
key := mapiterkey(iter) key := mapiterkey(&mapCtx.Iter)
store(ctxptr, code.Next.Idx, uintptr(key)) store(ctxptr, code.Next.Idx, uintptr(key))
code = code.Next code = code.Next
case encoder.OpMapKey: case encoder.OpMapKey:
idx := load(ctxptr, code.ElemIdx) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
length := load(ctxptr, code.Length) idx := mapCtx.Idx
idx++ idx++
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
if idx < length { if idx < mapCtx.Len {
b = appendMapKeyIndent(ctx, code, b) b = appendMapKeyIndent(ctx, code, b)
store(ctxptr, code.ElemIdx, idx) mapCtx.Idx = int(idx)
ptr := load(ctxptr, code.MapIter) key := mapiterkey(&mapCtx.Iter)
iter := ptrToUnsafePtr(ptr)
key := mapiterkey(iter)
store(ctxptr, code.Next.Idx, uintptr(key)) store(ctxptr, code.Next.Idx, uintptr(key))
code = code.Next code = code.Next
} else { } else {
b = appendObjectEnd(ctx, code, b) b = appendObjectEnd(ctx, code, b)
encoder.ReleaseMapContext(mapCtx)
code = code.End.Next code = code.End.Next
} }
} else { } else {
ptr := load(ctxptr, code.End.MapPos) mapCtx.Slice.Items[mapCtx.Idx].Value = b[mapCtx.Start:len(b)]
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) if idx < mapCtx.Len {
mapCtx.Pos = append(mapCtx.Pos, len(b)) mapCtx.Idx = int(idx)
if idx < length { mapCtx.Start = len(b)
ptr := load(ctxptr, code.MapIter) key := mapiterkey(&mapCtx.Iter)
iter := ptrToUnsafePtr(ptr)
store(ctxptr, code.ElemIdx, idx)
key := mapiterkey(iter)
store(ctxptr, code.Next.Idx, uintptr(key)) store(ctxptr, code.Next.Idx, uintptr(key))
code = code.Next code = code.Next
} else { } else {
@ -452,46 +445,27 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
} }
case encoder.OpMapValue: case encoder.OpMapValue:
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
b = appendColon(ctx, b) b = appendColon(ctx, b)
} else { } else {
ptr := load(ctxptr, code.End.MapPos) mapCtx.Slice.Items[mapCtx.Idx].Key = b[mapCtx.Start:len(b)]
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) mapCtx.Start = len(b)
mapCtx.Pos = append(mapCtx.Pos, len(b))
} }
ptr := load(ctxptr, code.MapIter) value := mapitervalue(&mapCtx.Iter)
iter := ptrToUnsafePtr(ptr)
value := mapitervalue(iter)
store(ctxptr, code.Next.Idx, uintptr(value)) store(ctxptr, code.Next.Idx, uintptr(value))
mapiternext(iter) mapiternext(&mapCtx.Iter)
code = code.Next code = code.Next
case encoder.OpMapEnd: case encoder.OpMapEnd:
// this operation only used by sorted map. // this operation only used by sorted map.
length := int(load(ctxptr, code.Length)) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
ptr := load(ctxptr, code.MapPos)
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr))
pos := mapCtx.Pos
for i := 0; i < length; i++ {
startKey := pos[i*2]
startValue := pos[i*2+1]
var endValue int
if i+1 < length {
endValue = pos[i*2+2]
} else {
endValue = len(b)
}
mapCtx.Slice.Items = append(mapCtx.Slice.Items, encoder.MapItem{
Key: b[startKey:startValue],
Value: b[startValue:endValue],
})
}
sort.Sort(mapCtx.Slice) sort.Sort(mapCtx.Slice)
buf := mapCtx.Buf buf := mapCtx.Buf
for _, item := range mapCtx.Slice.Items { for _, item := range mapCtx.Slice.Items {
buf = appendMapKeyValue(ctx, code, buf, item.Key, item.Value) buf = appendMapKeyValue(ctx, code, buf, item.Key, item.Value)
} }
buf = appendMapEnd(ctx, code, buf) buf = appendMapEnd(ctx, code, buf)
b = b[:pos[0]] b = b[:mapCtx.First]
b = append(b, buf...) b = append(b, buf...)
mapCtx.Buf = buf mapCtx.Buf = buf
encoder.ReleaseMapContext(mapCtx) encoder.ReleaseMapContext(mapCtx)

View File

@ -403,48 +403,41 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
break break
} }
b = appendStructHead(ctx, b) b = appendStructHead(ctx, b)
iter := mapiterinit(code.Type, uptr) mapCtx := encoder.NewMapContext(mlen)
ctx.KeepRefs = append(ctx.KeepRefs, iter) mapiterinit(code.Type, uptr, &mapCtx.Iter)
store(ctxptr, code.ElemIdx, 0) store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
store(ctxptr, code.Length, uintptr(mlen)) ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
store(ctxptr, code.MapIter, uintptr(iter))
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
b = appendMapKeyIndent(ctx, code.Next, b) b = appendMapKeyIndent(ctx, code.Next, b)
} else { } else {
mapCtx := encoder.NewMapContext(mlen) mapCtx.Start = len(b)
mapCtx.Pos = append(mapCtx.Pos, len(b)) mapCtx.First = len(b)
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
store(ctxptr, code.End.MapPos, uintptr(unsafe.Pointer(mapCtx)))
} }
key := mapiterkey(iter) key := mapiterkey(&mapCtx.Iter)
store(ctxptr, code.Next.Idx, uintptr(key)) store(ctxptr, code.Next.Idx, uintptr(key))
code = code.Next code = code.Next
case encoder.OpMapKey: case encoder.OpMapKey:
idx := load(ctxptr, code.ElemIdx) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
length := load(ctxptr, code.Length) idx := mapCtx.Idx
idx++ idx++
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
if idx < length { if idx < mapCtx.Len {
b = appendMapKeyIndent(ctx, code, b) b = appendMapKeyIndent(ctx, code, b)
store(ctxptr, code.ElemIdx, idx) mapCtx.Idx = int(idx)
ptr := load(ctxptr, code.MapIter) key := mapiterkey(&mapCtx.Iter)
iter := ptrToUnsafePtr(ptr)
key := mapiterkey(iter)
store(ctxptr, code.Next.Idx, uintptr(key)) store(ctxptr, code.Next.Idx, uintptr(key))
code = code.Next code = code.Next
} else { } else {
b = appendObjectEnd(ctx, code, b) b = appendObjectEnd(ctx, code, b)
encoder.ReleaseMapContext(mapCtx)
code = code.End.Next code = code.End.Next
} }
} else { } else {
ptr := load(ctxptr, code.End.MapPos) mapCtx.Slice.Items[mapCtx.Idx].Value = b[mapCtx.Start:len(b)]
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) if idx < mapCtx.Len {
mapCtx.Pos = append(mapCtx.Pos, len(b)) mapCtx.Idx = int(idx)
if idx < length { mapCtx.Start = len(b)
ptr := load(ctxptr, code.MapIter) key := mapiterkey(&mapCtx.Iter)
iter := ptrToUnsafePtr(ptr)
store(ctxptr, code.ElemIdx, idx)
key := mapiterkey(iter)
store(ctxptr, code.Next.Idx, uintptr(key)) store(ctxptr, code.Next.Idx, uintptr(key))
code = code.Next code = code.Next
} else { } else {
@ -452,46 +445,27 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
} }
case encoder.OpMapValue: case encoder.OpMapValue:
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
b = appendColon(ctx, b) b = appendColon(ctx, b)
} else { } else {
ptr := load(ctxptr, code.End.MapPos) mapCtx.Slice.Items[mapCtx.Idx].Key = b[mapCtx.Start:len(b)]
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) mapCtx.Start = len(b)
mapCtx.Pos = append(mapCtx.Pos, len(b))
} }
ptr := load(ctxptr, code.MapIter) value := mapitervalue(&mapCtx.Iter)
iter := ptrToUnsafePtr(ptr)
value := mapitervalue(iter)
store(ctxptr, code.Next.Idx, uintptr(value)) store(ctxptr, code.Next.Idx, uintptr(value))
mapiternext(iter) mapiternext(&mapCtx.Iter)
code = code.Next code = code.Next
case encoder.OpMapEnd: case encoder.OpMapEnd:
// this operation only used by sorted map. // this operation only used by sorted map.
length := int(load(ctxptr, code.Length)) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
ptr := load(ctxptr, code.MapPos)
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr))
pos := mapCtx.Pos
for i := 0; i < length; i++ {
startKey := pos[i*2]
startValue := pos[i*2+1]
var endValue int
if i+1 < length {
endValue = pos[i*2+2]
} else {
endValue = len(b)
}
mapCtx.Slice.Items = append(mapCtx.Slice.Items, encoder.MapItem{
Key: b[startKey:startValue],
Value: b[startValue:endValue],
})
}
sort.Sort(mapCtx.Slice) sort.Sort(mapCtx.Slice)
buf := mapCtx.Buf buf := mapCtx.Buf
for _, item := range mapCtx.Slice.Items { for _, item := range mapCtx.Slice.Items {
buf = appendMapKeyValue(ctx, code, buf, item.Key, item.Value) buf = appendMapKeyValue(ctx, code, buf, item.Key, item.Value)
} }
buf = appendMapEnd(ctx, code, buf) buf = appendMapEnd(ctx, code, buf)
b = b[:pos[0]] b = b[:mapCtx.First]
b = append(b, buf...) b = append(b, buf...)
mapCtx.Buf = buf mapCtx.Buf = buf
encoder.ReleaseMapContext(mapCtx) encoder.ReleaseMapContext(mapCtx)

View File

@ -403,48 +403,41 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
break break
} }
b = appendStructHead(ctx, b) b = appendStructHead(ctx, b)
iter := mapiterinit(code.Type, uptr) mapCtx := encoder.NewMapContext(mlen)
ctx.KeepRefs = append(ctx.KeepRefs, iter) mapiterinit(code.Type, uptr, &mapCtx.Iter)
store(ctxptr, code.ElemIdx, 0) store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
store(ctxptr, code.Length, uintptr(mlen)) ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
store(ctxptr, code.MapIter, uintptr(iter))
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
b = appendMapKeyIndent(ctx, code.Next, b) b = appendMapKeyIndent(ctx, code.Next, b)
} else { } else {
mapCtx := encoder.NewMapContext(mlen) mapCtx.Start = len(b)
mapCtx.Pos = append(mapCtx.Pos, len(b)) mapCtx.First = len(b)
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
store(ctxptr, code.End.MapPos, uintptr(unsafe.Pointer(mapCtx)))
} }
key := mapiterkey(iter) key := mapiterkey(&mapCtx.Iter)
store(ctxptr, code.Next.Idx, uintptr(key)) store(ctxptr, code.Next.Idx, uintptr(key))
code = code.Next code = code.Next
case encoder.OpMapKey: case encoder.OpMapKey:
idx := load(ctxptr, code.ElemIdx) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
length := load(ctxptr, code.Length) idx := mapCtx.Idx
idx++ idx++
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
if idx < length { if idx < mapCtx.Len {
b = appendMapKeyIndent(ctx, code, b) b = appendMapKeyIndent(ctx, code, b)
store(ctxptr, code.ElemIdx, idx) mapCtx.Idx = int(idx)
ptr := load(ctxptr, code.MapIter) key := mapiterkey(&mapCtx.Iter)
iter := ptrToUnsafePtr(ptr)
key := mapiterkey(iter)
store(ctxptr, code.Next.Idx, uintptr(key)) store(ctxptr, code.Next.Idx, uintptr(key))
code = code.Next code = code.Next
} else { } else {
b = appendObjectEnd(ctx, code, b) b = appendObjectEnd(ctx, code, b)
encoder.ReleaseMapContext(mapCtx)
code = code.End.Next code = code.End.Next
} }
} else { } else {
ptr := load(ctxptr, code.End.MapPos) mapCtx.Slice.Items[mapCtx.Idx].Value = b[mapCtx.Start:len(b)]
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) if idx < mapCtx.Len {
mapCtx.Pos = append(mapCtx.Pos, len(b)) mapCtx.Idx = int(idx)
if idx < length { mapCtx.Start = len(b)
ptr := load(ctxptr, code.MapIter) key := mapiterkey(&mapCtx.Iter)
iter := ptrToUnsafePtr(ptr)
store(ctxptr, code.ElemIdx, idx)
key := mapiterkey(iter)
store(ctxptr, code.Next.Idx, uintptr(key)) store(ctxptr, code.Next.Idx, uintptr(key))
code = code.Next code = code.Next
} else { } else {
@ -452,46 +445,27 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
} }
case encoder.OpMapValue: case encoder.OpMapValue:
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
b = appendColon(ctx, b) b = appendColon(ctx, b)
} else { } else {
ptr := load(ctxptr, code.End.MapPos) mapCtx.Slice.Items[mapCtx.Idx].Key = b[mapCtx.Start:len(b)]
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) mapCtx.Start = len(b)
mapCtx.Pos = append(mapCtx.Pos, len(b))
} }
ptr := load(ctxptr, code.MapIter) value := mapitervalue(&mapCtx.Iter)
iter := ptrToUnsafePtr(ptr)
value := mapitervalue(iter)
store(ctxptr, code.Next.Idx, uintptr(value)) store(ctxptr, code.Next.Idx, uintptr(value))
mapiternext(iter) mapiternext(&mapCtx.Iter)
code = code.Next code = code.Next
case encoder.OpMapEnd: case encoder.OpMapEnd:
// this operation only used by sorted map. // this operation only used by sorted map.
length := int(load(ctxptr, code.Length)) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
ptr := load(ctxptr, code.MapPos)
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr))
pos := mapCtx.Pos
for i := 0; i < length; i++ {
startKey := pos[i*2]
startValue := pos[i*2+1]
var endValue int
if i+1 < length {
endValue = pos[i*2+2]
} else {
endValue = len(b)
}
mapCtx.Slice.Items = append(mapCtx.Slice.Items, encoder.MapItem{
Key: b[startKey:startValue],
Value: b[startValue:endValue],
})
}
sort.Sort(mapCtx.Slice) sort.Sort(mapCtx.Slice)
buf := mapCtx.Buf buf := mapCtx.Buf
for _, item := range mapCtx.Slice.Items { for _, item := range mapCtx.Slice.Items {
buf = appendMapKeyValue(ctx, code, buf, item.Key, item.Value) buf = appendMapKeyValue(ctx, code, buf, item.Key, item.Value)
} }
buf = appendMapEnd(ctx, code, buf) buf = appendMapEnd(ctx, code, buf)
b = b[:pos[0]] b = b[:mapCtx.First]
b = append(b, buf...) b = append(b, buf...)
mapCtx.Buf = buf mapCtx.Buf = buf
encoder.ReleaseMapContext(mapCtx) encoder.ReleaseMapContext(mapCtx)

View File

@ -403,48 +403,41 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
break break
} }
b = appendStructHead(ctx, b) b = appendStructHead(ctx, b)
iter := mapiterinit(code.Type, uptr) mapCtx := encoder.NewMapContext(mlen)
ctx.KeepRefs = append(ctx.KeepRefs, iter) mapiterinit(code.Type, uptr, &mapCtx.Iter)
store(ctxptr, code.ElemIdx, 0) store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
store(ctxptr, code.Length, uintptr(mlen)) ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
store(ctxptr, code.MapIter, uintptr(iter))
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
b = appendMapKeyIndent(ctx, code.Next, b) b = appendMapKeyIndent(ctx, code.Next, b)
} else { } else {
mapCtx := encoder.NewMapContext(mlen) mapCtx.Start = len(b)
mapCtx.Pos = append(mapCtx.Pos, len(b)) mapCtx.First = len(b)
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
store(ctxptr, code.End.MapPos, uintptr(unsafe.Pointer(mapCtx)))
} }
key := mapiterkey(iter) key := mapiterkey(&mapCtx.Iter)
store(ctxptr, code.Next.Idx, uintptr(key)) store(ctxptr, code.Next.Idx, uintptr(key))
code = code.Next code = code.Next
case encoder.OpMapKey: case encoder.OpMapKey:
idx := load(ctxptr, code.ElemIdx) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
length := load(ctxptr, code.Length) idx := mapCtx.Idx
idx++ idx++
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
if idx < length { if idx < mapCtx.Len {
b = appendMapKeyIndent(ctx, code, b) b = appendMapKeyIndent(ctx, code, b)
store(ctxptr, code.ElemIdx, idx) mapCtx.Idx = int(idx)
ptr := load(ctxptr, code.MapIter) key := mapiterkey(&mapCtx.Iter)
iter := ptrToUnsafePtr(ptr)
key := mapiterkey(iter)
store(ctxptr, code.Next.Idx, uintptr(key)) store(ctxptr, code.Next.Idx, uintptr(key))
code = code.Next code = code.Next
} else { } else {
b = appendObjectEnd(ctx, code, b) b = appendObjectEnd(ctx, code, b)
encoder.ReleaseMapContext(mapCtx)
code = code.End.Next code = code.End.Next
} }
} else { } else {
ptr := load(ctxptr, code.End.MapPos) mapCtx.Slice.Items[mapCtx.Idx].Value = b[mapCtx.Start:len(b)]
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) if idx < mapCtx.Len {
mapCtx.Pos = append(mapCtx.Pos, len(b)) mapCtx.Idx = int(idx)
if idx < length { mapCtx.Start = len(b)
ptr := load(ctxptr, code.MapIter) key := mapiterkey(&mapCtx.Iter)
iter := ptrToUnsafePtr(ptr)
store(ctxptr, code.ElemIdx, idx)
key := mapiterkey(iter)
store(ctxptr, code.Next.Idx, uintptr(key)) store(ctxptr, code.Next.Idx, uintptr(key))
code = code.Next code = code.Next
} else { } else {
@ -452,46 +445,27 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
} }
case encoder.OpMapValue: case encoder.OpMapValue:
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
b = appendColon(ctx, b) b = appendColon(ctx, b)
} else { } else {
ptr := load(ctxptr, code.End.MapPos) mapCtx.Slice.Items[mapCtx.Idx].Key = b[mapCtx.Start:len(b)]
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) mapCtx.Start = len(b)
mapCtx.Pos = append(mapCtx.Pos, len(b))
} }
ptr := load(ctxptr, code.MapIter) value := mapitervalue(&mapCtx.Iter)
iter := ptrToUnsafePtr(ptr)
value := mapitervalue(iter)
store(ctxptr, code.Next.Idx, uintptr(value)) store(ctxptr, code.Next.Idx, uintptr(value))
mapiternext(iter) mapiternext(&mapCtx.Iter)
code = code.Next code = code.Next
case encoder.OpMapEnd: case encoder.OpMapEnd:
// this operation only used by sorted map. // this operation only used by sorted map.
length := int(load(ctxptr, code.Length)) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
ptr := load(ctxptr, code.MapPos)
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr))
pos := mapCtx.Pos
for i := 0; i < length; i++ {
startKey := pos[i*2]
startValue := pos[i*2+1]
var endValue int
if i+1 < length {
endValue = pos[i*2+2]
} else {
endValue = len(b)
}
mapCtx.Slice.Items = append(mapCtx.Slice.Items, encoder.MapItem{
Key: b[startKey:startValue],
Value: b[startValue:endValue],
})
}
sort.Sort(mapCtx.Slice) sort.Sort(mapCtx.Slice)
buf := mapCtx.Buf buf := mapCtx.Buf
for _, item := range mapCtx.Slice.Items { for _, item := range mapCtx.Slice.Items {
buf = appendMapKeyValue(ctx, code, buf, item.Key, item.Value) buf = appendMapKeyValue(ctx, code, buf, item.Key, item.Value)
} }
buf = appendMapEnd(ctx, code, buf) buf = appendMapEnd(ctx, code, buf)
b = b[:pos[0]] b = b[:mapCtx.First]
b = append(b, buf...) b = append(b, buf...)
mapCtx.Buf = buf mapCtx.Buf = buf
encoder.ReleaseMapContext(mapCtx) encoder.ReleaseMapContext(mapCtx)

View File

@ -11,8 +11,8 @@ func TestOpcodeSize(t *testing.T) {
const uintptrSize = 4 << (^uintptr(0) >> 63) const uintptrSize = 4 << (^uintptr(0) >> 63)
if uintptrSize == 8 { if uintptrSize == 8 {
size := unsafe.Sizeof(encoder.Opcode{}) size := unsafe.Sizeof(encoder.Opcode{})
if size != 120 { if size != 112 {
t.Fatalf("unexpected opcode size: expected 120bytes but got %dbytes", size) t.Fatalf("unexpected opcode size: expected 112bytes but got %dbytes", size)
} }
} }
} }