-
Notifications
You must be signed in to change notification settings - Fork 337
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
Revamp region system #2137
Revamp region system #2137
Conversation
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.
Yeah, I think that's an improvment. High level some ideas:
- Is RegionSource still needed now?
- A Region makes everything mutable (e.g. there is non-mut [u8] references in the BLS PR, which are written into). Do we need some sort of mutability flag or RwRegion?
It would be fun to see if this actually saves gas in practice, by avoiding calls to the allocator (and potentially even saving memory grow instructions) |
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.
LGTM.
In my tests, for a lot of addr_humanize
calls, it uses ~30% less gas (comparing inlined String::from_utf8_unchecked
vs consume_string_region_written_by_vm
), but that number is just for the wasm execution (i.e. ignoring the wasmd part) and could still be off by quite a bit.
I used a cosmwasm_vm::MockApi
(so gas cost for the actual call into the chain is comically low compared to what wasmd charges) and there's some overhead for constructing the CanonicalAddr and adding it to the response, the loop itself, etc.
For a real use-case it's probably negligible, since I don't expect people to humanize more than a handful of addresses in one message execution and almost all of the cost for this will be charged by wasmd, but still nice to reduce execution gas cost here.
The change also reduces the wasm size of the addr_humanize
function a bit, so that's a nice bonus.
As for the gas saving of using stack vs Box pointers: For |
This is a full internal rewrite of the region system, making use of the fact that the stack is part of the linear memory in WASM and can be read by the host.
Meaning a decrease in allocations when calling host functions from the guest module, and the entire system is now a little more robust and has a nicer API.
So far I tested it locally with the
crypto-verify
contract which is a pretty complex one, but the CI will run all the contracts, so we'll see how it holds up.