Skip to content

Reimplement how instance exports are stored/loaded#2984

Merged
alexcrichton merged 2 commits into
bytecodealliance:mainfrom
alexcrichton:fix-leak
Jun 17, 2021
Merged

Reimplement how instance exports are stored/loaded#2984
alexcrichton merged 2 commits into
bytecodealliance:mainfrom
alexcrichton:fix-leak

Conversation

@alexcrichton
Copy link
Copy Markdown
Member

This commit internally refactors how instance exports are handled and
fixes two issues. One issue is that when we instantiate an instance we
no longer forcibly load all items from the instance immediately,
deferring insertion of each item into the store data tables to happen
later as necessary. The next issue is that repeated calls to
Caller::get_export would continuously insert items into the store data
tables. While working as intended this was undesirable because it would
continuously push onto a vector that only got deallocated once the
entire store was deallocate. Now it's routed to Instance::get_export
which doesn't have this behavior.

Closes #2916
Closes #2983

This commit internally refactors how instance exports are handled and
fixes two issues. One issue is that when we instantiate an instance we
no longer forcibly load all items from the instance immediately,
deferring insertion of each item into the store data tables to happen
later as necessary. The next issue is that repeated calls to
`Caller::get_export` would continuously insert items into the store data
tables. While working as intended this was undesirable because it would
continuously push onto a vector that only got deallocated once the
entire store was deallocate. Now it's routed to `Instance::get_export`
which doesn't have this behavior.

Closes bytecodealliance#2916
Closes bytecodealliance#2983
@github-actions github-actions Bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Jun 14, 2021
@github-actions
Copy link
Copy Markdown

Subscribe to Label Action

cc @peterhuene

Details This issue or pull request has been labeled: "wasmtime:api"

Thus the following users have been cc'd because of the following labels:

  • peterhuene: wasmtime:api

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

Comment thread crates/wasmtime/src/instance.rs Outdated
Comment on lines +746 to +748
exports,
types: Arc::clone(self.cur.module.types()),
signatures: Arc::clone(self.cur.module.signatures()),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of unfortunate to add a new malloc call and a couple of atomic inc refs on the instantiation hot path. Did you happen to measure if this has any observable effect on instantiation times?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was actually already happening, although it was even worse. Previously we created an IndexMap with freshly allocated strings for all the names as well. This should perform fewer allocations (only one) as well as be a bit more optimized (it's an array, should be calloc-able).

The atomics I don't think really affect anything, they're on the order of a handful of ns when we shoot for a handful of us for instantiation.

@fitzgen
Copy link
Copy Markdown
Member

fitzgen commented Jun 14, 2021

I don't really feel familiar enough with this code to give an r+ but those two things jumped out at me when I looked at the PR.

Copy link
Copy Markdown
Member

@peterhuene peterhuene left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me!

I appreciate the plethora of comments in instantiate_raw too.

@alexcrichton alexcrichton merged commit d8d4bf8 into bytecodealliance:main Jun 17, 2021
@alexcrichton alexcrichton deleted the fix-leak branch June 17, 2021 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasmtime:api Related to the API of the `wasmtime` crate itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling Caller::get_export allocates memory that can't be un-allocated Don't load all instance exports into a Store immediately after instantiation

3 participants