optimizations
This commit is contained in:
14
profile.go
14
profile.go
@@ -7,7 +7,6 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"monkey/internal/compiler"
|
||||
"monkey/internal/object"
|
||||
"monkey/internal/parser"
|
||||
"monkey/internal/vm"
|
||||
"os"
|
||||
@@ -24,7 +23,7 @@ fib := fn(n) {
|
||||
return fib(n-1) + fib(n-2)
|
||||
}
|
||||
|
||||
print(fib(%d))`
|
||||
println(fib(%d))`
|
||||
|
||||
func checkErr(err error) {
|
||||
if err != nil {
|
||||
@@ -39,13 +38,12 @@ func readFile(path string) string {
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func fileOrDefault() string {
|
||||
func fileOrDefault() (string, []string) {
|
||||
if flag.NArg() > 0 {
|
||||
log.Printf("Using %s as program input...", flag.Arg(0))
|
||||
object.Args = flag.Args()[1:]
|
||||
return readFile(flag.Arg(0))
|
||||
return readFile(flag.Arg(0)), flag.Args()[1:]
|
||||
}
|
||||
return defaultCode
|
||||
return defaultCode, flag.Args()[:]
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -60,7 +58,7 @@ func main() {
|
||||
|
||||
log.Printf("Using %d as n", *n)
|
||||
|
||||
code := fileOrDefault()
|
||||
code, args := fileOrDefault()
|
||||
if strings.Count(code, "%d") > 0 {
|
||||
code = fmt.Sprintf(code, *n)
|
||||
}
|
||||
@@ -78,7 +76,7 @@ func main() {
|
||||
checkErr(c.Compile(tree))
|
||||
checkErr(pprof.StartCPUProfile(cpuf))
|
||||
defer pprof.StopCPUProfile()
|
||||
mvm := vm.New("<profiler>", c.Bytecode())
|
||||
mvm := vm.New("<profiler>", c.Bytecode(), vm.WithContext(context.New(context.WithArgs(args))))
|
||||
checkErr(mvm.Run())
|
||||
runtime.GC()
|
||||
checkErr(pprof.WriteHeapProfile(heapf))
|
||||
|
||||
Reference in New Issue
Block a user