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

feat: lazily generate instanceFlags #275

Merged
merged 6 commits into from
Nov 27, 2023

Conversation

bnjbvr
Copy link
Member

@bnjbvr bnjbvr commented Nov 24, 2023

This lazily generates the instanceFlags, instead of emitting them all at once. The rationale is that if they're not required (e.g. in use cases involving only leaf components, if I understand correctly), then we can avoid allocating them and we can remove a dependency on the WebAssembly object being present. A future change could be to not use WebAssembly globals to represent the instance flags, but instead to use some global JS state, eliminating the dependency on the WebAssembly object more generally.

@bnjbvr
Copy link
Member Author

bnjbvr commented Nov 24, 2023

Got hit by the temporal dead zone, aka use-before-def of a let binding. So I had to add a Source::prepend method for the instance flags to be defined before the imports would reference them, of course.

Copy link
Collaborator

@guybedford guybedford left a comment

Choose a reason for hiding this comment

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

Looks good thank you. I suppose this fixes your issue in the simple component cases where instance flags are unused.

@@ -599,6 +600,20 @@ impl<'a> Instantiator<'a, '_> {
}
}

fn ensure_instance_flags(&mut self) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can just call this instance_flags I think, since the ensure naming is usually for cases where we need to check it against an already-written list.

CoreDef::InstanceFlags(i) => format!("instanceFlags{}", i.as_u32()),
CoreDef::InstanceFlags(i) => {
// SAFETY: short-lived borrow-mut.
self.used_instance_flags.borrow_mut().insert(*i);
Copy link
Collaborator

Choose a reason for hiding this comment

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

These are quite a few hoops just to deal with a u32. I'd suggest rather just cloning it into the set directly to avoid all the other borrow complexity. (it's underlying datatype is just a u32)

Copy link
Member Author

Choose a reason for hiding this comment

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

oh it's because I'm mutating the used_instance_flags that I need the inner mutability with RefCell. The alternatives that I've discarded were the following:

  • have core_def take &mut sef: didn't work with existing code where this would cause mutable vs immutable conflicts
  • pass a instance_flags: &mut BTreeSet parameter
    • if that variable lived in self and is mutably borrowed at call sites, we get back to the previous bullet item
    • if not, then I found it a bit invasive to pass it down to all the callers, from the top-level.

Hence the inner mutability. Do you have a preference over how to achieve this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ahh I see, makes sense, thanks for explaining the reasoning here!

@guybedford guybedford added this pull request to the merge queue Nov 27, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Nov 27, 2023
@bnjbvr
Copy link
Member Author

bnjbvr commented Nov 27, 2023

Thanks for the review!

Is the merge queue buggy? It seems the checks have passed, and it claims it's waiting for other status checks?

@guybedford guybedford added this pull request to the merge queue Nov 27, 2023
@guybedford guybedford removed this pull request from the merge queue due to a manual request Nov 27, 2023
@guybedford guybedford merged commit 2a5daf6 into bytecodealliance:main Nov 27, 2023
5 checks passed
@guybedford
Copy link
Collaborator

Yeah the merge queue is buggy, merged manually. It might be worth disabling merge queue, it's mostly been trouble so far...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants