From 79dba78e41ab23f1415da620a3cd0d55a33bdd10 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Sat, 27 Nov 2021 18:05:42 +0900 Subject: [PATCH] remove unnecessary code --- internal/encoder/code.go | 1 - internal/encoder/opcode.go | 76 ++------------------------------------ size_test.go | 2 +- 3 files changed, 5 insertions(+), 74 deletions(-) diff --git a/internal/encoder/code.go b/internal/encoder/code.go index 24b5126..8881215 100644 --- a/internal/encoder/code.go +++ b/internal/encoder/code.go @@ -421,7 +421,6 @@ func (c *StructCode) ToOpcode(ctx *compileContext) Opcodes { head.NextField = end head.Next = end head.End = end - end.PrevField = head codes = append(codes, head, end) ctx.incIndex() } diff --git a/internal/encoder/opcode.go b/internal/encoder/opcode.go index eb78001..309cfe6 100644 --- a/internal/encoder/opcode.go +++ b/internal/encoder/opcode.go @@ -38,7 +38,6 @@ type Opcode struct { Flags OpFlags Type *runtime.Type // go type - PrevField *Opcode // prev struct field 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 @@ -86,20 +85,6 @@ func (c *Opcode) IsEnd() bool { return c.Op == OpEnd || c.Op == OpInterfaceEnd || c.Op == OpRecursiveEnd } -func (c *Opcode) IsStructHeadOp() bool { - if c == nil { - return false - } - return strings.Contains(c.Op.String(), "Head") -} - -func (c *Opcode) IsRecursiveOp() bool { - if c == nil { - return false - } - return strings.Contains(c.Op.String(), "Recursive") -} - func (c *Opcode) MaxIdx() uint32 { max := uint32(0) for _, value := range []uint32{ @@ -337,29 +322,18 @@ func copyOpcode(code *Opcode) *Opcode { } func setTotalLengthToInterfaceOp(code *Opcode) { - c := code - for c.Op != OpEnd && c.Op != OpInterfaceEnd { + for c := code; !c.IsEnd(); { if c.Op == OpInterface { c.Length = uint32(code.TotalLength()) } - switch c.Op.CodeType() { - case CodeArrayElem, CodeSliceElem, CodeMapKey: - c = c.End - default: - c = c.Next - } + c = c.IterNext() } } func ToEndCode(code *Opcode) *Opcode { c := code - for c.Op != OpEnd && c.Op != OpInterfaceEnd { - switch c.Op.CodeType() { - case CodeArrayElem, CodeSliceElem, CodeMapKey: - c = c.End - default: - c = c.Next - } + for !c.IsEnd() { + c = c.IterNext() } return c } @@ -418,24 +392,12 @@ func (c *Opcode) copy(codeMap map[uintptr]*Opcode) *Opcode { } codeMap[addr] = copied copied.End = c.End.copy(codeMap) - copied.PrevField = c.PrevField.copy(codeMap) copied.NextField = c.NextField.copy(codeMap) copied.Next = c.Next.copy(codeMap) copied.Jmp = c.Jmp return copied } -func (c *Opcode) BeforeLastCode() *Opcode { - code := c - for { - nextCode := code.IterNext() - if nextCode.IsEnd() { - return code - } - code = nextCode - } -} - func (c *Opcode) TotalLength() int { var idx int code := c @@ -622,36 +584,6 @@ func (c *Opcode) Dump() string { return strings.Join(codes, "\n") } -func prevField(code *Opcode, removedFields map[*Opcode]struct{}) *Opcode { - if _, exists := removedFields[code]; exists { - return prevField(code.PrevField, removedFields) - } - return code -} - -func nextField(code *Opcode, removedFields map[*Opcode]struct{}) *Opcode { - if _, exists := removedFields[code]; exists { - return nextField(code.NextField, removedFields) - } - return code -} - -func linkPrevToNextField(cur *Opcode, removedFields map[*Opcode]struct{}) { - prev := prevField(cur.PrevField, removedFields) - prev.NextField = nextField(cur.NextField, removedFields) - code := prev - for { - nextCode := code.IterNext() - if nextCode == cur { - code.Next = cur.NextField - break - } else if nextCode.Op == OpEnd { - break - } - code = nextCode - } -} - func newSliceHeaderCode(ctx *compileContext) *Opcode { idx := opcodeOffset(ctx.ptrIndex) ctx.incPtrIndex() diff --git a/size_test.go b/size_test.go index a57d663..8f3864e 100644 --- a/size_test.go +++ b/size_test.go @@ -11,7 +11,7 @@ func TestOpcodeSize(t *testing.T) { const uintptrSize = 4 << (^uintptr(0) >> 63) if uintptrSize == 8 { size := unsafe.Sizeof(encoder.Opcode{}) - if size != 128 { + if size != 120 { t.Fatalf("unexpected opcode size: expected 128bytes but got %dbytes", size) } }