Skip to content

Commit

Permalink
fix(gnolang): ensure complete Uverse initialization (#2997)
Browse files Browse the repository at this point in the history
Fixes #2067.

UverseNode now distinguishes when it's uninitialized, initializing and
initialized. In combination with calling Uverse() at init, we make sure
that after package initialization we always have the same result from
Uverse() and UverseNode() and we don't have issues like those pointed
out in #2067.
  • Loading branch information
thehowl authored Oct 25, 2024
1 parent a478348 commit 0b2c67e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
12 changes: 0 additions & 12 deletions gnovm/pkg/gnolang/gno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,18 +404,6 @@ func BenchmarkBenchdata(b *testing.B) {
name += "_param:" + param
}
b.Run(name, func(b *testing.B) {
if strings.HasPrefix(name, "matrix.gno_param") {
// CGO_ENABLED=0 go test -bench . -benchmem ./... -short -run=^$ -cpu 1,2 -count=1 ./...
// That is not just exposing test and benchmark traces as output, but these benchmarks are failing
// making the output unparseable:
/*
BenchmarkBenchdata/matrix.gno_param:3 panic: runtime error: index out of range [31] with length 25 [recovered]
panic: runtime error: index out of range [31] with length 25:
...
*/
b.Skip("it panics causing an error when parsing benchmark results")
}

// Gen template with N and param.
var buf bytes.Buffer
require.NoError(b, tpl.Execute(&buf, bdataParams{
Expand Down
6 changes: 1 addition & 5 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,12 +734,8 @@ func preprocess1(store Store, ctx BlockNode, n Node) Node {
// TRANS_BLOCK -----------------------
case *FuncDecl:
// retrieve cached function type.
// the type and receiver are already set in predefineNow.
ft := getType(&n.Type).(*FuncType)
if n.IsMethod {
// recv/type set @ predefineNow().
} else {
// type set @ predefineNow().
}

// push func body block.
pushInitBlock(n, &last, &stack)
Expand Down
47 changes: 38 additions & 9 deletions gnovm/pkg/gnolang/uverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,55 @@ var gStringerType = &DeclaredType{
var (
uverseNode *PackageNode
uverseValue *PackageValue
uverseInit = uverseUninitialized
)

const (
uverseUninitialized = iota
uverseInitializing
uverseInitialized
)

func init() {
// Call Uverse() so we initialize the Uverse node ahead of any calls to the package.
Uverse()
}

const uversePkgPath = ".uverse"

// Always returns a new copy from the latest state of source.
// UverseNode returns the uverse PackageValue.
// If called while initializing the UverseNode itself, it will return an empty
// PackageValue.
func Uverse() *PackageValue {
if uverseValue == nil {
pn := UverseNode()
uverseValue = pn.NewPackage()
switch uverseInit {
case uverseUninitialized:
uverseInit = uverseInitializing
makeUverseNode()
uverseInit = uverseInitialized
case uverseInitializing:
return &PackageValue{}
}

return uverseValue
}

// Always returns the same instance with possibly differing completeness.
// UverseNode returns the uverse PackageNode.
// If called while initializing the UverseNode itself, it will return an empty
// PackageNode.
func UverseNode() *PackageNode {
// Global is singleton.
if uverseNode != nil {
return uverseNode
switch uverseInit {
case uverseUninitialized:
uverseInit = uverseInitializing
makeUverseNode()
uverseInit = uverseInitialized
case uverseInitializing:
return &PackageNode{}
}

return uverseNode
}

func makeUverseNode() {
// NOTE: uverse node is hidden, thus the leading dot in pkgPath=".uverse".
uverseNode = NewPackageNode("uverse", uversePkgPath, nil)

Expand Down Expand Up @@ -1017,7 +1046,7 @@ func UverseNode() *PackageNode {
m.Exceptions = nil
},
)
return uverseNode
uverseValue = uverseNode.NewPackage()
}

func copyDataToList(dst []TypedValue, data []byte, et Type) {
Expand Down

1 comment on commit 0b2c67e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Go Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.20.

Benchmark suite Current: 0b2c67e Previous: 287c22e Ratio
BenchmarkBinary/EmptyStruct:encode 480 ns/op 96 B/op 2 allocs/op 286.9 ns/op 96 B/op 2 allocs/op 1.67
BenchmarkBinary/EmptyStruct:encode - ns/op 480 ns/op 286.9 ns/op 1.67
BenchmarkBinary/EmptyStruct:decode 270.2 ns/op 0 B/op 0 allocs/op 133.3 ns/op 0 B/op 0 allocs/op 2.03
BenchmarkBinary/EmptyStruct:decode - ns/op 270.2 ns/op 133.3 ns/op 2.03
BenchmarkBinary/ShortArraysStruct:encode 756.7 ns/op 192 B/op 4 allocs/op 616.5 ns/op 192 B/op 4 allocs/op 1.23
BenchmarkBinary/ShortArraysStruct:encode - ns/op 756.7 ns/op 616.5 ns/op 1.23
BenchmarkBinary/ShortArraysStruct:decode 399.7 ns/op 0 B/op 0 allocs/op 215.1 ns/op 0 B/op 0 allocs/op 1.86
BenchmarkBinary/ShortArraysStruct:decode - ns/op 399.7 ns/op 215.1 ns/op 1.86
BenchmarkBcryptGenerateFromPassword/benchmark-security-param 63647235 ns/op 5130 B/op 9 allocs/op 31901806 ns/op 5125 B/op 9 allocs/op 2.00
BenchmarkBcryptGenerateFromPassword/benchmark-security-param - ns/op 63647235 ns/op 31901806 ns/op 2.00
BenchmarkBcryptGenerateFromPassword/benchmark-security-param 127267147 ns/op 5139 B/op 9 allocs/op 31901806 ns/op 5125 B/op 9 allocs/op 3.99
BenchmarkBcryptGenerateFromPassword/benchmark-security-param - ns/op 127267147 ns/op 31901806 ns/op 3.99
BenchmarkBcryptGenerateFromPassword/benchmark-security-param 254368631 ns/op 5158 B/op 9 allocs/op 31901806 ns/op 5125 B/op 9 allocs/op 7.97
BenchmarkBcryptGenerateFromPassword/benchmark-security-param - ns/op 254368631 ns/op 31901806 ns/op 7.97
BenchmarkBcryptGenerateFromPassword/benchmark-security-param 508759844 ns/op 5196 B/op 10 allocs/op 31901806 ns/op 5125 B/op 9 allocs/op 15.95
BenchmarkBcryptGenerateFromPassword/benchmark-security-param - ns/op 508759844 ns/op 31901806 ns/op 15.95
BenchmarkBcryptGenerateFromPassword/benchmark-security-param 1017185385 ns/op 5736 B/op 15 allocs/op 31901806 ns/op 5125 B/op 9 allocs/op 31.88
BenchmarkBcryptGenerateFromPassword/benchmark-security-param - ns/op 1017185385 ns/op 31901806 ns/op 31.88
BenchmarkBcryptGenerateFromPassword/benchmark-security-param - allocs/op 15 allocs/op 9 allocs/op 1.67
BenchmarkBcryptGenerateFromPassword/benchmark-security-param 2034651179 ns/op 5528 B/op 13 allocs/op 31901806 ns/op 5125 B/op 9 allocs/op 63.78
BenchmarkBcryptGenerateFromPassword/benchmark-security-param - ns/op 2034651179 ns/op 31901806 ns/op 63.78
BenchmarkBcryptGenerateFromPassword/benchmark-security-param - allocs/op 13 allocs/op 9 allocs/op 1.44
BenchmarkSigning 83525 ns/op 1856 B/op 36 allocs/op 25716 ns/op 64 B/op 1 allocs/op 3.25
BenchmarkSigning - ns/op 83525 ns/op 25716 ns/op 3.25
BenchmarkSigning - B/op 1856 B/op 64 B/op 29
BenchmarkSigning - allocs/op 36 allocs/op 1 allocs/op 36
BenchmarkSigning 84229 ns/op 1856 B/op 36 allocs/op 25716 ns/op 64 B/op 1 allocs/op 3.28
BenchmarkSigning - ns/op 84229 ns/op 25716 ns/op 3.28
BenchmarkSigning - B/op 1856 B/op 64 B/op 29
BenchmarkSigning - allocs/op 36 allocs/op 1 allocs/op 36
BenchmarkVerification 165923 ns/op 864 B/op 19 allocs/op 61337 ns/op 0 B/op 0 allocs/op 2.71
BenchmarkVerification - ns/op 165923 ns/op 61337 ns/op 2.71
BenchmarkVerification - B/op 864 B/op 0 B/op +∞
BenchmarkVerification - allocs/op 19 allocs/op 0 allocs/op +∞
BenchmarkVerification 171044 ns/op 864 B/op 19 allocs/op 61337 ns/op 0 B/op 0 allocs/op 2.79
BenchmarkVerification - ns/op 171044 ns/op 61337 ns/op 2.79
BenchmarkVerification - B/op 864 B/op 0 B/op +∞
BenchmarkVerification - allocs/op 19 allocs/op 0 allocs/op +∞
BenchmarkImmutableAvlTreeMemDB 4090475 ns/op 1001607 B/op 20374 allocs/op 3220999 ns/op 836512 B/op 16992 allocs/op 1.27
BenchmarkImmutableAvlTreeMemDB - ns/op 4090475 ns/op 3220999 ns/op 1.27
BenchmarkRandomBytes/random 68.42 ns/op 16 B/op 1 allocs/op 32.77 ns/op 4 B/op 1 allocs/op 2.09
BenchmarkRandomBytes/random - ns/op 68.42 ns/op 32.77 ns/op 2.09
BenchmarkRandomBytes/random - B/op 16 B/op 4 B/op 4
BenchmarkRandomBytes/random 105.7 ns/op 32 B/op 1 allocs/op 32.77 ns/op 4 B/op 1 allocs/op 3.23
BenchmarkRandomBytes/random - ns/op 105.7 ns/op 32.77 ns/op 3.23
BenchmarkRandomBytes/random - B/op 32 B/op 4 B/op 8
BenchmarkRandomBytes/random 265.3 ns/op 112 B/op 1 allocs/op 32.77 ns/op 4 B/op 1 allocs/op 8.10
BenchmarkRandomBytes/random - ns/op 265.3 ns/op 32.77 ns/op 8.10
BenchmarkRandomBytes/random - B/op 112 B/op 4 B/op 28
BenchmarkRandomBytes/random 2310 ns/op 1024 B/op 1 allocs/op 32.77 ns/op 4 B/op 1 allocs/op 70.49
BenchmarkRandomBytes/random - ns/op 2310 ns/op 32.77 ns/op 70.49
BenchmarkRandomBytes/random - B/op 1024 B/op 4 B/op 256
BenchmarkSmall/boltdb-1000-100-16-40/update 1332282 ns/op 40798 B/op 372 allocs/op 981912 ns/op 37696 B/op 375 allocs/op 1.36
BenchmarkSmall/boltdb-1000-100-16-40/update - ns/op 1332282 ns/op 981912 ns/op 1.36
BenchmarkSmall/boltdb-1000-100-16-40/block - B/op 5606453 B/op 4631094 B/op 1.21
BenchmarkSmall/memdb-1000-100-16-40/block 16151357 ns/op 9392961 B/op 171570 allocs/op 12241675 ns/op 6587137 B/op 116865 allocs/op 1.32
BenchmarkSmall/memdb-1000-100-16-40/block - ns/op 16151357 ns/op 12241675 ns/op 1.32
BenchmarkSmall/memdb-1000-100-16-40/block - B/op 9392961 B/op 6587137 B/op 1.43
BenchmarkSmall/memdb-1000-100-16-40/block - allocs/op 171570 allocs/op 116865 allocs/op 1.47
BenchmarkMedium/boltdb-100000-100-16-40/update 7155641 ns/op 136720 B/op 1056 allocs/op 5410085 ns/op 100865 B/op 854 allocs/op 1.32
BenchmarkMedium/boltdb-100000-100-16-40/update - ns/op 7155641 ns/op 5410085 ns/op 1.32
BenchmarkMedium/boltdb-100000-100-16-40/update - B/op 136720 B/op 100865 B/op 1.36
BenchmarkMedium/boltdb-100000-100-16-40/update - allocs/op 1056 allocs/op 854 allocs/op 1.24
BenchmarkMedium/memdb-100000-100-16-40/update 1290219 ns/op 393269 B/op 7797 allocs/op 997219 ns/op 254581 B/op 4910 allocs/op 1.29
BenchmarkMedium/memdb-100000-100-16-40/update - ns/op 1290219 ns/op 997219 ns/op 1.29
BenchmarkMedium/memdb-100000-100-16-40/update - B/op 393269 B/op 254581 B/op 1.54
BenchmarkMedium/memdb-100000-100-16-40/update - allocs/op 7797 allocs/op 4910 allocs/op 1.59
BenchmarkLevelDBBatchSizes/goleveldb-100000-400-16-40/update - B/op 48253 B/op 39101 B/op 1.23
BenchmarkLevelDBBatchSizes/goleveldb-100000-400-16-40/update - allocs/op 583 allocs/op 458 allocs/op 1.27
BenchmarkLevelDBBatchSizes/goleveldb-100000-2000-16-40/block - B/op 96313492 B/op 79373362 B/op 1.21
BenchmarkHash/ripemd160 2841 ns/op 25 B/op 1 allocs/op 703.5 ns/op 25 B/op 1 allocs/op 4.04
BenchmarkHash/ripemd160 - ns/op 2841 ns/op 703.5 ns/op 4.04
BenchmarkHash/sha2-256 523.3 ns/op 33 B/op 1 allocs/op 170.9 ns/op 33 B/op 1 allocs/op 3.06
BenchmarkHash/sha2-256 - ns/op 523.3 ns/op 170.9 ns/op 3.06
BenchmarkHash/sha3-256 1840 ns/op 33 B/op 1 allocs/op 713.6 ns/op 33 B/op 1 allocs/op 2.58
BenchmarkHash/sha3-256 - ns/op 1840 ns/op 713.6 ns/op 2.58
BenchmarkWriteSecretConnection 6409 ns/op 0 B/op 0 allocs/op 4090 ns/op 0 B/op 0 allocs/op 1.57
BenchmarkWriteSecretConnection - ns/op 6409 ns/op 4090 ns/op 1.57
BenchmarkReadSecretConnection 3666 ns/op 0 B/op 0 allocs/op 2360 ns/op 0 B/op 0 allocs/op 1.55
BenchmarkReadSecretConnection - ns/op 3666 ns/op 2360 ns/op 1.55

This comment was automatically generated by workflow using github-action-benchmark.

CC: @ajnavarro @thehowl @zivkovicmilos

Please sign in to comment.