diff options
| -rw-r--r-- | cmd/hlang/h/parser_test.go | 11 | ||||
| -rw-r--r-- | cmd/hlang/http.go | 2 | ||||
| -rw-r--r-- | cmd/hlang/main.go | 1 | ||||
| -rw-r--r-- | cmd/hlang/nguh/compile_test.go | 19 | ||||
| -rw-r--r-- | cmd/hlang/run.go | 70 | ||||
| -rw-r--r-- | cmd/hlang/run_test.go | 17 | ||||
| -rw-r--r-- | cmd/hlang/testdata/h.wasm | bin | 0 -> 69 bytes | |||
| -rw-r--r-- | go.mod | 2 | ||||
| -rw-r--r-- | go.sum | 8 | ||||
| -rw-r--r-- | gomod2nix.toml | 6 |
10 files changed, 70 insertions, 66 deletions
diff --git a/cmd/hlang/h/parser_test.go b/cmd/hlang/h/parser_test.go new file mode 100644 index 0000000..c55cd7d --- /dev/null +++ b/cmd/hlang/h/parser_test.go @@ -0,0 +1,11 @@ +package h + +import "testing" + +func BenchmarkParse(b *testing.B) { + for i := 0; i < b.N; i++ { + if _, err := Parse("h h h"); err != nil { + b.Fatal(err) + } + } +} diff --git a/cmd/hlang/http.go b/cmd/hlang/http.go index 4a7f9df..2dc61b6 100644 --- a/cmd/hlang/http.go +++ b/cmd/hlang/http.go @@ -384,7 +384,6 @@ const playgroundTemplate = `<html> const programData = document.getElementById("program").value; const output = document.getElementById("output"); const astBox = document.getElementById("ast_box"); - const gasUsed = document.getElementById("gas_used"); const execTime = document.getElementById("exec_time"); const status = document.getElementById("status"); @@ -400,7 +399,6 @@ const playgroundTemplate = `<html> status.innerHTML = "success"; astBox.innerHTML = data.prog.ast; output.innerHTML = data.res.out; - gasUsed.innerHTML = data.res.gas; execTime.innerHTML = data.res.exec_duration; }) .catch(function(error) { diff --git a/cmd/hlang/main.go b/cmd/hlang/main.go index c17b5e7..69f8f82 100644 --- a/cmd/hlang/main.go +++ b/cmd/hlang/main.go @@ -53,7 +53,6 @@ func oneOff() error { log.Println("success!") - log.Printf("gas used:\t%d", er.GasUsed) log.Printf("exec time:\t%s", er.ExecTime) log.Println("output:") fmt.Print(er.Output) diff --git a/cmd/hlang/nguh/compile_test.go b/cmd/hlang/nguh/compile_test.go index 2b130f9..698fb7b 100644 --- a/cmd/hlang/nguh/compile_test.go +++ b/cmd/hlang/nguh/compile_test.go @@ -1,8 +1,6 @@ package nguh import ( - "encoding/json" - "os" "testing" "github.com/eaburns/peggy/peg" @@ -11,10 +9,23 @@ import ( func TestCompile(t *testing.T) { inp := &peg.Node{Text: "h"} - result, err := Compile(inp) + _, err := Compile(inp) if err != nil { t.Fatal(err) } +} + +func BenchmarkCompile(b *testing.B) { + inp := &peg.Node{Kids: []*peg.Node{ + {Text: "h"}, + {Text: "h"}, + {Text: "h"}, + }, + } - json.NewEncoder(os.Stdout).Encode(result) + for i := 0; i < b.N; i++ { + if _, err := Compile(inp); err != nil { + b.Fatal(err) + } + } } diff --git a/cmd/hlang/run.go b/cmd/hlang/run.go index fb55bb7..baa6756 100644 --- a/cmd/hlang/run.go +++ b/cmd/hlang/run.go @@ -1,73 +1,57 @@ package main import ( - "errors" + "context" "time" - "github.com/perlin-network/life/compiler" - "github.com/perlin-network/life/exec" + "github.com/tetratelabs/wazero" ) type Process struct { Output []byte } -// ResolveGlobal does nothing, currently. -func (p *Process) ResolveGlobal(module, field string) int64 { return 0 } - -// ResolveFunc resolves h's ABI and importable function. -func (p *Process) ResolveFunc(module, field string) exec.FunctionImport { - switch module { - case "h": - switch field { - case "h": - return func(vm *exec.VirtualMachine) int64 { - frame := vm.GetCurrentFrame() - data := frame.Locals[0] - p.Output = append(p.Output, byte(data)) - - return 0 - } - - default: - panic("impossible state") - } - - default: - panic("impossible state") - } -} - -type ExecResult struct { - Output string `json:"out"` - GasUsed uint64 `json:"gas"` - ExecTime time.Duration `json:"exec_duration"` +func (p *Process) Putchar(char int32) { + p.Output = append(p.Output, byte(char)) } func run(bin []byte) (*ExecResult, error) { + ctx := context.Background() + r := wazero.NewRuntime(ctx) + defer r.Close(ctx) + p := &Process{} - var cfg exec.VMConfig - gp := &compiler.SimpleGasPolicy{GasPerInstruction: 1} - vm, err := exec.NewVirtualMachine(bin, cfg, p, gp) + env, err := r.NewHostModuleBuilder("h").NewFunctionBuilder().WithFunc(func(char int32) { p.Putchar(char) }).Export("h").Instantiate(ctx, r) if err != nil { return nil, err } + defer env.Close(ctx) - mainFunc, ok := vm.GetFunctionExport("h") - if !ok { - return nil, errors.New("impossible state: no h function exposed") + code, err := r.CompileModule(ctx, bin) + if err != nil { + return nil, err } - t0 := time.Now() - _, err = vm.Run(mainFunc) + mod, err := r.InstantiateModule(ctx, code, wazero.NewModuleConfig()) if err != nil { return nil, err } + defer mod.Close(ctx) + + t0 := time.Now() + if _, err = mod.ExportedFunction("h").Call(ctx); err != nil { + return nil, err + } + runTime := time.Since(t0) return &ExecResult{ Output: string(p.Output), - GasUsed: vm.Gas, - ExecTime: time.Since(t0), + ExecTime: runTime, }, nil } + +type ExecResult struct { + Output string `json:"out"` + ExecTime time.Duration `json:"exec_duration"` +} diff --git a/cmd/hlang/run_test.go b/cmd/hlang/run_test.go new file mode 100644 index 0000000..9fe38cb --- /dev/null +++ b/cmd/hlang/run_test.go @@ -0,0 +1,17 @@ +package main + +import ( + _ "embed" + "testing" +) + +//go:embed testdata/h.wasm +var bin []byte + +func BenchmarkRun(b *testing.B) { + for i := 0; i < b.N; i++ { + if _, err := run(bin); err != nil { + b.Fatal(err) + } + } +} diff --git a/cmd/hlang/testdata/h.wasm b/cmd/hlang/testdata/h.wasm Binary files differnew file mode 100644 index 0000000..6a0d90d --- /dev/null +++ b/cmd/hlang/testdata/h.wasm @@ -24,7 +24,6 @@ require ( github.com/mmikulicic/stringlist v1.0.0 github.com/mndrix/golog v0.0.0-20170330170653-a28e2a269775 github.com/otiai10/copy v1.9.0 - github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea github.com/peterh/liner v1.2.2 github.com/pkg/errors v0.9.1 github.com/posener/complete v1.2.3 @@ -111,7 +110,6 @@ require ( github.com/u-root/uio v0.0.0-20220204230159-dac05f7d2cb4 // indirect github.com/vishvananda/netlink v1.1.1-0.20211118161826-650dca95af54 // indirect github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 // indirect - github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect github.com/x448/float16 v0.8.4 // indirect go4.org/mem v0.0.0-20210711025021-927187094b94 // indirect go4.org/netipx v0.0.0-20220725152314-7e7bdc8411bf // indirect @@ -103,7 +103,6 @@ github.com/eaburns/peggy v0.0.0-20190420135231-b61cdde6efe6/go.mod h1:5tfPwI6uki github.com/eaburns/pretty v0.0.0-20170305202417-362524b72369/go.mod h1:iW/TU1T4mA4w2KzqNbBCjacPFdJ9PfGvNSxr8ajT/iM= github.com/eaburns/pretty v0.0.0-20190404101635-2e1d2550ef0b h1:R9nfsvxwt5wAfutYAJykrIpxPvUX9n5LUit05E2E6vA= github.com/eaburns/pretty v0.0.0-20190404101635-2e1d2550ef0b/go.mod h1:iW/TU1T4mA4w2KzqNbBCjacPFdJ9PfGvNSxr8ajT/iM= -github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw= github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -329,8 +328,6 @@ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6 github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= github.com/otiai10/mint v1.4.0 h1:umwcf7gbpEwf7WFzqmWwSv0CzbeMsae2u9ZvpP8j2q4= github.com/otiai10/mint v1.4.0/go.mod h1:gifjb2MYOoULtKLqUAEILUG/9KONW6f7YsJ6vQLTlFI= -github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea h1:okKoivlkNRRLqXraEtatHfEhW+D71QTwkaj+4n4M2Xc= -github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea/go.mod h1:3KEU5Dm8MAYWZqity880wOFJ9PhQjyKVZGwAEfc5Q4E= github.com/peterh/liner v1.2.2 h1:aJ4AOodmL+JxOZZEL2u9iJf8omNRpqHc/EbrK+3mAXw= github.com/peterh/liner v1.2.2/go.mod h1:xFwJyiKIXJZUKItq5dGHZSTBRAuG/CpeNpWLyiNRNwI= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= @@ -379,7 +376,6 @@ github.com/tetratelabs/wazero v1.0.0-pre.5 h1:ChZsQewsazFwF+NT6qAKPwnqqiOlf2MN0a github.com/tetratelabs/wazero v1.0.0-pre.5/go.mod h1:u8wrFmpdrykiFK0DFPiFm5a4+0RzsdmXYVtijBKqUVo= github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef h1:7D6Nm4D6f0ci9yttWaKjM1TMAXrH5Su72dojqYGntFY= github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef/go.mod h1:WLFStEdnJXpjK8kd4qKLwQKX/1vrDzp5BcDyiZJBHJM= -github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc h1:RTUQlKzoZZVG3umWNzOYeFecQLIh+dbxXvJp1zPQJTI= github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc/go.mod h1:NoCfSFWosfqMqmmD7hApkirIK9ozpHjxRnRxs1l413A= github.com/u-root/uio v0.0.0-20210528114334-82958018845c/go.mod h1:LpEX5FO/cB+WF4TYGY1V5qktpaZLkKkSegbr0V4eYXA= github.com/u-root/uio v0.0.0-20220204230159-dac05f7d2cb4 h1:hl6sK6aFgTLISijk6xIzeqnPzQcsLqqvL6vEfTPinME= @@ -394,8 +390,6 @@ github.com/vishvananda/netlink v1.1.1-0.20211118161826-650dca95af54/go.mod h1:tw github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0= github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 h1:gga7acRE695APm9hlsSMoOoE65U4/TcqNj90mc69Rlg= github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0= -github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= -github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -680,7 +674,6 @@ google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9Ywl google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/appengine v1.6.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= @@ -742,7 +735,6 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/tucnak/telebot.v2 v2.0.0-20190415090633-8c1c512262f2 h1:wGFU+pn16CuaCt4lc9gkdF1R/y4M8CRi4g6yj7pxpBE= diff --git a/gomod2nix.toml b/gomod2nix.toml index d9b0a64..02cc99a 100644 --- a/gomod2nix.toml +++ b/gomod2nix.toml @@ -223,9 +223,6 @@ schema = 3 [mod."github.com/otiai10/copy"] version = "v1.9.0" hash = "sha256-JKRqb4hfnYjglrsF9WikEZup8zIcV7Au854Sn+nJagU=" - [mod."github.com/perlin-network/life"] - version = "v0.0.0-20191203030451-05c0e0f7eaea" - hash = "sha256-xgHmt0kiHNKag5Hxa+b/Q+8Vy78Nq23r80c80pmbFd8=" [mod."github.com/peterh/liner"] version = "v1.2.2" hash = "sha256-F+cIfbpR/xQ3ODf5srpfJxLYIdgSHVc6YK16oKSqAEM=" @@ -283,9 +280,6 @@ schema = 3 [mod."github.com/vishvananda/netns"] version = "v0.0.0-20211101163701-50045581ed74" hash = "sha256-B5BcBbKh/XD0baExSidXZZGPzQccsOrOmdUMUAE6VU0=" - [mod."github.com/vmihailenco/msgpack"] - version = "v4.0.4+incompatible" - hash = "sha256-gA3l8Ma1XVja+C8XJzgRTQVb1htbsh4yrbnOu6cuw9U=" [mod."github.com/x448/float16"] version = "v0.8.4" hash = "sha256-VKzMTMS9pIB/cwe17xPftCSK9Mf4Y6EuBEJlB4by5mE=" |
