-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
First-class support for bin crates #1630
Comments
I personally see one of the bigger drawbacks here that The bloat comes from the standard library, specifically this function. This basically the initialization routine of the standard library, and while most of this is stubbed out for wasm (and as a result optimized away) there's two main sources of bloat remaining:
Fixing these two are the major sources of bloat, and there's still small pieces here and there but we're talking on the order of kilobytes for above and the remaining would be on the order of bytes. Now that's sort of the main drawback I see for this today, but even if we were to solve that I'm not actually sure how we would detect this in wasm-bindgen. The perhaps easiest solution is to simply see that |
Could you elaborate more on that? If somebody has a bin crate with a |
Oh sorry sure, so for // src/main.rs
fn main() {} vs // src/lib.rs
#[wasm_bindgen]
pub fn main() {
// ...
} in the latter case we don't automatically run it today, but we should in the former (even though we don't today) |
Ah, I see, yes that's tricky. Ideally |
True yeah! If rustc did something like that then we could definitely handle this pretty easily. To be clear I'm not sure my example is a blocker at all, just something to point out. The main blocker to me is the binary bloat problem. |
does the bloat really need to be a blocker on this issue? it seems to me that "bin crates Just Work(tm) but the output binary is larger than it needs to be" is still better than the status quo, and that binary bloat could be fixed after the fact. |
@boringcactus I tend to agree with that perspective, but... we've had a lot of people complain about the size of the So if the default experience creates huge file sizes, that's really not good. Most people will try out the default way of doing things, see the huge file sizes, then give up completely. If it was just a small increase it wouldn't matter so much, but it's actually a quite large increase. Large enough to make even me worried (and I care less about file size than other people). |
@Pauan if it's so bad it scares people off, i guess that is not actually better than the status quo. the thread naming issue could probably be solved by just not setting the thread name on wasm, although i'm not sure if that's really the best approach, or how best to implement that. no clue where to even start on deinitialization, though. |
FWIW I think a PR to support this would be fine today (to send to |
unfortunately, inferring the correct export behavior for however, if we make users toss #[cfg(target_arch = "wasm32")]
#[wasm_bindgen(start)]
pub fn wasm_main() {
main();
} alongside things i tried that don't work, and cause
"you need to toss this arbitrary chunk of code at the end of your file" is not great. a short-term solution in lieu of something more optimal would be a new proc macro that generates that automatically, like so: #[wasm_bindgen_main]
fn main() {
...
} does anybody know if "automatically attach things to the entry point of a compiled binary" is even possible without making changes to |
@boringcactus wasm-pack isn't really the problem (we can easily change it to accept bin crates), the tricky bits involve changing wasm-bindgen. We know that bin crates can be supported as-is by wasm, because stdweb fully supports bin crates, no need to add in extra attributes or anything like that. |
@Pauan i was interpreting wasm-bindgen/crates/cli-support/src/wasm2es6js.rs Lines 135 to 152 in 04c9b32
wasm-bindgen/crates/cli-support/src/js/mod.rs Lines 192 to 196 in 04c9b32
main() as-is doesn't work, but evidently stdweb resolves that somehow.
edit: that's probably mostly unrelated, stdweb resolves it by automatically treating any exported function called |
@boringcactus Yes, the stdweb solution is reasonable: just treat an exported I'm not worried about the possible confusion with |
i think matching based on name and also type signature, and only checking in the first place if no |
@boringcactus Yes, I want it. I'm currenly implementing web feature for env_logger, but I found out that I couldn't add |
Motivation
Right now, all crates must have
crate-type = ["cdylib"]
. This causes some issues:Even though you're creating an application, you need to use
src/lib.rs
(which goes against Rust idioms).You need to use this...
...rather than this:
You can't (easily) have multiple binaries, since you can only have one lib per crate.
So that means you need to create multiple crates, link them together with a workspace, then set up a build script to call wasm-pack multiple times (since it can only compile one crate at a time).
This is a pretty big deal: you might want to develop both the client and server in the same crate, but right now that's quite clunky.
Let's compare that with the standard Rust approach, which is to simply make multiple
src/bin/
subfolders.Proposed Solution
Both wasm-bindgen and wasm-pack should have native support for binaries. You should be able to create a
src/main.rs
file, slap afn main() {}
into it, and have it Just Work(tm).Similarly, you should be able to create multiple
src/bin/
subfolders, and they should also Just Work(tm).In other words, the user experience should be exactly the same as creating a non-wasm Rust binary.
That means wasm-bindgen and wasm-pack will need to compile multiple binaries at the same time and output them all together into the output folder.
Additional Context
There might be a bit of size bloat caused by using bin crates rather than lib crates. We should investigate what we can do to reduce that.
The text was updated successfully, but these errors were encountered: