Skip to content

Commit

Permalink
chore(trie): add trie heap usage workflow step (#2961)
Browse files Browse the repository at this point in the history
- Fix `mutateTrieLeavesAtPrefix` (shame on previous me!)
- Fix `mutateTrieLeavesAtPrefix` to work with empty values
- Tigthen ratio brackets to newer values
- Add trie memory workflow step
  • Loading branch information
qdm12 authored Jan 26, 2023
1 parent 43e7c98 commit d7e1177
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ jobs:
- name: Run unit tests
run: go test -coverprofile=coverage.out -covermode=atomic -timeout=45m ./...

- name: Trie memory test
run: |
sed -i 's/const skip = true/const skip = false/g' ./lib/trie/mem_test.go
go test -run ^Test_Trie_MemoryUsage$ ./lib/trie
sed -i 's/const skip = false/const skip = true/g' ./lib/trie/mem_test.go
- name: Test State - Race
run: make test-state-race

Expand Down
28 changes: 16 additions & 12 deletions lib/trie/mem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func Test_Trie_MemoryUsage(t *testing.T) {
// Check heap memory usage - it should be 2X
filledTrieHeap := getHeapUsage()
ratio := getApproximateRatio(halfFilledTrieHeap, filledTrieHeap)
assert.Greater(t, ratio, 1.5)
assert.Less(t, ratio, 2.1)
assert.Greater(t, ratio, 1.6)
assert.Less(t, ratio, 1.7)

// Snapshot the trie
triesMap["second"] = triesMap["first"].Snapshot()
Expand All @@ -55,17 +55,17 @@ func Test_Trie_MemoryUsage(t *testing.T) {
// Check heap memory usage - it should be 3X
halfMutatedTrieHeap := getHeapUsage()
ratio = getApproximateRatio(halfFilledTrieHeap, halfMutatedTrieHeap)
assert.Greater(t, ratio, 2.0)
assert.Less(t, ratio, 3.1)
assert.Greater(t, ratio, 2.2)
assert.Less(t, ratio, 2.4)

// Remove the older trie from our reference
delete(triesMap, "first")

// Check heap memory usage - it should be 2X
prunedTrieHeap := getHeapUsage()
ratio = getApproximateRatio(halfFilledTrieHeap, prunedTrieHeap)
assert.Greater(t, ratio, 1.5)
assert.Less(t, ratio, 2.1)
assert.Greater(t, ratio, 1.6)
assert.Less(t, ratio, 1.8)

// Dummy calls - has to be after prunedTrieHeap for
// GC to keep them
Expand Down Expand Up @@ -102,13 +102,17 @@ func mutateTrieLeavesAtPrefix(trie *Trie,
for keyString, value := range originalKV {
key := append(prefix, []byte(keyString)...) //skipcq: CRT-D0001

// Reverse value byte slice
newValue := make([]byte, len(value))
copy(newValue, value)
for i, j := 0, len(newValue)-1; i < j; i, j = i+1, j-1 {
newValue[i], newValue[j] = newValue[j], newValue[i]
var newValue []byte
if len(value) == 0 {
newValue = []byte{1}
} else {
newValue = make([]byte, len(value))
copy(newValue, value)
for i := range newValue {
newValue[i]++
}
}

trie.Put(key, value)
trie.Put(key, newValue)
}
}

0 comments on commit d7e1177

Please sign in to comment.