avalanchego icon indicating copy to clipboard operation
avalanchego copied to clipboard

Cache BLS Keys

Open joshua-kim opened this issue 1 year ago • 3 comments

Why this should be merged

Since the P-Chain communicates with the warp contract over GRPC, we can re-use the bytes that are coming out of grpc to pre-populate the serialized form of a bls key, avoiding re-calculating it later which turns out to be roughly half of the cpu cycles spent in GetCanonicalValidatorSet (aside from sorting).

How this works

Pre-populates a new field that holds the cached representation of the bls key.

How this was tested

Added a benchmark test. Seeing roughly a 20% performance boost:

Before:

goos: darwin
goarch: arm64
pkg: github.com/ava-labs/avalanchego/vms/platformvm/warp
BenchmarkGetCanonicalValidatorSet
BenchmarkGetCanonicalValidatorSet/0
BenchmarkGetCanonicalValidatorSet/0-10  	 6599539	       180.4 ns/op
BenchmarkGetCanonicalValidatorSet/1
BenchmarkGetCanonicalValidatorSet/1-10  	 2472804	       485.5 ns/op
BenchmarkGetCanonicalValidatorSet/10
BenchmarkGetCanonicalValidatorSet/10-10 	  342585	      3498 ns/op
BenchmarkGetCanonicalValidatorSet/100
BenchmarkGetCanonicalValidatorSet/100-10         	   33254	     36251 ns/op
BenchmarkGetCanonicalValidatorSet/1000
BenchmarkGetCanonicalValidatorSet/1000-10        	    3010	    388901 ns/op
BenchmarkGetCanonicalValidatorSet/10000
BenchmarkGetCanonicalValidatorSet/10000-10       	     260	   4471217 ns/op
PASS

After:

goos: darwin
goarch: arm64
pkg: github.com/ava-labs/avalanchego/vms/platformvm/warp
BenchmarkGetCanonicalValidatorSet
BenchmarkGetCanonicalValidatorSet/0
BenchmarkGetCanonicalValidatorSet/0-10  	 6491647	       186.2 ns/op
BenchmarkGetCanonicalValidatorSet/1
BenchmarkGetCanonicalValidatorSet/1-10  	 3459529	       349.5 ns/op
BenchmarkGetCanonicalValidatorSet/10
BenchmarkGetCanonicalValidatorSet/10-10 	  557814	      2129 ns/op
BenchmarkGetCanonicalValidatorSet/100
BenchmarkGetCanonicalValidatorSet/100-10         	   49543	     24245 ns/op
BenchmarkGetCanonicalValidatorSet/1000
BenchmarkGetCanonicalValidatorSet/1000-10        	    4250	    285461 ns/op
BenchmarkGetCanonicalValidatorSet/10000
BenchmarkGetCanonicalValidatorSet/10000-10       	     328	   3542274 ns/op
PASS

joshua-kim avatar Apr 28 '23 21:04 joshua-kim

Some more benchmarks here https://github.com/ava-labs/avalanchego/pull/1467

abi87 avatar May 04 '23 13:05 abi87

@joshua-kim validators have been moved to their own package. The PublicKey initialization changes cause some minor conflicts. Please solve them.

abi87 avatar May 29 '23 19:05 abi87

This PR has become stale because it has been open for 30 days with no activity. Adding the lifecycle/frozen label will cause this PR to ignore lifecycle events.

github-actions[bot] avatar Mar 31 '24 00:03 github-actions[bot]