forked from mirror/go-json
Refactor opcode fields
This commit is contained in:
parent
c220d90e4c
commit
5418c49bcf
|
@ -405,7 +405,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
b = appendStructHead(ctx, b)
|
||||
mapCtx := encoder.NewMapContext(mlen)
|
||||
mapiterinit(code.Type, uptr, &mapCtx.Iter)
|
||||
store(ctxptr, code.MapIter, uintptr(unsafe.Pointer(mapCtx)))
|
||||
store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
|
||||
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
|
||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
||||
b = appendMapKeyIndent(ctx, code.Next, b)
|
||||
|
@ -417,7 +417,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
store(ctxptr, code.Next.Idx, uintptr(key))
|
||||
code = code.Next
|
||||
case encoder.OpMapKey:
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.MapIter)))
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
|
||||
idx := mapCtx.Idx
|
||||
idx++
|
||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
||||
|
@ -445,7 +445,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
}
|
||||
}
|
||||
case encoder.OpMapValue:
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.MapIter)))
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
|
||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
||||
b = appendColon(ctx, b)
|
||||
} else {
|
||||
|
@ -458,7 +458,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
code = code.Next
|
||||
case encoder.OpMapEnd:
|
||||
// this operation only used by sorted map.
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.MapIter)))
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
|
||||
sort.Sort(mapCtx.Slice)
|
||||
buf := mapCtx.Buf
|
||||
for _, item := range mapCtx.Slice.Items {
|
||||
|
|
|
@ -39,10 +39,8 @@ type Opcode struct {
|
|||
|
||||
Type *runtime.Type // go type
|
||||
Jmp *CompiledCode // for recursive call
|
||||
ElemIdx uint32 // offset to access array/slice/map elem
|
||||
Length uint32 // offset to access slice/map length or array length
|
||||
MapIter uint32 // offset to access map iterator
|
||||
MapPos uint32 // offset to access position list for sorted map
|
||||
ElemIdx uint32 // offset to access array/slice elem
|
||||
Length uint32 // offset to access slice length or array length
|
||||
Indent uint32 // indent number
|
||||
Size uint32 // array/slice elem size
|
||||
DisplayIdx uint32 // opcode index
|
||||
|
@ -91,8 +89,6 @@ func (c *Opcode) MaxIdx() uint32 {
|
|||
c.Idx,
|
||||
c.ElemIdx,
|
||||
c.Length,
|
||||
c.MapIter,
|
||||
c.MapPos,
|
||||
c.Size,
|
||||
} {
|
||||
if max < value {
|
||||
|
@ -341,8 +337,6 @@ func copyOpcode(code *Opcode) *Opcode {
|
|||
DisplayKey: c.DisplayKey,
|
||||
ElemIdx: c.ElemIdx,
|
||||
Length: c.Length,
|
||||
MapIter: c.MapIter,
|
||||
MapPos: c.MapPos,
|
||||
Size: c.Size,
|
||||
Indent: c.Indent,
|
||||
Jmp: c.Jmp,
|
||||
|
@ -448,26 +442,21 @@ func (c *Opcode) dumpHead(code *Opcode) string {
|
|||
|
||||
func (c *Opcode) dumpMapHead(code *Opcode) string {
|
||||
return fmt.Sprintf(
|
||||
`[%03d]%s%s ([idx:%d][elemIdx:%d][length:%d][mapIter:%d])`,
|
||||
`[%03d]%s%s ([idx:%d])`,
|
||||
code.DisplayIdx,
|
||||
strings.Repeat("-", int(code.Indent)),
|
||||
code.Op,
|
||||
code.Idx/uintptrSize,
|
||||
code.ElemIdx/uintptrSize,
|
||||
code.Length/uintptrSize,
|
||||
code.MapIter/uintptrSize,
|
||||
)
|
||||
}
|
||||
|
||||
func (c *Opcode) dumpMapEnd(code *Opcode) string {
|
||||
return fmt.Sprintf(
|
||||
`[%03d]%s%s ([idx:%d][mapPos:%d][length:%d])`,
|
||||
`[%03d]%s%s ([idx:%d])`,
|
||||
code.DisplayIdx,
|
||||
strings.Repeat("-", int(code.Indent)),
|
||||
code.Op,
|
||||
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 {
|
||||
return fmt.Sprintf(
|
||||
`[%03d]%s%s ([idx:%d][elemIdx:%d][length:%d][mapIter:%d])`,
|
||||
`[%03d]%s%s ([idx:%d])`,
|
||||
code.DisplayIdx,
|
||||
strings.Repeat("-", int(code.Indent)),
|
||||
code.Op,
|
||||
code.Idx/uintptrSize,
|
||||
code.ElemIdx/uintptrSize,
|
||||
code.Length/uintptrSize,
|
||||
code.MapIter/uintptrSize,
|
||||
)
|
||||
}
|
||||
|
||||
func (c *Opcode) dumpValue(code *Opcode) string {
|
||||
return fmt.Sprintf(
|
||||
`[%03d]%s%s ([idx:%d][mapIter:%d])`,
|
||||
`[%03d]%s%s ([idx:%d])`,
|
||||
code.DisplayIdx,
|
||||
strings.Repeat("-", int(code.Indent)),
|
||||
code.Op,
|
||||
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 {
|
||||
idx := opcodeOffset(ctx.ptrIndex)
|
||||
ctx.incPtrIndex()
|
||||
elemIdx := opcodeOffset(ctx.ptrIndex)
|
||||
ctx.incPtrIndex()
|
||||
length := opcodeOffset(ctx.ptrIndex)
|
||||
ctx.incPtrIndex()
|
||||
mapIter := opcodeOffset(ctx.ptrIndex)
|
||||
return &Opcode{
|
||||
Op: OpMap,
|
||||
Type: typ,
|
||||
Idx: idx,
|
||||
DisplayIdx: ctx.opcodeIndex,
|
||||
ElemIdx: elemIdx,
|
||||
Length: length,
|
||||
MapIter: mapIter,
|
||||
Indent: ctx.indent,
|
||||
}
|
||||
}
|
||||
|
@ -650,11 +627,8 @@ func newMapKeyCode(ctx *compileContext, typ *runtime.Type, head *Opcode) *Opcode
|
|||
return &Opcode{
|
||||
Op: OpMapKey,
|
||||
Type: typ,
|
||||
Idx: opcodeOffset(ctx.ptrIndex),
|
||||
Idx: head.Idx,
|
||||
DisplayIdx: ctx.opcodeIndex,
|
||||
ElemIdx: head.ElemIdx,
|
||||
Length: head.Length,
|
||||
MapIter: head.MapIter,
|
||||
Indent: ctx.indent,
|
||||
}
|
||||
}
|
||||
|
@ -663,29 +637,20 @@ func newMapValueCode(ctx *compileContext, typ *runtime.Type, head *Opcode) *Opco
|
|||
return &Opcode{
|
||||
Op: OpMapValue,
|
||||
Type: typ,
|
||||
Idx: opcodeOffset(ctx.ptrIndex),
|
||||
Idx: head.Idx,
|
||||
DisplayIdx: ctx.opcodeIndex,
|
||||
ElemIdx: head.ElemIdx,
|
||||
Length: head.Length,
|
||||
MapIter: head.MapIter,
|
||||
Indent: ctx.indent,
|
||||
}
|
||||
}
|
||||
|
||||
func newMapEndCode(ctx *compileContext, typ *runtime.Type, head *Opcode) *Opcode {
|
||||
mapPos := opcodeOffset(ctx.ptrIndex)
|
||||
ctx.incPtrIndex()
|
||||
idx := opcodeOffset(ctx.ptrIndex)
|
||||
return &Opcode{
|
||||
Op: OpMapEnd,
|
||||
Type: typ,
|
||||
Idx: idx,
|
||||
Next: newEndOp(ctx, typ),
|
||||
Idx: head.Idx,
|
||||
DisplayIdx: ctx.opcodeIndex,
|
||||
Length: head.Length,
|
||||
MapPos: mapPos,
|
||||
Indent: ctx.indent,
|
||||
MapIter: head.MapIter,
|
||||
Next: newEndOp(ctx, typ),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -405,7 +405,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
b = appendStructHead(ctx, b)
|
||||
mapCtx := encoder.NewMapContext(mlen)
|
||||
mapiterinit(code.Type, uptr, &mapCtx.Iter)
|
||||
store(ctxptr, code.MapIter, uintptr(unsafe.Pointer(mapCtx)))
|
||||
store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
|
||||
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
|
||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
||||
b = appendMapKeyIndent(ctx, code.Next, b)
|
||||
|
@ -417,7 +417,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
store(ctxptr, code.Next.Idx, uintptr(key))
|
||||
code = code.Next
|
||||
case encoder.OpMapKey:
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.MapIter)))
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
|
||||
idx := mapCtx.Idx
|
||||
idx++
|
||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
||||
|
@ -445,7 +445,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
}
|
||||
}
|
||||
case encoder.OpMapValue:
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.MapIter)))
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
|
||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
||||
b = appendColon(ctx, b)
|
||||
} else {
|
||||
|
@ -458,7 +458,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
code = code.Next
|
||||
case encoder.OpMapEnd:
|
||||
// this operation only used by sorted map.
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.MapIter)))
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
|
||||
sort.Sort(mapCtx.Slice)
|
||||
buf := mapCtx.Buf
|
||||
for _, item := range mapCtx.Slice.Items {
|
||||
|
|
|
@ -405,7 +405,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
b = appendStructHead(ctx, b)
|
||||
mapCtx := encoder.NewMapContext(mlen)
|
||||
mapiterinit(code.Type, uptr, &mapCtx.Iter)
|
||||
store(ctxptr, code.MapIter, uintptr(unsafe.Pointer(mapCtx)))
|
||||
store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
|
||||
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
|
||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
||||
b = appendMapKeyIndent(ctx, code.Next, b)
|
||||
|
@ -417,7 +417,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
store(ctxptr, code.Next.Idx, uintptr(key))
|
||||
code = code.Next
|
||||
case encoder.OpMapKey:
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.MapIter)))
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
|
||||
idx := mapCtx.Idx
|
||||
idx++
|
||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
||||
|
@ -445,7 +445,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
}
|
||||
}
|
||||
case encoder.OpMapValue:
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.MapIter)))
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
|
||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
||||
b = appendColon(ctx, b)
|
||||
} else {
|
||||
|
@ -458,7 +458,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
code = code.Next
|
||||
case encoder.OpMapEnd:
|
||||
// this operation only used by sorted map.
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.MapIter)))
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
|
||||
sort.Sort(mapCtx.Slice)
|
||||
buf := mapCtx.Buf
|
||||
for _, item := range mapCtx.Slice.Items {
|
||||
|
|
|
@ -405,7 +405,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
b = appendStructHead(ctx, b)
|
||||
mapCtx := encoder.NewMapContext(mlen)
|
||||
mapiterinit(code.Type, uptr, &mapCtx.Iter)
|
||||
store(ctxptr, code.MapIter, uintptr(unsafe.Pointer(mapCtx)))
|
||||
store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
|
||||
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
|
||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
||||
b = appendMapKeyIndent(ctx, code.Next, b)
|
||||
|
@ -417,7 +417,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
store(ctxptr, code.Next.Idx, uintptr(key))
|
||||
code = code.Next
|
||||
case encoder.OpMapKey:
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.MapIter)))
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
|
||||
idx := mapCtx.Idx
|
||||
idx++
|
||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
||||
|
@ -445,7 +445,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
}
|
||||
}
|
||||
case encoder.OpMapValue:
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.MapIter)))
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
|
||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
||||
b = appendColon(ctx, b)
|
||||
} else {
|
||||
|
@ -458,7 +458,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
code = code.Next
|
||||
case encoder.OpMapEnd:
|
||||
// this operation only used by sorted map.
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.MapIter)))
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
|
||||
sort.Sort(mapCtx.Slice)
|
||||
buf := mapCtx.Buf
|
||||
for _, item := range mapCtx.Slice.Items {
|
||||
|
|
|
@ -405,7 +405,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
b = appendStructHead(ctx, b)
|
||||
mapCtx := encoder.NewMapContext(mlen)
|
||||
mapiterinit(code.Type, uptr, &mapCtx.Iter)
|
||||
store(ctxptr, code.MapIter, uintptr(unsafe.Pointer(mapCtx)))
|
||||
store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
|
||||
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
|
||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
||||
b = appendMapKeyIndent(ctx, code.Next, b)
|
||||
|
@ -417,7 +417,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
store(ctxptr, code.Next.Idx, uintptr(key))
|
||||
code = code.Next
|
||||
case encoder.OpMapKey:
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.MapIter)))
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
|
||||
idx := mapCtx.Idx
|
||||
idx++
|
||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
||||
|
@ -445,7 +445,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
}
|
||||
}
|
||||
case encoder.OpMapValue:
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.MapIter)))
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
|
||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
||||
b = appendColon(ctx, b)
|
||||
} else {
|
||||
|
@ -458,7 +458,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
code = code.Next
|
||||
case encoder.OpMapEnd:
|
||||
// this operation only used by sorted map.
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.MapIter)))
|
||||
mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(load(ctxptr, code.Idx)))
|
||||
sort.Sort(mapCtx.Slice)
|
||||
buf := mapCtx.Buf
|
||||
for _, item := range mapCtx.Slice.Items {
|
||||
|
|
|
@ -11,8 +11,8 @@ func TestOpcodeSize(t *testing.T) {
|
|||
const uintptrSize = 4 << (^uintptr(0) >> 63)
|
||||
if uintptrSize == 8 {
|
||||
size := unsafe.Sizeof(encoder.Opcode{})
|
||||
if size != 120 {
|
||||
t.Fatalf("unexpected opcode size: expected 120bytes but got %dbytes", size)
|
||||
if size != 112 {
|
||||
t.Fatalf("unexpected opcode size: expected 112bytes but got %dbytes", size)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue