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

runtime: introduce PublicKeyBuffer to avoid memory allocation #8277

Merged
merged 1 commit into from
Jan 2, 2023

Conversation

mina86
Copy link
Contributor

@mina86 mina86 commented Dec 28, 2022

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.

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.
@mina86 mina86 requested a review from a team as a code owner December 28, 2022 15:16
@mina86 mina86 requested a review from akhi3030 December 28, 2022 15:16
Copy link
Collaborator

@nagisa nagisa left a comment

Choose a reason for hiding this comment

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

Neat!

@near-bulldozer near-bulldozer bot merged commit d17fae9 into near:master Jan 2, 2023
@mina86 mina86 deleted the f branch January 2, 2023 12:26
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.

None yet

2 participants