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(gnovm/pkg/gnolang): make ReadMemPackageFromList singly use byteslices from os.ReadFile #3599

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
24 changes: 24 additions & 0 deletions gnovm/pkg/gnolang/bench_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gnolang

import (
"path/filepath"
"runtime"
"testing"
)

Expand Down Expand Up @@ -29,3 +31,25 @@ func BenchmarkPkgIDFromPkgPath(b *testing.B) {
}
sink = nil
}

func BenchmarkReadMemPackage(b *testing.B) {
_, currentFile, _, ok := runtime.Caller(0)
_ = ok // Appease golang-ci
rootOfRepo, err := filepath.Abs(filepath.Join(filepath.Dir(currentFile), "..", "..", ".."))
if err != nil {
b.Fatal(err)
}
demoDir := filepath.Join(rootOfRepo, "examples", "gno.land", "p", "demo")
ufmtDir := filepath.Join(demoDir, "ufmt")
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
sink = MustReadMemPackage(ufmtDir, "ufmt")
}

if sink == nil {
b.Fatal("Benchmark did not run!")
}
sink = nil
}
20 changes: 18 additions & 2 deletions gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"regexp"
"strconv"
"strings"
"unsafe"

"github.com/gnolang/gno/gnovm"
"go.uber.org/multierr"
Expand Down Expand Up @@ -1317,7 +1318,10 @@ func ReadMemPackageFromList(list []string, pkgPath string) (*gnovm.MemPackage, e
}
// XXX: should check that all pkg names are the same (else package is invalid)
if pkgName == "" && strings.HasSuffix(fname, ".gno") {
pkgName, err = PackageNameFromFileBody(fname, string(bz))
// The string derived from bz is never modified inside PackageNameFromFileBody
// hence shave these unnecessary allocations by an unsafe
// byteslice->string conversion per https://github.com/gnolang/gno/issues/3598
pkgName, err = PackageNameFromFileBody(fname, unsafeBytesliceToStr(bz))
if err != nil {
return nil, err
}
Expand All @@ -1328,7 +1332,12 @@ func ReadMemPackageFromList(list []string, pkgPath string) (*gnovm.MemPackage, e
memPkg.Files = append(memPkg.Files,
&gnovm.MemFile{
Name: fname,
Body: string(bz),
// The string derived from bz is never modified, hence to shave
// unnecessary allocations, perform this unsafe byteslice->string
// conversion per https://github.com/gnolang/gno/issues/3598
// and the Go garbage collector will knows to garbage collect
// bz when no other object points to that memory.
Body: unsafeBytesliceToStr(bz),
})
}

Expand All @@ -1343,6 +1352,13 @@ func ReadMemPackageFromList(list []string, pkgPath string) (*gnovm.MemPackage, e
return memPkg, nil
}

// unsafeBytesliceToStr helps rid the unnecessary byteslice->string conversion
// in instances where we definitely would not be modifying the input byteslice
// such as in ReadMemPackageFromList.
func unsafeBytesliceToStr(bz []byte) string {
return unsafe.String(&bz[0], len(bz))
}

// MustReadMemPackageFromList is a wrapper around [ReadMemPackageFromList] that panics on error.
func MustReadMemPackageFromList(list []string, pkgPath string) *gnovm.MemPackage {
pkg, err := ReadMemPackageFromList(list, pkgPath)
Expand Down
Loading