-
Notifications
You must be signed in to change notification settings - Fork 289
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: add static buffer as an alternative to allocating memory #939
Conversation
This comment was marked as duplicate.
This comment was marked as duplicate.
@Ddystopia Thank you a lot for the PR! I have to make sure (at least locally) that these changes indeed won't have an effect on the performance of Wasmi for inputs that should have a large impact on linear memory accesses. Unfortunately I have observed far too often that effects shown via Compiler Explorer are not transferable to real world execution since Compiler Explorer views code snippets in isolation where real code always run within some context where a compiler might miss certain optimization opportunities. I am not yet sold on the config addition but it seems to be necessary to allow for non-imported linear memory backed by static buffers. Seems like a non-ideal API surface to me. Is this a critical feature to you? Could we maybe separate this out to make this feature a bit smaller and only support imported linear memory for now? |
Thank you for the response! I don't actually know, would imported memory be sufficient or not. Is there a way to compile Rust to wasm so that it would only import memory and not export any, so that wasmi would not create them on it's own? |
I am not sure but what I can say is that there are a tons of tools that you could use to simply post process the resulting Wasm file. This is to be recommended anyways imo since LLVMs own Wasm codegen is suboptimal. |
9a87845
to
143f2b7
Compare
Force-pushed to remove config changes. This is now only available for imported memories. |
143f2b7
to
d648a8c
Compare
Force pushed to fix formatting |
d648a8c
to
08c64c9
Compare
I'm not sure what should I do with that clippy job - it would not compile if I remove those 2 imports. They're probably unused under some combination of features. Should I gate those imports? |
I think it is a |
08c64c9
to
faab04f
Compare
Unfortunately, compiler did not manage to optimize that time when I've added a len to the static buffer, so I went with your initial idea to use vec's raw parts. There are also some tests that I tried with miri and they do pass. Additionally rebased to main. |
faab04f
to
4147fe4
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #939 +/- ##
==========================================
- Coverage 81.60% 81.57% -0.03%
==========================================
Files 258 258
Lines 23601 23682 +81
==========================================
+ Hits 19259 19318 +59
- Misses 4342 4364 +22 ☔ View full report in Codecov by Sentry. |
4147fe4
to
502cc79
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I can see now only the proper zeroing of bytes on new_static
and grow
is missing. We want to zero only the initial_len
for new_static
and only the new parts in grow
to limit the amount of work needed per operation to the minimum.
Also please do not rebase this PR since this kinda destroys my previous reviews and makes it really hard for me to keep track of what happened.
Okay, I'll implement it tomorrow as a separate commit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me now! :)
The new tests pass and the CI is happy.
Thanks a lot for the PR and the new feature with static buffer backed linear memories in Wasmi. I am sure this feature is going to be very helpful for the embedded community. :)
🚀
…-labs#939) * feat: add static buffer as an alternative to allocating memory * fix: zeroing static buffer gradually
Draft for static buffer as an alternative to allocating memory. Closes #878.
User can give
fn() -> Option<&'static mut [u8]>
, which is a factory for static buffers. I personally only need one, but there are problems if we want user to provide&'static mut [u8]
. It is notCopy
orClone
for obvious reasons, butConfig
is. It also cannot be wrapped inRefCell
to be taken from config by shared reference, because there is aSync
assert.In my code, this feature could used in 100% safe way:
As can be seen from godbolt, rustc optimizes out enum access in the hot methods
data
,data_mut
andlen
, so there is no need for unsafe code and enum is sufficient.Unresolved questions