Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(gnolang): make print+println much more efficient #3952

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions gnovm/pkg/gnolang/native_perf_and_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package gnolang

import (
"bytes"
"testing"

"github.com/google/go-cmp/cmp"

"github.com/gnolang/gno/gnovm"
"github.com/gnolang/gno/tm2/pkg/db/memdb"
"github.com/gnolang/gno/tm2/pkg/store/dbadapter"
"github.com/gnolang/gno/tm2/pkg/store/iavl"
stypes "github.com/gnolang/gno/tm2/pkg/store/types"
)

var pSink any = nil

func BenchmarkGnoPrintln(b *testing.B) {
var buf bytes.Buffer
db := memdb.NewMemDB()
baseStore := dbadapter.StoreConstructor(db, stypes.StoreOptions{})
iavlStore := iavl.StoreConstructor(db, stypes.StoreOptions{})
store := NewStore(nil, baseStore, iavlStore)

m := NewMachineWithOptions(MachineOptions{
Output: &buf,
Store: store,
})

program := `package p
func main() {
for i := 0; i < 1000; i++ {
println("abcdeffffffffffffffff1222 11111 11111")
}
}`
m.RunMemPackage(&gnovm.MemPackage{
Name: "p",
Path: "p",
Files: []*gnovm.MemFile{
{Name: "a.gno", Body: program},
},
}, false)

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
buf.Reset()
m.RunStatement(S(Call(Nx("main"))))
pSink = buf.String()
}

if pSink == nil {
b.Fatal("Benchmark did not run!")
}
pSink = nil
}

func TestGnoPrintAndPrintln(t *testing.T) {
tests := []struct {
name string
srcArgs string
want string
}{
{
"print with no args",
"print()",
"",
},
{
"print with 1 arg",
`print("1")`,
"1",
},
{
"print with 2 args",
`print("1", 2)`,
"1 2",
},
{
"print with 3 args",
`print("1", 2, "*")`,
"1 2 *",
},
{
"print with own spaces",
`print("1 ", 2, "*")`,
"1 2 *",
},
{
"println with no args",
"println()",
"\n",
},
{
"print with 1 arg",
`println("1")`,
"1\n",
},
{
"println with 2 args",
`println("1", 2)`,
"1 2\n",
},
{
"println with 3 args",
`println("1", 2, "*")`,
"1 2 *\n",
},
{
"println with own spaces",
`println("1 ", 2, "*")`,
"1 2 *\n",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
db := memdb.NewMemDB()
baseStore := dbadapter.StoreConstructor(db, stypes.StoreOptions{})
iavlStore := iavl.StoreConstructor(db, stypes.StoreOptions{})
store := NewStore(nil, baseStore, iavlStore)

m := NewMachineWithOptions(MachineOptions{
Output: &buf,
Store: store,
})

program := `package p
func main() {` + tt.srcArgs + "\n}"
m.RunMemPackage(&gnovm.MemPackage{
Name: "p",
Path: "p",
Files: []*gnovm.MemFile{
{Name: "a.gno", Body: program},
},
}, false)

buf.Reset()
m.RunStatement(S(Call(Nx("main"))))
got := buf.String()
if diff := cmp.Diff(got, tt.want); diff != "" {
t.Fatalf("Mismatched output: got - want +\n%s", diff)
}
})
}
}
60 changes: 45 additions & 15 deletions gnovm/pkg/gnolang/uverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"fmt"
"io"
"strings"

bm "github.com/gnolang/gno/gnovm/pkg/benchops"
Expand Down Expand Up @@ -718,16 +719,32 @@
arg0 := m.LastBlock().GetParams1()
xv := arg0
xvl := xv.TV.GetLength()
ss := make([]string, xvl)
for i := 0; i < xvl; i++ {
ev := xv.TV.GetPointerAtIndexInt(m.Store, i).Deref()
ss[i] = ev.Sprint(m)
if xvl == 0 {
// Mimick Go in which invoking print() does nothing.
return
}
rs := strings.Join(ss, " ")
if debug {

switch debug {
case true:
ss := make([]string, xvl)
for i := 0; i < xvl; i++ {
ev := xv.TV.GetPointerAtIndexInt(m.Store, i).Deref()
ss[i] = ev.Sprint(m)
}
rs := strings.Join(ss, " ")

Check warning on line 734 in gnovm/pkg/gnolang/uverse.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/uverse.go#L728-L734

Added lines #L728 - L734 were not covered by tests
print(rs)
io.WriteString(m.Output, rs)

Check warning on line 736 in gnovm/pkg/gnolang/uverse.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/uverse.go#L736

Added line #L736 was not covered by tests

default:
nMax := xvl - 1
for i := 0; i < xvl; i++ {
ev := xv.TV.GetPointerAtIndexInt(m.Store, i).Deref()
io.WriteString(m.Output, ev.Sprint(m))
if i < nMax { // Not the last item.
io.WriteString(m.Output, " ")
}
}
}
m.Output.Write([]byte(rs))
},
)
defNative("println",
Expand All @@ -739,16 +756,29 @@
arg0 := m.LastBlock().GetParams1()
xv := arg0
xvl := xv.TV.GetLength()
ss := make([]string, xvl)
for i := 0; i < xvl; i++ {
ev := xv.TV.GetPointerAtIndexInt(m.Store, i).Deref()
ss[i] = ev.Sprint(m)
}
rs := strings.Join(ss, " ") + "\n"
if debug {

switch debug {
case true:
ss := make([]string, xvl)
for i := 0; i < xvl; i++ {
ev := xv.TV.GetPointerAtIndexInt(m.Store, i).Deref()
ss[i] = ev.Sprint(m)
}
rs := strings.Join(ss, " ") + "\n"

Check warning on line 767 in gnovm/pkg/gnolang/uverse.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/uverse.go#L761-L767

Added lines #L761 - L767 were not covered by tests
println("DEBUG/stdout: " + rs)
io.WriteString(m.Output, rs)

Check warning on line 769 in gnovm/pkg/gnolang/uverse.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/uverse.go#L769

Added line #L769 was not covered by tests

default:
nMax := xvl - 1
for i := 0; i < xvl; i++ {
ev := xv.TV.GetPointerAtIndexInt(m.Store, i).Deref()
io.WriteString(m.Output, ev.Sprint(m))
if i < nMax { // Not the last item.
io.WriteString(m.Output, " ")
}
}
io.WriteString(m.Output, "\n")
}
m.Output.Write([]byte(rs))
},
)
defNative("recover",
Expand Down
Loading