Skip to content

Commit

Permalink
Instructions refactor (#756)
Browse files Browse the repository at this point in the history
* operation lookup table from map to static array

* added comments to make operations array human readable and added more benchmark tests

* added quicksort to context switch benchmark

* fix fib benchmark, fn was being called with 0 as param

* more sensible numbers for fib benchmark

* reverted to previous more readable composite literal, maintaing array structure
  • Loading branch information
ear7h authored and st0012 committed Nov 24, 2018
1 parent 5f7576a commit d452510
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions compiler/bytecode/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
Pop
Dup
Leave
InstructionCount
)

// InstructionNameTable is the table the maps instruction's op code with its readable name
Expand Down
62 changes: 62 additions & 0 deletions vm/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,65 @@ func BenchmarkConcurrency(b *testing.B) {
runBench(b, script)
})
}

func BenchmarkContextSwitch(b *testing.B) {
b.Run("fib", func(b *testing.B) {
script := `
def fib(n)
if n <= 1
return n
else
return fib(n - 1) + fib(n - 2)
end
end
25.times do |i|
fib(i/2)
end
`
runBench(b, script)
})

b.Run("quicksort", func(b *testing.B) {
script := `
def quicksort(arr, l, r)
if l >= r
return
end
pivot = arr[l]
i = l - 1
j = r + 1
while true do
i += 1
while arr[i] < pivot do
i += 1
end
j -= 1
while arr[j] > pivot do
j -= 1
end
if i >= j
break
end
# swap
tmp = arr[i]
arr[i] = arr[j]
arr[j] = tmp
end
quicksort(arr, l, j)
quicksort(arr, j + 1, r)
end
arr = [ 0, 5, 3, 2, 5, 7, 3, 5, 6, 9] * 10
quicksort(arr, 0, arr.length - 1)
`
runBench(b, script)
})
}
4 changes: 2 additions & 2 deletions vm/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ type instructionSet struct {
paramTypes *bytecode.ArgSet
}

var operations map[operationType]operation
var operations [bytecode.InstructionCount]operation

// This is for avoiding initialization loop
func init() {
operations = map[operationType]operation{
operations = [bytecode.InstructionCount]operation{
bytecode.Pop: func(t *Thread, sourceLine int, cf *normalCallFrame, args ...interface{}) {
t.Stack.Pop()
},
Expand Down

0 comments on commit d452510

Please sign in to comment.