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

VKT: Optimizations #147

Closed
wants to merge 7 commits into from
Closed

Conversation

jsign
Copy link
Collaborator

@jsign jsign commented Dec 13, 2022

This PR contains more optimizations apart from the ones in:

The go.mod in this PR uses those branches; we should update the official commits whenever those PRs are merged.

Apart from that, we're doing other optimizations that will be explained in PR comments.

The results are significant comparing beverly-hills vs this PR:

name                               old time/op    new time/op    delta
TriesRandom/VKT/1000_accounts-16     56.8ms ± 3%    22.9ms ± 3%  -59.73%  (p=0.001 n=10+5)
TriesRandom/VKT/5000_accounts-16      264ms ± 2%      76ms ± 2%  -71.34%  (p=0.001 n=10+5)
TriesRandom/VKT/10000_accounts-16     535ms ± 2%     146ms ± 5%  -72.78%  (p=0.001 n=10+5)

name                               old alloc/op   new alloc/op   delta
TriesRandom/VKT/1000_accounts-16     9.56MB ± 0%    9.76MB ± 0%   +2.13%  (p=0.001 n=9+5)
TriesRandom/VKT/5000_accounts-16     43.5MB ± 0%    43.9MB ± 0%   +0.94%  (p=0.002 n=8+5)
TriesRandom/VKT/10000_accounts-16    87.5MB ± 0%    88.2MB ± 0%   +0.83%  (p=0.001 n=10+5)

name                               old allocs/op  new allocs/op  delta
TriesRandom/VKT/1000_accounts-16      32.8k ± 0%     35.5k ± 0%   +8.08%  (p=0.001 n=9+5)
TriesRandom/VKT/5000_accounts-16       151k ± 0%      144k ± 0%   -4.88%  (p=0.001 n=10+5)
TriesRandom/VKT/10000_accounts-16      303k ± 0%      285k ± 0%   -5.97%  (p=0.001 n=10+5)

@jsign jsign changed the title Jsign/benchktries VKT: Optimizations Dec 13, 2022
Signed-off-by: Ignacio Hagopian <[email protected]>
go.mod Outdated Show resolved Hide resolved
@@ -0,0 +1,76 @@
package tests
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here we have the benchmarks from #146.
I only removed the statelessness one as we discussed, so we avoid test flags.

Comment on lines -85 to +89
trieIndexBytes := treeIndex.Bytes32()
verkle.FromBytes(&poly[3], trieIndexBytes[16:])
verkle.FromBytes(&poly[4], trieIndexBytes[:16])
if !treeIndex.IsZero() {
trieIndexBytes := treeIndex.Bytes32()
verkle.FromBytes(&poly[3], trieIndexBytes[16:])
verkle.FromBytes(&poly[4], trieIndexBytes[:16])
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

First optimization is avoiding work if treeIndex is zero, which makes sense and is a pretty normal case.

trie/verkle.go Outdated Show resolved Hide resolved
Comment on lines -159 to +161
resolver :=
func(h []byte) ([]byte, error) {
return trie.db.diskdb.Get(h)
}
resolver := func(h []byte) ([]byte, error) {
return trie.db.diskdb.Get(h)
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Lint.

Comment on lines -242 to +257
flush := make(chan verkle.VerkleNode)
type vnflush struct {
n verkle.VerkleNode
value []byte
dbKey []byte
}
flush := make(chan vnflush, 1024)
resolver := func(n verkle.VerkleNode) {
flush <- n
value, err := n.Serialize()
if err != nil {
panic(err)
}
dbKey := nodeToDBKey(n)
flush <- vnflush{
n: n,
value: value,
dbKey: dbKey,
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Looking further at where the wall-clock time was going, I realized that after Commit()ing the underlying go-verkle trie, a significant amount of time was spent flushing the result.

What I did in ethereum/go-verkle#314, is making Flush() for the root node to do the work in parallel. This means that resolver will be called by multiple goroutines balancing more CPU work in all available cores.

To squeeze things more, the Serialize() part of the process was now moved from L258 to the resolver. This means that the Serialize() work will also be done in the goroutines that are flushing the result, and not in a single core.

In a nutshell, since the resolver execution is exploiting all cores, we want to do as much work as possible there, so the main goroutine ranging L269 is receiving plain results to be stored in the diskdb. This main goroutine isn't doing heavy CPU work, just receiving exactly what needs to be stored.

This is important, since if the range in L269 is slow, it will slow down everything. Note how I also made a buffered channel in L246. We need to avoid as much as possible to be blocking goroutines, so having extra breathing room also improved performance.

Copy link
Owner

Choose a reason for hiding this comment

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

So if #295 gets merged, Serialize will become a tad more expensive - at the cost of a cheaper deserialization (hence not producing a tree that is crash-prone because of potentially invalid commitments, as in your current approach). Your change, I believe, will mitigate this problem and make it cheaper to follow this approach. That's very nice.

jsign added 3 commits January 12, 2023 11:11
Signed-off-by: Ignacio Hagopian <[email protected]>
Signed-off-by: Ignacio Hagopian <[email protected]>
@jsign jsign marked this pull request as ready for review January 12, 2023 19:14
@jsign jsign requested a review from gballet January 12, 2023 19:14
@jsign
Copy link
Collaborator Author

jsign commented Mar 23, 2023

Should be interesting if we consider ethereum/go-verkle#314, but for now I'll close this PR.
We can recover it if we go that route.

@jsign jsign closed this Mar 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants