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

Improved traphandler code #2318

Closed
wants to merge 2 commits into from
Closed

Improved traphandler code #2318

wants to merge 2 commits into from

Conversation

syrusakbary
Copy link
Member

Description

Improved traphandler code by simplifying the TrapHandler and making explicit it has to be Send + Sync.

Review

  • Add a short description of the change to the CHANGELOG.md file

Copy link
Contributor

@Hywan Hywan left a comment

Choose a reason for hiding this comment

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

Improved traphandler code

Can you elaborate please about how the code is improved now? What was the issue with the previous version of the code?

Thanks!

@syrusakbary
Copy link
Member Author

Can you elaborate please about how the code is improved now? What was the issue with the previous version of the code?

Before it was depending on a Trait just for using a function. It makes the system on depending on a Lock when it was not really necessary. It is much simpler to depend on a function!

lib/api/src/store.rs Show resolved Hide resolved
lib/api/src/store.rs Outdated Show resolved Hide resolved
lib/engine/src/artifact.rs Outdated Show resolved Hide resolved
lib/api/src/store.rs Outdated Show resolved Hide resolved
*m = handler;
/// Sets a [`TrapHandler`] for the `Store`
pub fn set_trap_handler(&mut self, handler: Option<Box<TrapHandler<'static>>>) {
self.trap_handler = Arc::new(handler);
Copy link
Contributor

Choose a reason for hiding this comment

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

So I like this, but lets make sure the new semantics are what we intended. In the old code, calling set_trap_handler would globally update the trap handler in all Stores. In the new code it only updates the trap handler for this specific store (and Stores Cloned from it). All the old trap handlers will continue to work here because they're kept alive by the Arc. (This is actually a great example of how using the raw pointer could have caused problems, the moment set is called again, the trap handler being used could have been invalid.)

If the move from Store's having 1 trap handler to Stores having N trap handlers in a tree-like structure is intentional then 👍 , if not then we probably need to rethink some of this.

And if this is intentional we may want to call it out more in the doc comments.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also side note but if you want to remove the extra Box in Arc<Option<Box<TrapHandler<'static>>> let me know, I think it should be possible to do so in many cases.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with the first comment :-).

@@ -1015,7 +1018,7 @@ impl InstanceHandle {
/// Only safe to call immediately after instantiation.
pub unsafe fn finish_instantiation(
&self,
trap_handler: &dyn TrapHandler,
trap_handler: Arc<Option<Box<TrapHandler<'static>>>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Generally for any of these functions, but you can make them take&Arc, too, especially if they're not storing the Arc (in the cases where they just want to read it, you can even pass Option<&TrapHandler<'static>> I think by deref'ing the Arc and using as_ref on the Option)

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @MarkMcCaskey here. It avoids the need to clone the Arc, so it avoids atomic operations, and allocations.

Comment on lines +43 to 45
pub fn trap_handler(&self) -> Arc<Option<Box<TrapHandler<'static>>>> {
self.trap_handler.clone()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Idea: We can change trap_handler to return &Arc<…> rather than Arc<…>, so that we don't need clone the trap handler for every clone. It's going to be the responsability of the “user” to clone it if necessary. It will improve code even more.

Suggested change
pub fn trap_handler(&self) -> Arc<Option<Box<TrapHandler<'static>>>> {
self.trap_handler.clone()
}
pub fn trap_handler(&self) -> &Arc<Option<Box<TrapHandler<'static>>>> {
&self.trap_handler
}

@Hywan Hywan self-assigned this Jul 19, 2021
@Hywan Hywan added 🎉 enhancement New feature! 📦 lib-api About wasmer 📦 lib-object About wasmer-object 📦 lib-vm About wasmer-vm labels Jul 19, 2021
@syrusakbary syrusakbary assigned Amanieu and unassigned Hywan Oct 28, 2021
@syrusakbary syrusakbary added the 🕵️ needs investigation The issue/PR needs further investigation label Oct 28, 2021
@heyjdp
Copy link

heyjdp commented May 24, 2022

Closing PR, fixed by other PR #2892

@heyjdp heyjdp closed this May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎉 enhancement New feature! 📦 lib-api About wasmer 📦 lib-object About wasmer-object 📦 lib-vm About wasmer-vm 🕵️ needs investigation The issue/PR needs further investigation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants