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

Rewrite BorshSchema to use const functions #79

Open
MaksymZavershynskyi opened this issue Jun 15, 2020 · 0 comments
Open

Rewrite BorshSchema to use const functions #79

MaksymZavershynskyi opened this issue Jun 15, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@MaksymZavershynskyi
Copy link
Contributor

Currently BorshSchema has static but not constant methods. These methods when compiled to Wasm occupy significant space. Also, they create a significant execution overhead when self-described borsh deserialization/serialization is called using https://docs.rs/borsh/0.6.2/borsh/schema_helpers/index.html

To fix it we need to implement const version of BorshSchema:: schema_container(). Unfortunately, it means two things:

  • We can only use types allowed by the const context;
  • schema_container() cannot return a type that requires allocation.

We currently intend to serialize BorshSchemaContainer using either borsh or JSON. Therefore we can have two versions of schema_container():

  • schema_container_borsh() -> &[u8];
  • schema_container_json() -> &str;
    Both return container in already serialized form. Then we can allow reconstructing BorshSchemaContainer from it, if necessary.
    schema_container_json internally would define const variables for each type and recursively concatenate them using std::concat. As the result, each type will have a compile-time computed schema. Similar technique can be used with byte slices and schema_container_borsh.

This will improve performance in the following way:

  • If we want to serialize a self-described borsh type using https://docs.rs/borsh/0.6.2/borsh/schema_helpers/index.html the helper will use schema_container_borsh to prepend an already generated sequence of bytes in front the borsh serialized object. Upon deserialization of that object the helper will check that the schema in the self-described type matches the schema of the type is deserializes it into.
  • Wasm will have hardcoded schemas instead of the code that generates them.
@MaksymZavershynskyi MaksymZavershynskyi added the enhancement New feature or request label Jun 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant