Change Map iter to not create many Maps #1003
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
Change Map iter to not create many Maps.
Why
The Map iterator currently makes a host copy of the map on every step of the iteration. This is because it removes seen items rom the map which causes a new map to be created host side. Even though it results in optimized guest code because the iterator is only a single u64 handle, those optimizations are outsized by the calls to the host that repeatedly duplicate the map.
The contract size increases slightly, but the cost
Close #191
Alternatives
There might be some advantage in having the map use a shared backing structure for non-adding mutations. The host could lazily reuse the map backing data structure for read, and remove operations. We can adjust the guest side implementation of the map iterator relatively easily if the host side is improved.
Perf Diff
Code Size
Code size was marginally higher for the iterator itself, but smaller for the iterations themselves.
Budget
Budget was significantly smaller without the copies.