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: use swiss map for the object cache #677

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
29 changes: 29 additions & 0 deletions gnovm/pkg/gnolang/gno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gnolang
import (
"bytes"
"fmt"
"github.com/dolthub/swiss"
"reflect"
"testing"
"unsafe"
Expand All @@ -19,6 +20,34 @@ func TestRunEmptyMain(t *testing.T) {
m.RunMain()
}

func BenchmarkSwissMap(b *testing.B) {
m := swiss.NewMap[string, string](10000)
for i := 0; i < 100000; i++ {
str := fmt.Sprintf("%v", i)
m.Put(str, str)
}

for i := 0; i < b.N; i++ {
ind := i % 100000
str := fmt.Sprintf("%v", ind)
m.Get(str)
}
}

func BenchmarkMap(b *testing.B) {
m := map[string]string{}
for i := 0; i < 100000; i++ {
str := fmt.Sprintf("%v", i)
m[str] = str
}

for i := 0; i < b.N; i++ {
ind := i % 100000
str := fmt.Sprintf("%v", ind)
_ = m[str]
}
}

// run main() with a for loop.
func TestRunLoopyMain(t *testing.T) {
m := NewMachine("test", nil)
Expand Down
32 changes: 17 additions & 15 deletions gnovm/pkg/gnolang/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"strings"

"github.com/dolthub/swiss"
"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/store"
Expand Down Expand Up @@ -65,7 +66,7 @@ type Store interface {
type defaultStore struct {
alloc *Allocator // for accounting for cached items
pkgGetter PackageGetter // non-realm packages
cacheObjects map[ObjectID]Object
cacheObjects *swiss.Map[ObjectID, Object]
cacheTypes map[TypeID]Type
cacheNodes map[Location]BlockNode
cacheNativeTypes map[reflect.Type]Type // go spec: reflect.Type are comparable
Expand All @@ -84,7 +85,7 @@ func NewStore(alloc *Allocator, baseStore, iavlStore store.Store) *defaultStore
ds := &defaultStore{
alloc: alloc,
pkgGetter: nil,
cacheObjects: make(map[ObjectID]Object),
cacheObjects: swiss.NewMap[ObjectID, Object](100),
cacheTypes: make(map[TypeID]Type),
cacheNodes: make(map[Location]BlockNode),
cacheNativeTypes: make(map[reflect.Type]Type),
Expand Down Expand Up @@ -118,7 +119,7 @@ func (ds *defaultStore) GetPackage(pkgPath string, isImport bool) *PackageValue
}
// first, check cache.
oid := ObjectIDFromPkgPath(pkgPath)
if oo, exists := ds.cacheObjects[oid]; exists {
if oo, exists := ds.cacheObjects.Get(oid); exists {
pv := oo.(*PackageValue)
return pv
}
Expand Down Expand Up @@ -171,7 +172,7 @@ func (ds *defaultStore) GetPackage(pkgPath string, isImport bool) *PackageValue
// Realm values obtained this way
// will get written elsewhere
// later.
ds.cacheObjects[oid] = pv
ds.cacheObjects.Put(oid, pv)
// inject natives after init.
if ds.pkgInjector != nil {
if pn.HasAttribute(ATTR_INJECTED) {
Expand Down Expand Up @@ -203,10 +204,10 @@ func (ds *defaultStore) GetPackage(pkgPath string, isImport bool) *PackageValue
// Used to set throwaway packages.
func (ds *defaultStore) SetCachePackage(pv *PackageValue) {
oid := ObjectIDFromPkgPath(pv.PkgPath)
if _, exists := ds.cacheObjects[oid]; exists {
if exists := ds.cacheObjects.Has(oid); exists {
panic(fmt.Sprintf("package %s already exists in cache", pv.PkgPath))
}
ds.cacheObjects[oid] = pv
ds.cacheObjects.Put(oid, pv)
}

// Some atomic operation.
Expand Down Expand Up @@ -250,7 +251,7 @@ func (ds *defaultStore) GetObject(oid ObjectID) Object {

func (ds *defaultStore) GetObjectSafe(oid ObjectID) Object {
// check cache.
if oo, exists := ds.cacheObjects[oid]; exists {
if oo, exists := ds.cacheObjects.Get(oid); exists {
return oo
}
// check baseStore.
Expand Down Expand Up @@ -285,7 +286,7 @@ func (ds *defaultStore) loadObjectSafe(oid ObjectID) Object {
}
}
oo.SetHash(ValueHash{NewHashlet(hash)})
ds.cacheObjects[oid] = oo
ds.cacheObjects.Put(oid, oo)
_ = fillTypesOfValue(ds, oo)
return oo
}
Expand Down Expand Up @@ -319,15 +320,15 @@ func (ds *defaultStore) SetObject(oo Object) {
if oid.IsZero() {
panic("object id cannot be zero")
}
if oo2, exists := ds.cacheObjects[oid]; exists {
if oo2, exists := ds.cacheObjects.Get(oid); exists {
if oo != oo2 {
panic(fmt.Sprintf(
"duplicate object: set %s (oid: %s) but %s (oid %s) already exists",
oo.String(), oid.String(), oo2.String(), oo2.GetObjectID().String()))
}
}
}
ds.cacheObjects[oid] = oo
ds.cacheObjects.Put(oid, oo)
// make store op log entry
if ds.opslog != nil {
var op StoreOpType
Expand All @@ -351,7 +352,8 @@ func (ds *defaultStore) SetObject(oo Object) {
func (ds *defaultStore) DelObject(oo Object) {
oid := oo.GetObjectID()
// delete from cache.
delete(ds.cacheObjects, oid)
ds.cacheObjects.Delete(oid)

// delete from backend.
if ds.baseStore != nil {
key := backendObjectKey(oid)
Expand Down Expand Up @@ -582,8 +584,8 @@ func (ds *defaultStore) IterMemPackage() <-chan *std.MemPackage {
// It also sets a new allocator.
func (ds *defaultStore) ClearObjectCache() {
ds.alloc.Reset()
ds.cacheObjects = make(map[ObjectID]Object) // new cache.
ds.opslog = nil // new ops log.
ds.cacheObjects = swiss.NewMap[ObjectID, Object](100) // new cache.
ds.opslog = nil // new ops log.
if len(ds.current) > 0 {
ds.current = make(map[string]struct{})
}
Expand All @@ -596,7 +598,7 @@ func (ds *defaultStore) Fork() Store {
ds2 := &defaultStore{
alloc: ds.alloc.Fork().Reset(),
pkgGetter: ds.pkgGetter,
cacheObjects: make(map[ObjectID]Object), // new cache.
cacheObjects: swiss.NewMap[ObjectID, Object](100), // new cache.
cacheTypes: ds.cacheTypes,
cacheNodes: ds.cacheNodes,
cacheNativeTypes: ds.cacheNativeTypes,
Expand Down Expand Up @@ -695,7 +697,7 @@ func (ds *defaultStore) LogSwitchRealm(rlmpath string) {
}

func (ds *defaultStore) ClearCache() {
ds.cacheObjects = make(map[ObjectID]Object)
ds.cacheObjects = swiss.NewMap[ObjectID, Object](100)
ds.cacheTypes = make(map[TypeID]Type)
ds.cacheNodes = make(map[Location]BlockNode)
ds.cacheNativeTypes = make(map[reflect.Type]Type)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ require (
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dolthub/maphash v0.0.0-20221220182448-74e1e1ea1577 // indirect
github.com/dolthub/swiss v0.1.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c // indirect
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.