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

feat(gstd): propagate stack allocations #3007

Merged
merged 33 commits into from
Aug 18, 2023

Conversation

StackOverflowExcept1on
Copy link
Member

@StackOverflowExcept1on StackOverflowExcept1on commented Jul 31, 2023

Resolves #2880

Changes:

  • msg::send*(...), msg::reply*(...), {prog, ProgramGenerator}::create_program<E>*(...) might use stack allocation if payload.encoded_size() is less than 64 KiB
  • removed load_on_stack and reply_on_stack from gstd
  • gstd::prelude re-exports MaxEncodedLen
  • if the user uses #[derive(MaxEncodedLen)], then due to the specialization, encoding on stack is faster (no need to calculate the size)

@StackOverflowExcept1on StackOverflowExcept1on added the A0-pleasereview PR is ready to be reviewed by the team label Jul 31, 2023
gstd/src/msg/encoded.rs Outdated Show resolved Hide resolved
@StackOverflowExcept1on StackOverflowExcept1on added the D3-gstd Gear Standard Library label Aug 4, 2023
Base automatically changed from av/alloca to master August 14, 2023 10:43
@StackOverflowExcept1on StackOverflowExcept1on marked this pull request as ready for review August 14, 2023 11:36
@StackOverflowExcept1on
Copy link
Member Author

StackOverflowExcept1on commented Aug 14, 2023

I think we can make the write method infallible because:

  1. ExternalBufferOutput is used when encoding on stack with precomputed size, where size <= 64 KiB. So overflow when adding numbers is impossible.
  2. parity-scale-codec assumes that the write method is also infallible. For example, impl Output for Vec<u8> uses the Vec::extend_from_slice method, which does ptr::copy_nonoverlapping under the hood.

Performance impact:

0.9254876953446401 * 10^9 (master)
0.9276711477285593 * 10^9 (av/stack-allocation-2, with additional checks)
0.9215007208181087 * 10^9 (av/stack-allocation-2, with write infallible)

@StackOverflowExcept1on StackOverflowExcept1on merged commit c44e07d into master Aug 18, 2023
7 checks passed
@StackOverflowExcept1on StackOverflowExcept1on deleted the av/stack-allocation-2 branch August 18, 2023 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A0-pleasereview PR is ready to be reviewed by the team D3-gstd Gear Standard Library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

use encoded_size in gstd::msg::reply<D: Encode>
3 participants