-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core/state: reduce memory allocation in IsAddressBlacklisted (#460)
* core/state: add GetLocMappingAtKey test * core/state: reduce memory allocation in GetLocMappingAtKey > go test -bench=BenchmarkGetLocMappingAtKey -benchtime=5s Before goos: linux goarch: amd64 pkg: github.com/ethereum/go-ethereum/core/state cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz BenchmarkGetLocMappingAtKey-8 8499260 877.7 ns/op 664 B/op 9 allocs/op After goos: linux goarch: amd64 pkg: github.com/ethereum/go-ethereum/core/state cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz BenchmarkGetLocMappingAtKey-8 9745719 592.2 ns/op 584 B/op 5 allocs/op * core/state: add IsAddressBlacklisted test * core/state: reduce memory allocation in IsAddressBlacklisted > go test -bench=BenchmarkIsAddressBlacklisted -benchtime=5s Before goos: linux goarch: amd64 pkg: github.com/ethereum/go-ethereum/core/state cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz BenchmarkIsAddressBlacklisted-8 3349110 2432 ns/op 480 B/op 11 allocs/op After goos: linux goarch: amd64 pkg: github.com/ethereum/go-ethereum/core/state cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz BenchmarkIsAddressBlacklisted-8 3863929 2245 ns/op 352 B/op 9 allocs/op * core/state: reduce memory allocation in GetLocSimpleVariable
- Loading branch information
Showing
4 changed files
with
104 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package state | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
func TestGetLocSimpleVariable(t *testing.T) { | ||
hash := GetLocSimpleVariable(12) | ||
expect := common.BigToHash(big.NewInt(12)) | ||
|
||
if hash != expect { | ||
t.Fatalf("Hash mismatches, got %s expect %s", hash, expect) | ||
} | ||
} | ||
|
||
func TestGetLocMappingAtKey(t *testing.T) { | ||
hash := GetLocMappingAtKey(common.BigToHash(big.NewInt(10)), 12) | ||
|
||
expect := common.HexToHash("0x9e6c92d7be355807bd948171438a5e65aaf9e4c36f1405c1b9ca25d27c4ea3a0") | ||
if hash != expect { | ||
t.Fatalf("Hash mismatches, got %s expect %s", hash, expect) | ||
} | ||
} | ||
|
||
func BenchmarkGetLocMappingAtKey(b *testing.B) { | ||
b.ReportAllocs() | ||
for i := 0; i < b.N; i++ { | ||
GetLocMappingAtKey(common.BigToHash(big.NewInt(10)), 12) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package state | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/rawdb" | ||
) | ||
|
||
func TestIsAddressBlacklisted(t *testing.T) { | ||
blackListContract := common.Address{0x11} | ||
|
||
statedb, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil) | ||
|
||
blacklistedAddress := common.BigToAddress(common.Big3) | ||
// Blacklist address 0x000..0003 | ||
statedb.SetState( | ||
blackListContract, | ||
common.HexToHash("0x7dfe757ecd65cbd7922a9c0161e935dd7fdbcc0e999689c7d31633896b1fc60b"), | ||
common.BigToHash(common.Big1), | ||
) | ||
if !IsAddressBlacklisted(statedb, &blackListContract, &blacklistedAddress) { | ||
t.Fatalf("Expect address %s to be blacklisted", blacklistedAddress.String()) | ||
} | ||
|
||
notBlacklistedAddress := common.BigToAddress(big.NewInt(10)) | ||
if IsAddressBlacklisted(statedb, &blackListContract, ¬BlacklistedAddress) { | ||
t.Fatalf("Expect address %s to be not blacklisted", notBlacklistedAddress.String()) | ||
} | ||
|
||
statedb.SetState(blackListContract, common.BigToHash(common.Big2), common.BigToHash(common.Big1)) | ||
if IsAddressBlacklisted(statedb, &blackListContract, &blacklistedAddress) { | ||
t.Fatalf("Expect address %s to be not blacklisted", blacklistedAddress.String()) | ||
} | ||
} | ||
|
||
func BenchmarkIsAddressBlacklisted(b *testing.B) { | ||
statedb, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil) | ||
blackListContract := common.Address{0x11} | ||
|
||
queriedAddress := common.BigToAddress(common.Big3) | ||
|
||
b.ResetTimer() | ||
b.ReportAllocs() | ||
for i := 0; i < b.N; i++ { | ||
IsAddressBlacklisted(statedb, &blackListContract, &queriedAddress) | ||
} | ||
} |