runtime: introduce PublicKeyBuffer to avoid memory allocation #8277
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.
Currently when public key is read it’s unnecessarily copied into
a temporary vector. The copy is necessary because otherwise public
key data would hold a shared reference on self.registers which
prevents any methods which take exclusive reference to self to be
called.
However, if we decode the public key immediately after reading it, we
can deal with sized objects which can be stored on stack. This is
perfect except because we cannot change when errors are reported we
have to postpone returning of the deserialisation errors.
Introduce PublicKeyBuffer which facilitates that. It hides from the
users the fact that deserialisation happens immediately as soon as the
data is read providing an interface which looks as if deserialisation
was happening separately from reading.
An alternative would be to use a fixed-size 65-byte array and separate
reading the data from deserialisation just like it’s happening
now. A minor downside of that is hard-coding the 65-byte size. We’d
probably want PublicKey::MAX_LEN but even then having such a constant
doesn’t seem too clean of a solution to me.