Skip to content

Switch WgpuWrapper to a macro and have it compute Send/Sync eagerly - #25512

Open
SkiFire13 wants to merge 12 commits into
bevyengine:mainfrom
SkiFire13:wgpu-eager-send-sync
Open

Switch WgpuWrapper to a macro and have it compute Send/Sync eagerly#25512
SkiFire13 wants to merge 12 commits into
bevyengine:mainfrom
SkiFire13:wgpu-eager-send-sync

Conversation

@SkiFire13

@SkiFire13 SkiFire13 commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Objective

Solution

While investigating a wgpu trait overflow issue with the new solver I noticed that eagerly computing its Send/Sync reduces the compile time of bevy_render by around 10 seconds (on my machine debug goes from 20s to 9s and release from 26s to 15s).

I don't like too much having to expose all these types though, and we might get the same perf benefits if gfx-rs/wgpu#9953 is merged though (edit: I tested with that PR changes and that alone reduces compile times by ~6 seconds, however when this PR is added it reduces another ~4 seconds).

I also ended up removing the Arcs used in some of the wgpu wrapper types, as all wgpu types now use Arcs under the hood or equivalent.

Comment thread crates/bevy_render/src/renderer/wgpu_wrapper.rs
@laundmo

laundmo commented Aug 22, 2026

Copy link
Copy Markdown
Member

Honestly i think thw 4 seconds are worth it, and it doesn't seem like many of the types are public if any at all?

@Zeophlite Zeophlite added A-Rendering Drawing game state to the screen D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 23, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in Rendering Aug 23, 2026
@SkiFire13

Copy link
Copy Markdown
Contributor Author

and it doesn't seem like many of the types are public if any at all?

WgpuQueue, WgpuAdapter, WgpuInstance, WgpuAdapterInfo and WgpuErrorSource are public.

Note that previously WgpuWrapper was public as well.

@laundmo

laundmo commented Aug 23, 2026

Copy link
Copy Markdown
Member

It seems only bevy_pbr needs one of them as public, and for a usecase that i think could easily be changed to not need access to the wgpu wrapper type.

tho i haven't looked how much WgpuWrapper is used across the ecosystem. Kinda wish it was easily possible to search all of crates.io (not just all of github)

@futile

futile commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Could this also be added to a (potential) 0.19.2 release? :> Currently the forward compat warning is printed on every build, even on a no-op build. It can be worked around by manually setting the recursion limit to something higher, but every downstream crate needs to repeat that. It's also possible to allow or expect the warning, but that's not the nicest either.

Upside is, it only applies to recent nightly rustc versions, so not everyone should run into this. I would expect it to become more common over time though, as more people upgrade their nightly versions.

So if a 0.19.2 gets released, it would be great to have this, and/or the related wgpu-fix in that release.

@laundmo

laundmo commented Aug 26, 2026

Copy link
Copy Markdown
Member

IIRC this is a compile-time optimization discovered while attempting to solve the overflow, but doesn't actually fully solve it. That would mean backporting it would be nice for compile times but not get rid of the warning.

@laundmo

laundmo commented Aug 27, 2026

Copy link
Copy Markdown
Member

This seems to conclusively solve

According to Heaptrack (which i was messing around with trying to understand why) this PR manages to cut the memory needed to compile just bevy_render from 14GB to 1.4GB.

I haven't tested this with just the wgpu PR, but if memory is at all correlated to runtime then its likely this is still way worth it.

The heaptrack files, for those curious: https://share.yadamiel.com/bevy_benchmarks/wgpu-eager-send-sync/
you can view them with heaptrack --analyze <file>

@stuartparmenter stuartparmenter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seems reasonable to me and can't think of any obvious reasons why one would need it externally. The only hit I found in my dependencies was a test in Hanabi where it is mocking a RenderQueue.

Probably worth a migration guide or note somewhere though

@tychedelia tychedelia left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is fine? Although it's definitely ugly. Not blowing up the compiler is probably a worthwhile fix no matter the cost.

Comment thread crates/bevy_render/src/renderer/wgpu_wrapper.rs Outdated
@stuartparmenter stuartparmenter added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 27, 2026
@SkiFire13

Copy link
Copy Markdown
Contributor Author

I changed the way RenderQueue, RenderAdapter, RenderInstance and RenderAdapterInfo are defined, making them the wgpu wrappers themselves and thus avoiding having to create and export other wgpu wrappers.

Moreover I removed the Arc wrappers they had. WGPU almost 3 years ago started reference-counting all their types so there's no need to add an extra Arc on our side.

@stuartparmenter stuartparmenter added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels Aug 29, 2026
@stuartparmenter
stuartparmenter self-requested a review August 29, 2026 16:28
@Zeophlite Zeophlite added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 30, 2026
@Zeophlite Zeophlite modified the milestones: 0.20, 0.19.2 Aug 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Rendering Drawing game state to the screen D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

8 participants