Skip to content

Commit cf0f887

Browse files
committed
vm: compile debug out with a constant - 180% speed improvement!
1 parent 4b9bdd5 commit cf0f887

File tree

1 file changed

+78
-34
lines changed

1 file changed

+78
-34
lines changed

vm/eval.go

+78-34
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ const debugging = false
3636

3737
// Debug print
3838
func debugf(format string, a ...interface{}) {
39-
if debugging {
40-
fmt.Printf(format, a...)
41-
}
39+
fmt.Printf(format, a...)
4240
}
4341

4442
// Stack operations
@@ -114,12 +112,14 @@ func (vm *Vm) CheckExceptionRecover(r interface{}) {
114112
vm.curexc = exc
115113
vm.AddTraceback(&vm.curexc)
116114
vm.why = whyException
117-
debugf("*** Propagating exception: %s\n", exc.Error())
115+
if debugging {
116+
debugf("*** Propagating exception: %s\n", exc.Error())
117+
}
118118
} else {
119119
// Coerce whatever was raised into a *Exception
120120
vm.SetException(py.MakeException(r))
121-
debugf("*** Exception raised %v\n", r)
122121
if debugging {
122+
debugf("*** Exception raised %v\n", r)
123123
debug.PrintStack()
124124
}
125125
}
@@ -130,7 +130,9 @@ func (vm *Vm) CheckExceptionRecover(r interface{}) {
130130
// Must be called as a defer function
131131
func (vm *Vm) CheckException() {
132132
if r := recover(); r != nil {
133-
debugf("*** Panic recovered %v\n", r)
133+
if debugging {
134+
debugf("*** Panic recovered %v\n", r)
135+
}
134136
vm.CheckExceptionRecover(r)
135137
}
136138
}
@@ -694,13 +696,19 @@ func do_POP_EXCEPT(vm *Vm, arg int32) error {
694696
// continues with the outer-next block.
695697
func do_END_FINALLY(vm *Vm, arg int32) error {
696698
v := vm.POP()
697-
debugf("END_FINALLY v=%#v\n", v)
699+
if debugging {
700+
debugf("END_FINALLY v=%#v\n", v)
701+
}
698702
if v == py.None {
699703
// None exception
700-
debugf(" END_FINALLY: None\n")
704+
if debugging {
705+
debugf(" END_FINALLY: None\n")
706+
}
701707
} else if vInt, ok := v.(py.Int); ok {
702708
vm.why = vmStatus(vInt)
703-
debugf(" END_FINALLY: Int %v\n", vm.why)
709+
if debugging {
710+
debugf(" END_FINALLY: Int %v\n", vm.why)
711+
}
704712
switch vm.why {
705713
case whyYield:
706714
panic("vm: Unexpected whyYield in END_FINALLY")
@@ -725,7 +733,9 @@ func do_END_FINALLY(vm *Vm, arg int32) error {
725733
} else if py.ExceptionClassCheck(v) {
726734
w := vm.POP()
727735
u := vm.POP()
728-
debugf(" END_FINALLY: Exc %v, Type %v, Traceback %v\n", v, w, u)
736+
if debugging {
737+
debugf(" END_FINALLY: Exc %v, Type %v, Traceback %v\n", v, w, u)
738+
}
729739
// FIXME PyErr_Restore(v, w, u)
730740
vm.curexc.Type, _ = v.(*py.Type)
731741
vm.curexc.Value = w
@@ -734,7 +744,9 @@ func do_END_FINALLY(vm *Vm, arg int32) error {
734744
} else {
735745
return py.ExceptionNewf(py.SystemError, "'finally' pops bad exception %#v", v)
736746
}
737-
debugf("END_FINALLY: vm.why = %v\n", vm.why)
747+
if debugging {
748+
debugf("END_FINALLY: vm.why = %v\n", vm.why)
749+
}
738750
return nil
739751
}
740752

@@ -861,7 +873,9 @@ func do_WITH_CLEANUP(vm *Vm, arg int32) error {
861873
// co_names of the code object. The compiler tries to use STORE_FAST
862874
// or STORE_GLOBAL if possible.
863875
func do_STORE_NAME(vm *Vm, namei int32) error {
864-
debugf("STORE_NAME %v\n", vm.frame.Code.Names[namei])
876+
if debugging {
877+
debugf("STORE_NAME %v\n", vm.frame.Code.Names[namei])
878+
}
865879
vm.frame.Locals[vm.frame.Code.Names[namei]] = vm.POP()
866880
return nil
867881
}
@@ -934,14 +948,16 @@ func do_DELETE_GLOBAL(vm *Vm, namei int32) error {
934948
// Pushes co_consts[consti] onto the stack.
935949
func do_LOAD_CONST(vm *Vm, consti int32) error {
936950
vm.PUSH(vm.frame.Code.Consts[consti])
937-
// debugf("LOAD_CONST %v\n", vm.TOP())
951+
// if debugging { debugf("LOAD_CONST %v\n", vm.TOP()) }
938952
return nil
939953
}
940954

941955
// Pushes the value associated with co_names[namei] onto the stack.
942956
func do_LOAD_NAME(vm *Vm, namei int32) error {
943957
name := vm.frame.Code.Names[namei]
944-
debugf("LOAD_NAME %v\n", name)
958+
if debugging {
959+
debugf("LOAD_NAME %v\n", name)
960+
}
945961
obj, ok := vm.frame.Lookup(name)
946962
if !ok {
947963
return py.ExceptionNewf(py.NameError, nameErrorMsg, name)
@@ -1177,7 +1193,9 @@ func do_FOR_ITER(vm *Vm, delta int32) error {
11771193
// Loads the global named co_names[namei] onto the stack.
11781194
func do_LOAD_GLOBAL(vm *Vm, namei int32) error {
11791195
name := vm.frame.Code.Names[namei]
1180-
debugf("LOAD_GLOBAL %v\n", name)
1196+
if debugging {
1197+
debugf("LOAD_GLOBAL %v\n", name)
1198+
}
11811199
obj, ok := vm.frame.LookupGlobal(name)
11821200
if !ok {
11831201
return py.ExceptionNewf(py.NameError, nameErrorMsg, name)
@@ -1351,7 +1369,9 @@ func (vm *Vm) raise(exc, cause py.Object) error {
13511369
// raise <instance>
13521370
// raise <type>
13531371
excException := py.MakeException(exc)
1354-
debugf("raise: excException = %v\n", excException)
1372+
if debugging {
1373+
debugf("raise: excException = %v\n", excException)
1374+
}
13551375
if cause != nil {
13561376
excException.Cause = py.MakeException(cause)
13571377
}
@@ -1570,9 +1590,9 @@ func callInternal(fn py.Object, args py.Tuple, kwargs py.StringDict, f *py.Frame
15701590
//
15711591
// The result is put on the stack
15721592
func (vm *Vm) Call(argc int32, starArgs py.Object, starKwargs py.Object) error {
1573-
// debugf("Stack: %v\n", vm.frame.Stack)
1574-
// debugf("Locals: %v\n", vm.frame.Locals)
1575-
// debugf("Globals: %v\n", vm.frame.Globals)
1593+
// if debugging { debugf("Stack: %v\n", vm.frame.Stack) }
1594+
// if debugging { debugf("Locals: %v\n", vm.frame.Locals) }
1595+
// if debugging { debugf("Globals: %v\n", vm.frame.Globals) }
15761596

15771597
// Get the arguments off the stack
15781598
nargs := int(argc & 0xFF)
@@ -1588,7 +1608,7 @@ func (vm *Vm) Call(argc int32, starArgs py.Object, starKwargs py.Object) error {
15881608

15891609
const multipleValues = "%s%s got multiple values for keyword argument '%s'"
15901610

1591-
// debugf("Call %T %v with args = %v, kwargsTuple = %v\n", fnObj, fnObj, args, kwargsTuple)
1611+
// if debugging { debugf("Call %T %v with args = %v, kwargsTuple = %v\n", fnObj, fnObj, args, kwargsTuple) }
15921612
var kwargs py.StringDict
15931613
if len(kwargsTuple) > 0 {
15941614
// Convert kwargsTuple into dictionary
@@ -1660,17 +1680,23 @@ func (vm *Vm) UnwindBlock(frame *py.Frame, block *py.TryBlock) {
16601680

16611681
// Unwinds the stack in the presence of an exception
16621682
func (vm *Vm) UnwindExceptHandler(frame *py.Frame, block *py.TryBlock) {
1663-
debugf("** UnwindExceptHandler stack depth %v\n", vm.STACK_LEVEL())
1683+
if debugging {
1684+
debugf("** UnwindExceptHandler stack depth %v\n", vm.STACK_LEVEL())
1685+
}
16641686
if vm.STACK_LEVEL() < block.Level+3 {
16651687
panic("vm: Couldn't find traceback on stack")
16661688
} else {
16671689
frame.Stack = frame.Stack[:block.Level+3]
16681690
}
1669-
debugf("** UnwindExceptHandler stack depth now %v\n", vm.STACK_LEVEL())
1691+
if debugging {
1692+
debugf("** UnwindExceptHandler stack depth now %v\n", vm.STACK_LEVEL())
1693+
}
16701694
vm.exc.Type, _ = vm.POP().(*py.Type)
16711695
vm.exc.Value = vm.POP()
16721696
vm.exc.Traceback, _ = vm.POP().(*py.Traceback)
1673-
debugf("** UnwindExceptHandler exc = (type: %v, value: %v, traceback: %v)\n", vm.exc.Type, vm.exc.Value, vm.exc.Traceback)
1697+
if debugging {
1698+
debugf("** UnwindExceptHandler exc = (type: %v, value: %v, traceback: %v)\n", vm.exc.Type, vm.exc.Value, vm.exc.Traceback)
1699+
}
16741700
}
16751701

16761702
// Run the virtual machine on a Frame object
@@ -1692,7 +1718,7 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
16921718
// default:
16931719
// err = errors.New(fmt.Sprintf("Unknown error '%s'", x))
16941720
// }
1695-
// debugf("*** Exception raised %v\n", r)
1721+
// if debugging { debugf("*** Exception raised %v\n", r) }
16961722
// // Dump the goroutine stack
16971723
// debug.PrintStack()
16981724
// }
@@ -1718,7 +1744,9 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
17181744
var arg int32
17191745
for vm.why == whyNot {
17201746
frame := vm.frame
1721-
debugf("* %4d:", frame.Lasti)
1747+
if debugging {
1748+
debugf("* %4d:", frame.Lasti)
1749+
}
17221750
opcodes := frame.Code.Code
17231751
opcode = OpCode(opcodes[frame.Lasti])
17241752
frame.Lasti++
@@ -1730,9 +1758,13 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
17301758
if vm.extended {
17311759
arg += vm.ext << 16
17321760
}
1733-
debugf(" %v(%d)\n", opcode, arg)
1761+
if debugging {
1762+
debugf(" %v(%d)\n", opcode, arg)
1763+
}
17341764
} else {
1735-
debugf(" %v\n", opcode)
1765+
if debugging {
1766+
debugf(" %v\n", opcode)
1767+
}
17361768
}
17371769
vm.extended = false
17381770
err = jumpTable[opcode](vm, arg)
@@ -1747,10 +1779,12 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
17471779
}
17481780
}
17491781
if vm.frame != nil {
1750-
debugf("* Stack = %#v\n", vm.frame.Stack)
1782+
if debugging {
1783+
debugf("* Stack = %#v\n", vm.frame.Stack)
1784+
}
17511785
// if len(vm.frame.Stack) > 0 {
17521786
// if t, ok := vm.TOP().(*py.Type); ok {
1753-
// debugf(" * TOP = %#v\n", t)
1787+
// if debugging { debugf(" * TOP = %#v\n", t) }
17541788
// }
17551789
// }
17561790
}
@@ -1764,7 +1798,9 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
17641798
// Peek at the current block.
17651799
frame := vm.frame
17661800
b := frame.Block
1767-
debugf("*** Unwinding %#v vm %#v\n", b, vm)
1801+
if debugging {
1802+
debugf("*** Unwinding %#v vm %#v\n", b, vm)
1803+
}
17681804

17691805
if b.Type == py.TryBlockSetupLoop && vm.why == whyContinue {
17701806
vm.why = whyNot
@@ -1777,19 +1813,25 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
17771813
frame.PopBlock()
17781814

17791815
if b.Type == py.TryBlockExceptHandler {
1780-
debugf("*** EXCEPT_HANDLER\n")
1816+
if debugging {
1817+
debugf("*** EXCEPT_HANDLER\n")
1818+
}
17811819
vm.UnwindExceptHandler(frame, b)
17821820
continue
17831821
}
17841822
vm.UnwindBlock(frame, b)
17851823
if b.Type == py.TryBlockSetupLoop && vm.why == whyBreak {
1786-
debugf("*** Loop\n")
1824+
if debugging {
1825+
debugf("*** Loop\n")
1826+
}
17871827
vm.why = whyNot
17881828
frame.Lasti = b.Handler
17891829
break
17901830
}
17911831
if vm.why == whyException && (b.Type == py.TryBlockSetupExcept || b.Type == py.TryBlockSetupFinally) {
1792-
debugf("*** Exception\n")
1832+
if debugging {
1833+
debugf("*** Exception\n")
1834+
}
17931835
handler := b.Handler
17941836
// This invalidates b
17951837
frame.PushBlock(py.TryBlockExceptHandler, -1, vm.STACK_LEVEL())
@@ -1838,7 +1880,9 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
18381880
}
18391881
}
18401882
}
1841-
debugf("EXIT with %v\n", vm.why)
1883+
if debugging {
1884+
debugf("EXIT with %v\n", vm.why)
1885+
}
18421886
if vm.why != whyReturn {
18431887
vm.retval = nil
18441888
}

0 commit comments

Comments
 (0)