Skip to content

Commit

Permalink
Inlining regstry.Set*
Browse files Browse the repository at this point in the history
regstry.Set* were very simple functions at the beginning of the project,
but recently not.

It seems inlining these functions make improvements better.
  • Loading branch information
yuin committed Dec 2, 2023
1 parent 9d7d921 commit 1e3d646
Show file tree
Hide file tree
Showing 4 changed files with 945 additions and 132 deletions.
47 changes: 24 additions & 23 deletions _state.go
Expand Up @@ -398,25 +398,26 @@ func (rg *registry) forceResize(newSize int) {
copy(newSlice, rg.array[:rg.top]) // should we copy the area beyond top? there shouldn't be any valid values there so it shouldn't be necessary.
rg.array = newSlice
}
func (rg *registry) SetTop(top int) {
// +inline-call rg.checkSize top
oldtop := rg.top
rg.top = top
for i := oldtop; i < rg.top; i++ {

func (rg *registry) SetTop(topi int) { // +inline-start
// +inline-call rg.checkSize topi
oldtopi := rg.top
rg.top = topi
for i := oldtopi; i < rg.top; i++ {
rg.array[i] = LNil
}
// values beyond top don't need to be valid LValues, so setting them to nil is fine
// setting them to nil rather than LNil lets us invoke the golang memclr opto
if rg.top < oldtop {
nilRange := rg.array[rg.top:oldtop]
if rg.top < oldtopi {
nilRange := rg.array[rg.top:oldtopi]
for i := range nilRange {
nilRange[i] = nil
}
}
//for i := rg.top; i < oldtop; i++ {
// rg.array[i] = LNil
//}
}
} // +inline-end

func (rg *registry) Top() int {
return rg.top
Expand Down Expand Up @@ -498,34 +499,34 @@ func (rg *registry) FillNil(regm, n int) { // +inline-start
func (rg *registry) Insert(value LValue, reg int) {
top := rg.Top()
if reg >= top {
rg.Set(reg, value)
// +inline-call rg.Set reg value
return
}
top--
for ; top >= reg; top-- {
// FIXME consider using copy() here if Insert() is called enough
rg.Set(top+1, rg.Get(top))
// +inline-call rg.Set top+1 rg.Get(top)
}
rg.Set(reg, value)
// +inline-call rg.Set reg value
}

func (rg *registry) Set(reg int, val LValue) {
newSize := reg + 1
func (rg *registry) Set(regi int, vali LValue) { // +inline-start
newSize := regi + 1
// +inline-call rg.checkSize newSize
rg.array[reg] = val
if reg >= rg.top {
rg.top = reg + 1
rg.array[regi] = vali
if regi >= rg.top {
rg.top = regi + 1
}
}
} // +inline-end

func (rg *registry) SetNumber(reg int, val LNumber) {
newSize := reg + 1
func (rg *registry) SetNumber(regi int, vali LNumber) { // +inline-start
newSize := regi + 1
// +inline-call rg.checkSize newSize
rg.array[reg] = rg.alloc.LNumber2I(val)
if reg >= rg.top {
rg.top = reg + 1
rg.array[regi] = rg.alloc.LNumber2I(vali)
if regi >= rg.top {
rg.top = regi + 1
}
}
} // +inline-end

func (rg *registry) IsFull() bool {
return rg.top >= cap(rg.array)
Expand Down
100 changes: 58 additions & 42 deletions _vm.go
Expand Up @@ -173,7 +173,8 @@ func init() {
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
reg.Set(RA, reg.Get(lbase+B))
v := reg.Get(lbase + B)
// +inline-call reg.Set RA v
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_MOVEN
Expand All @@ -183,15 +184,17 @@ func init() {
A := int(inst>>18) & 0xff //GETA
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
reg.Set(lbase+A, reg.Get(lbase+B))
v := reg.Get(lbase + B)
// +inline-call reg.Set lbase+A v
code := cf.Fn.Proto.Code
pc := cf.Pc
for i := 0; i < C; i++ {
inst = code[pc]
pc++
A = int(inst>>18) & 0xff //GETA
B = int(inst & 0x1ff) //GETB
reg.Set(lbase+A, reg.Get(lbase+B))
v := reg.Get(lbase + B)
// +inline-call reg.Set lbase+A v
}
cf.Pc = pc
return 0
Expand All @@ -203,7 +206,8 @@ func init() {
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
Bx := int(inst & 0x3ffff) //GETBX
reg.Set(RA, cf.Fn.Proto.Constants[Bx])
v := cf.Fn.Proto.Constants[Bx]
// +inline-call reg.Set RA v
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_LOADBOOL
Expand All @@ -215,9 +219,9 @@ func init() {
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
if B != 0 {
reg.Set(RA, LTrue)
// +inline-call reg.Set RA LTrue
} else {
reg.Set(RA, LFalse)
// +inline-call reg.Set RA LFalse
}
if C != 0 {
cf.Pc++
Expand All @@ -232,7 +236,7 @@ func init() {
RA := lbase + A
B := int(inst & 0x1ff) //GETB
for i := RA; i <= lbase+B; i++ {
reg.Set(i, LNil)
// +inline-call reg.Set i LNil
}
return 0
},
Expand All @@ -243,7 +247,8 @@ func init() {
A := int(inst>>18) & 0xff //GETA
RA := lbase + A
B := int(inst & 0x1ff) //GETB
reg.Set(RA, cf.Fn.Upvalues[B].Value())
v := cf.Fn.Upvalues[B].Value()
// +inline-call reg.Set RA v
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_GETGLOBAL
Expand All @@ -254,7 +259,8 @@ func init() {
RA := lbase + A
Bx := int(inst & 0x3ffff) //GETBX
//reg.Set(RA, L.getField(cf.Fn.Env, cf.Fn.Proto.Constants[Bx]))
reg.Set(RA, L.getFieldString(cf.Fn.Env, cf.Fn.Proto.stringConstants[Bx]))
v := L.getFieldString(cf.Fn.Env, cf.Fn.Proto.stringConstants[Bx])
// +inline-call reg.Set RA v
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_GETTABLE
Expand All @@ -265,7 +271,8 @@ func init() {
RA := lbase + A
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
reg.Set(RA, L.getField(reg.Get(lbase+B), L.rkValue(C)))
v := L.getField(reg.Get(lbase+B), L.rkValue(C))
// +inline-call reg.Set RA v
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_GETTABLEKS
Expand All @@ -276,7 +283,8 @@ func init() {
RA := lbase + A
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
reg.Set(RA, L.getFieldString(reg.Get(lbase+B), L.rkString(C)))
v := L.getFieldString(reg.Get(lbase+B), L.rkString(C))
// +inline-call reg.Set RA v
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_SETGLOBAL
Expand Down Expand Up @@ -330,7 +338,8 @@ func init() {
RA := lbase + A
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
reg.Set(RA, newLTable(B, C))
v := newLTable(B, C)
// +inline-call reg.Set RA v
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_SELF
Expand All @@ -342,8 +351,9 @@ func init() {
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
selfobj := reg.Get(lbase + B)
reg.Set(RA, L.getFieldString(selfobj, L.rkString(C)))
reg.Set(RA+1, selfobj)
v := L.getFieldString(selfobj, L.rkString(C))
// +inline-call reg.Set RA v
// +inline-call reg.Set RA+1 selfobj
return 0
},
opArith, // OP_ADD
Expand All @@ -361,17 +371,17 @@ func init() {
B := int(inst & 0x1ff) //GETB
unaryv := L.rkValue(B)
if nm, ok := unaryv.(LNumber); ok {
reg.SetNumber(RA, -nm)
// +inline-call reg.Set RA -nm
} else {
op := L.metaOp1(unaryv, "__unm")
if op.Type() == LTFunction {
reg.Push(op)
reg.Push(unaryv)
L.Call(1, 1)
reg.Set(RA, reg.Pop())
// +inline-call reg.Set RA reg.Pop()
} else if str, ok1 := unaryv.(LString); ok1 {
if num, err := parseNumber(string(str)); err == nil {
reg.Set(RA, -num)
// +inline-call reg.Set RA -num
} else {
L.RaiseError("__unm undefined")
}
Expand All @@ -389,9 +399,9 @@ func init() {
RA := lbase + A
B := int(inst & 0x1ff) //GETB
if LVIsFalse(reg.Get(lbase + B)) {
reg.Set(RA, LTrue)
// +inline-call reg.Set RA LTrue
} else {
reg.Set(RA, LFalse)
// +inline-call reg.Set RA LFalse
}
return 0
},
Expand All @@ -404,7 +414,7 @@ func init() {
B := int(inst & 0x1ff) //GETB
switch lv := L.rkValue(B).(type) {
case LString:
reg.SetNumber(RA, LNumber(len(lv)))
// +inline-call reg.SetNumber RA LNumber(len(lv))
default:
op := L.metaOp1(lv, "__len")
if op.Type() == LTFunction {
Expand All @@ -413,12 +423,13 @@ func init() {
L.Call(1, 1)
ret := reg.Pop()
if ret.Type() == LTNumber {
reg.SetNumber(RA, ret.(LNumber))
v, _ := ret.(LNumber)
// +inline-call reg.SetNumber RA v
} else {
reg.Set(RA, ret)
// +inline-call reg.Set RA ret
}
} else if lv.Type() == LTTable {
reg.SetNumber(RA, LNumber(lv.(*LTable).Len()))
// +inline-call reg.SetNumber RA LNumber(lv.(*LTable).Len())
} else {
L.RaiseError("__len undefined")
}
Expand All @@ -435,7 +446,8 @@ func init() {
C := int(inst>>9) & 0x1ff //GETC
RC := lbase + C
RB := lbase + B
reg.Set(RA, stringConcat(L, RC-RB+1, RC))
v := stringConcat(L, RC-RB+1, RC)
// +inline-call reg.Set RA v
return 0
},
func(L *LState, inst uint32, baseframe *callFrame) int { //OP_JMP
Expand Down Expand Up @@ -538,7 +550,7 @@ func init() {
B := int(inst & 0x1ff) //GETB
C := int(inst>>9) & 0x1ff //GETC
if value := reg.Get(lbase + B); LVAsBool(value) != (C == 0) {
reg.Set(RA, value)
// +inline-call reg.Set RA value
} else {
cf.Pc++
}
Expand Down Expand Up @@ -677,13 +689,14 @@ func init() {
if limit, ok2 := reg.Get(RA + 1).assertFloat64(); ok2 {
if step, ok3 := reg.Get(RA + 2).assertFloat64(); ok3 {
init += step
reg.SetNumber(RA, LNumber(init))
v := LNumber(init)
// +inline-call reg.SetNumber RA v
if (step > 0 && init <= limit) || (step <= 0 && init >= limit) {
Sbx := int(inst&0x3ffff) - opMaxArgSbx //GETSBX
cf.Pc += Sbx
reg.SetNumber(RA+3, LNumber(init))
// +inline-call reg.SetNumber RA+3 v
} else {
reg.SetTop(RA + 1)
// +inline-call reg.SetTop RA+1
}
} else {
L.RaiseError("for statement step must be a number")
Expand All @@ -705,7 +718,7 @@ func init() {
Sbx := int(inst&0x3ffff) - opMaxArgSbx //GETSBX
if init, ok1 := reg.Get(RA).assertFloat64(); ok1 {
if step, ok2 := reg.Get(RA + 2).assertFloat64(); ok2 {
reg.SetNumber(RA, LNumber(init-step))
// +inline-call reg.SetNumber RA LNumber(init-step)
} else {
L.RaiseError("for statement step must be a number")
}
Expand All @@ -723,13 +736,13 @@ func init() {
RA := lbase + A
C := int(inst>>9) & 0x1ff //GETC
nret := C
reg.SetTop(RA + 3 + 2)
reg.Set(RA+3+2, reg.Get(RA+2))
reg.Set(RA+3+1, reg.Get(RA+1))
reg.Set(RA+3, reg.Get(RA))
// +inline-call reg.SetTop RA+3+2
// +inline-call reg.Set RA+3+2 reg.Get(RA+2)
// +inline-call reg.Set RA+3+1 reg.Get(RA+1)
// +inline-call reg.Set RA+3 reg.Get(RA)
L.callR(2, nret, RA+3)
if value := reg.Get(RA + 3); value != LNil {
reg.Set(RA+2, value)
// +inline-call reg.Set RA+2 value
pc := cf.Fn.Proto.Code[cf.Pc]
cf.Pc += int(pc&0x3ffff) - opMaxArgSbx
}
Expand Down Expand Up @@ -776,7 +789,7 @@ func init() {
Bx := int(inst & 0x3ffff) //GETBX
proto := cf.Fn.Proto.FunctionPrototypes[Bx]
closure := newLFunctionL(proto, cf.Fn.Env, int(proto.NumUpvalues))
reg.Set(RA, closure)
// +inline-call reg.Set RA closure
for i := 0; i < int(proto.NumUpvalues); i++ {
inst = cf.Fn.Proto.Code[cf.Pc]
cf.Pc++
Expand Down Expand Up @@ -829,9 +842,11 @@ func opArith(L *LState, inst uint32, baseframe *callFrame) int { //OP_ADD, OP_SU
v1, ok1 := lhs.assertFloat64()
v2, ok2 := rhs.assertFloat64()
if ok1 && ok2 {
reg.SetNumber(RA, numberArith(L, opcode, LNumber(v1), LNumber(v2)))
v := numberArith(L, opcode, LNumber(v1), LNumber(v2))
// +inline-call reg.SetNumber RA v
} else {
reg.Set(RA, objectArith(L, opcode, lhs, rhs))
v := objectArith(L, opcode, lhs, rhs)
// +inline-call reg.Set RA v
}
return 0
}
Expand Down Expand Up @@ -884,19 +899,19 @@ func objectArith(L *LState, opcode int, lhs, rhs LValue) LValue {
event = "__pow"
}
op := L.metaOp2(lhs, rhs, event)
if op.Type() == LTFunction {
if _, ok := op.assertFunction(); ok {
L.reg.Push(op)
L.reg.Push(lhs)
L.reg.Push(rhs)
L.Call(2, 1)
return L.reg.Pop()
}
if str, ok := lhs.(LString); ok {
if str, ok := lhs.assertString(); ok {
if lnum, err := parseNumber(string(str)); err == nil {
lhs = lnum
}
}
if str, ok := rhs.(LString); ok {
if str, ok := rhs.assertString(); ok {
if rnum, err := parseNumber(string(str)); err == nil {
rhs = rnum
}
Expand Down Expand Up @@ -972,12 +987,13 @@ func lessThan(L *LState, lhs, rhs LValue) bool {
}

func equals(L *LState, lhs, rhs LValue, raw bool) bool {
if lhs.Type() != rhs.Type() {
lt := lhs.Type()
if lt != rhs.Type() {
return false
}

ret := false
switch lhs.Type() {
switch lt {
case LTNil:
ret = true
case LTNumber:
Expand Down

0 comments on commit 1e3d646

Please sign in to comment.