Skip to content

Commit

Permalink
perf(gnovm): cache PkgIDFromPkgPath for higher performance
Browse files Browse the repository at this point in the history
This change comes from noticing that PkgIDFromPkgPath is very
heavily invoked within the VM yet its results with the same inputs
produced deterministic results aka SHA256(path)[:20]
Previously just spinning up the VM would take 80 seconds, with this
change that's shaved by ~8-10 seconds down and with repeatable and
visible results exhibited through profiles:

* Before
```shell
(pprof) list PkgIDFromPkgPath
Total: 100.94s
ROUTINE ======================== github.com/gnolang/gno/gnovm/pkg/gnolang.PkgIDFromPkgPath in $PATH
     220ms      9.26s (flat, cum)  9.17% of Total
         .          .     74:func PkgIDFromPkgPath(path string) PkgID {
     220ms      9.26s     75:	return PkgID{HashBytes([]byte(path))}
         .          .     76:}
         .          .     77:
         .          .     78:// Returns the ObjectID of the PackageValue associated with path.
         .          .     79:func ObjectIDFromPkgPath(path string) ObjectID {
         .          .     80:	pkgID := PkgIDFromPkgPath(path)
```

* After
```shell
(pprof) list PkgIDFromPkgPath
Total: 93.22s
ROUTINE ======================== github.com/gnolang/gno/gnovm/pkg/gnolang.PkgIDFromPkgPath in $PATH
     210ms      1.55s (flat, cum)  1.66% of Total
      50ms       50ms     78:func PkgIDFromPkgPath(path string) PkgID {
         .      490ms     79:	pkgIDMu.Lock()
      10ms       10ms     80:	defer pkgIDMu.Unlock()
         .          .     81:
      10ms      730ms     82:	pkgID, ok := pkgIDFromPkgPathCache[path]
         .          .     83:	if !ok {
         .          .     84:		pkgID = new(PkgID)
         .          .     85:		*pkgID = PkgID{HashBytes([]byte(path))}
         .          .     86:		pkgIDFromPkgPathCache[path] = pkgID
         .          .     87:	}
     140ms      270ms     88:	return *pkgID
         .          .     89:}
         .          .     90:
         .          .     91:// Returns the ObjectID of the PackageValue associated with path.
         .          .     92:func ObjectIDFromPkgPath(path string) ObjectID {
         .          .     93:	pkgID := PkgIDFromPkgPath(path)
```

Fixes #3423
  • Loading branch information
odeke-em committed Dec 28, 2024
1 parent 597f3f5 commit 1a939cf
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion gnovm/pkg/gnolang/realm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"reflect"
"strings"
"sync"

bm "github.com/gnolang/gno/gnovm/pkg/benchops"
)
Expand Down Expand Up @@ -71,8 +72,20 @@ func (pid PkgID) Bytes() []byte {
return pid.Hashlet[:]
}

var pkgIDMu sync.Mutex // pkgIDMu protects the shared cache.
var pkgIDFromPkgPathCache = make(map[string]*PkgID)

func PkgIDFromPkgPath(path string) PkgID {
return PkgID{HashBytes([]byte(path))}
pkgIDMu.Lock()
defer pkgIDMu.Unlock()

pkgID, ok := pkgIDFromPkgPathCache[path]
if !ok {
pkgID = new(PkgID)
*pkgID = PkgID{HashBytes([]byte(path))}
pkgIDFromPkgPathCache[path] = pkgID
}
return *pkgID
}

// Returns the ObjectID of the PackageValue associated with path.
Expand Down

0 comments on commit 1a939cf

Please sign in to comment.