Pyspec SSZ; HTR caching of Vector. #1481
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Although I still like to see pyspec-ssz replaced with py-ssz or moved out to refactor more, it looks like py-ssz is not quite ready, or maybe not the right choice. And moving it out and refactoring brings different problems: keeping it sync, and readability / size (py-ssz pyrsistent looks great, but also ~10x more code to go through as a reader).
The two big pain points for mainnet ssz tree-hashing are:
In tests the validator registry is not that big however, it is really the vector data that's slow without caching: 8192 roots to merkleize together.
So this PR split a vector into smaller vectors during hash-tree-root, and caches the results. On a modification, it removes the cache entry. And caching is only active for large enough vectors, of elements of an immutable type.
A quick bench shows a ~80 times improvement for a
Vector[Bytes32, 8192]
, when modifying elements in a rotation (like the historical vectors in the spec): https://gist.github.com/protolambda/4509db7f91d07b40a65ca3daf1e37685Writing some tests and a bench of the
BeaconState
later.Functionally this does not change SSZ or the spec. And although not too pretty, it helps to make mainnet test generation more bareable.
Note: base-branch on the other SSZ PR, which I would like to merge first, and then update the base.