gfx-rs / wgpu-rs

Rust bindings to wgpu native library
https://wgpu.rs
Mozilla Public License 2.0
1.69k stars 186 forks source link

Binary size and unused code #841

Closed sergmister closed 3 years ago

sergmister commented 3 years ago

A small program build with standard release options is 8.4 MB. Running cargo bloat yields:

File  .text     Size Crate
10.0%  16.4% 856.8KiB wgpu
 7.9%  13.0% 680.1KiB spirv_cross
 7.2%  11.8% 618.0KiB std
 5.5%   9.0% 471.8KiB wgpu_core
 4.1%   6.7% 353.1KiB winit
 4.1%   6.7% 352.5KiB naga
 2.6%   4.3% 223.6KiB smithay_client_toolkit
 1.9%   3.1% 162.7KiB regex_syntax
 1.9%   3.1% 161.7KiB wayland_client
 1.6%   2.7% 141.3KiB [Unknown]
 1.6%   2.7% 138.9KiB regex
 1.6%   2.6% 135.2KiB gfx_backend_gl
 1.4%   2.2% 116.8KiB inplace_it
 1.3%   2.1% 111.1KiB hashbrown
 0.8%   1.3%  69.8KiB aho_corasick
 0.8%   1.3%  66.0KiB gfx_backend_vulkan
 0.7%   1.2%  61.3KiB ttf_parser
 0.5%   0.9%  44.6KiB xml
 0.5%   0.8%  42.5KiB wayland_protocols
 0.4%   0.7%  37.5KiB ash
 4.1%   6.7% 349.0KiB And 44 more crates. Use -n N to show more.
61.0% 100.0%   5.1MiB .text section size, the file size is 8.4MiB

Is there a way to build a project without including spirv_cross, naga, wgpu_core, unused backends, etc. This would help make binaries smaller and linking faster.

kvark commented 3 years ago

"cross" enables SPIRV-Cross dependency. If you depend on wgpu with the default features disabled, you'll have a pure-Rust dependency without SPIRV-Cross.

It's surprising to me to see wgpu taking 10% of your bloat, given that it's just a wrapper. I wonder if there is anything we can change to compile to less code.

Naga is only built with features that are requested by wgpu and gfx-rs backends.

As for "unused backends", would that be OpenGL for you? I can see that it accounts for 1.6% of bloat in your app. That's not a lot. So even if/when we have an option to disable the backends, it's unlikely going to help you much.

Systemcluster commented 3 years ago

If you depend on wgpu with the default features disabled, you'll have a pure-Rust dependency without SPIRV-Cross.

According to cargo tree, spirv-cross is also a dependency of gfx-backend-dx12 and gfx-backend-dx11.

kvark commented 3 years ago

Right, these to can't switch to Naga yet. We are working on this. You can get away from SPIRV-Cross on macOS and Linux so far.

sergmister commented 3 years ago

So to be clear is there a way to build without some of these dependencies, and if so, how?

kvark commented 3 years ago

@sergmister there is no way to build without SPIRV-Cross on Windows right now. I suppose that would be one way to push for an ability to disable backends at compile time: to make a build that only works on Vulkan/Windows, and thus doesn't depend on SPIRV-Cross. But honestly, I'd rather ramp up our Naga development to generate HLSL and throw out SPIRV-Cross in a more natural way.

sergmister commented 3 years ago

What about Linux?

kvark commented 3 years ago

I answered about linux in https://github.com/gfx-rs/wgpu-rs/issues/841#issuecomment-813108902 Basically, you can disable the "cross" feature there.

cwfitzgerald commented 3 years ago

Due to the upcoming gfx-hal rethinking, I'm going to close this for now and we can re-evaluate after it.