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

Add a component-model feature to wasmparser #1845

Merged

Conversation

alexcrichton
Copy link
Member

This commit gates the implementation of the component-model proposal in
wasmparser behind a Cargo feature named component-model. This is
intended to lower the binary size and compile time of this crate in
situations where components are not needed. Additionally this helps
factor logic cleanly between core module and component layers both at
the API and internal levels.

@alexcrichton
Copy link
Member Author

Note that this is built on #1844 for now and is separated out into a few refactoring commits as well (after that PR)

This commit splits the `types.rs` in `wasmparser` into a `types.rs` and
`component_types.rs`. The goal here is to split all component-related
logic to a separate file instead of having everything bundled together.
This will make it easier to conditionally include component-related bits
in the future and additionally make each module more understandable by
focusing only on one piece of functionality.
Don't `pub use` all the types from that module into the preexisting
`types` module. This helps reduce the cognitive load when looking at the
`types` module by keeping all the component bits in one place.
This commit gates the implementation of the component-model proposal in
`wasmparser` behind a Cargo feature named `component-model`. This is
intended to lower the binary size and compile time of this crate in
situations where components are not needed. Additionally this helps
factor logic cleanly between core module and component layers both at
the API and internal levels.
@alexcrichton alexcrichton force-pushed the wasmparser-component-feature branch from d495f3e to 5a3dc81 Compare October 7, 2024 17:44
@alexcrichton alexcrichton requested a review from fitzgen October 7, 2024 17:44
crates/wasmparser/src/limits.rs Outdated Show resolved Hide resolved
@alexcrichton alexcrichton enabled auto-merge October 7, 2024 18:55
@alexcrichton alexcrichton added this pull request to the merge queue Oct 7, 2024
Merged via the queue into bytecodealliance:main with commit d12d533 Oct 7, 2024
30 checks passed
@alexcrichton alexcrichton deleted the wasmparser-component-feature branch October 7, 2024 19:06
@Robbepop
Copy link
Collaborator

Robbepop commented Oct 8, 2024

I just updated this Wasmi PR to use the new wasmparser v0.219.0 and disable the component-model feature with some nice compilation time reductions:

v0.218.0:

cargo build -p wasmi --profile bench  22.52s user 1.16s system 201% cpu 11.778 total

v0.219.0:

cargo build -p wasmi --profile bench  18.95s user 1.18s system 178% cpu 11.248 total

I looked at cargo tree -d and saw that hashbrown is included (with no-hash-maps) and thought about a solution. It is because of the no-hash-maps crate feature that is used in wasmparser and simultaneously in wasmi_collections.

@alexcrichton What do you think about a small re-design with which it is possible to also put those dependencies (and associated code in wasmparser) behind yet another crate feature? The new design gets rid of the no-hash-maps crate feature and introduces 2 new ones:

  • hash-collections: Adds the hash-table based collections to wasmparser.
  • prefer-btree-collections: Prefers btree-based collections over their hash-table based counterparts.

These 2 new crate features are additive. 4 cases:

  1. Both are disabled: the btree-based collections still exist and are used.
  2. Only hash-collections are enabled, then hash-based collections exist and are used.
  3. Only prefer-btree-collections are enabled then hash-based collections do not exist and btree-map collections are used.
  4. Both are enabled: then hash-based collections exist but btree-based collections are used instead.

Thus btree-based collections always exist while hash-based collections are optional. This is natural for wasmparser because it uses external dependencies for hash-based collections (indexmap) but ships its own implementations for btree-based collections.

To keep semantics the same as before by default only the hash-collections feature is included in the default crate feature set.

This covers all use-cases while also allowing to get rid of the hash-based collections altogether, also from dependency graph. What do you think about this? Is there an even simpler design alternative?

What I like in particular about this design is that crate feature names no longer contain a negation. Negations keep twisting my brain.

@alexcrichton
Copy link
Member Author

That seems like a promising idea yeah! I'm not sure I 100% follow in some points though because there are public types in the wasmparser API, e.g. a set of imports or exports, which can't change types as a result of various features in play, so I think we'd still need the wrappers, right? Part of the problem here as well is that if the std feature is active then there's no need to depend on hashbrown, but if the std feature is disabled the dependency is needed. AFAIK that can't be modeled with Cargo's optional dependencies at this time.

Regardless though I'd be happy to see changes in this regard (I agree on preferring "positive" features) so I'm also happy to review PRs along these lines.

@Robbepop
Copy link
Collaborator

Robbepop commented Oct 9, 2024

Okay good to know. I will hopefully have more time starting next week to dig into this and come up with PRs.

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.

3 participants