diff --git a/CHANGELOG.md b/CHANGELOG.md index e86529c4a..91fca2294 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -190,6 +190,10 @@ By @sagudev in [#10115](https://github.com/gfx-rs/wgpu/pull/10115). - Recognize `GPUInternalError` when converting a WebGPU error, mapping it to `Error::Internal` instead of panicking with "Unexpected error". By @evilpies in [#9919](https://github.com/gfx-rs/wgpu/pull/9919). - Fix `pop_error_scope` panics when it returns `null`. By @beicause in [#10039](https://github.com/gfx-rs/wgpu/pull/10039). +### Performance + +- Added explicit `Send` and `Sync` implementations to key `wgpu` types so that the compiler can do less work checking those bounds. If you previously added a `#![recursion_limit = ...]` attribute to your crate due to overflow errors involving `wgpu` types, you may now be able to remove it. By @kpreid in [#10177](https://github.com/gfx-rs/wgpu/pull/10177). + ### Documentation #### General diff --git a/wgpu/src/dispatch.rs b/wgpu/src/dispatch.rs index f28279074..d20aae3a6 100644 --- a/wgpu/src/dispatch.rs +++ b/wgpu/src/dispatch.rs @@ -670,6 +670,52 @@ pub trait BufferMappedRangeInterface: CommonTraits { fn as_uint8array(&self) -> &js_sys::Uint8Array; } +/// Generates a `Send` and `Sync` implementation for a dispatch enum generated by `dispatch_types!`. +macro_rules! explicit_send_sync_impl { + ($name:ident) => { + /// Implement [`Send`] + [`Sync`] for the dispatch type, and check that all of its fields + /// are [`Send`] + [`Sync`] so that that implementation is sound. + /// + /// This is identical to the “auto trait” implementation that Rust would provide, except + /// that it is eager rather than lazy: its requirements are checked now (in + /// `_fields_are_send_sync`), when this crate is compiled, rather than whenever a dependent + /// wants to know whether `$name: Send` holds. + /// + /// This improves compilation performance and avoids a risk of dependents running into the + /// default [`recursion_limit`] when checking types containing wgpu API types. This risk + /// will become greater when Rust’s “next solver” is stabilized. + /// + /// The effectiveness of this strategy is tested by + /// ../tests/send_sync_recursion.rs. + #[cfg(send_sync)] + const _: () = { + // SAFETY: Bounds checked below + unsafe impl Send for $name {} + // SAFETY: Bounds checked below + unsafe impl Sync for $name {} + + /// This code will fail to compile if any field is not `Send + Sync`, or if a new field + /// is added to the dispatch type. + /// + /// This technique is modeled after the macro library `non_structural_derive`, with + /// permission (see ). + /// We only need it in this very narrow situation, so we can use a simpler macro. + fn _fields_are_send_sync(dispatch_enum: &$name) { + fn _check_bound(_: &T) {} + // Must dereference to handle the case where there are no enabled variants. + match *dispatch_enum { + #[cfg(wgpu_core)] + $name::Core(ref value) => _check_bound(value), + #[cfg(webgpu)] + $name::WebGPU(ref value) => _check_bound(value), + #[cfg(custom)] + $name::Custom(ref value) => _check_bound(value), + } + } + }; + }; +} + /// Generates a dispatch type for some `wgpu` API type. /// /// Invocations of this macro take one of the following forms: @@ -846,6 +892,8 @@ macro_rules! dispatch_types { } } } + + explicit_send_sync_impl!($name); }; ( mut type $name:ident: $interface:ident = $core_type:ident,$webgpu_type:ident,$custom_type:ident @@ -1012,6 +1060,8 @@ macro_rules! dispatch_types { } } } + + explicit_send_sync_impl!($name); }; } diff --git a/wgpu/tests/send_sync_recursion.rs b/wgpu/tests/send_sync_recursion.rs new file mode 100644 index 000000000..cc4b44da4 --- /dev/null +++ b/wgpu/tests/send_sync_recursion.rs @@ -0,0 +1,83 @@ +//! Tests that evaluating `wgpu::SomeType: Send + Sync` is cheap. +//! +//! This test must be located within the `wgpu` package so that it can use `cfg(send_sync)`. +//! It is a separate test target so that, if it fails to compile, it doesn’t block other builds. +//! +//! The implementations that supply the property this test is testing for are located in +//! `../src/dispatch.rs`, in the macro `explicit_send_sync_impl`. + +#![cfg(send_sync)] +#![allow(clippy::type_complexity, clippy::explicit_auto_deref)] + +/// 50 `Box`es that have to be looked through to evaluate auto traits. +/// +/// This count is chosen so that if `wgpu` uses a significant amount of the default +/// recursion limit, this code will fail to compile. +#[rustfmt::skip] +struct Tower( + Box>>>>>>>>> + >>>>>>>>>> + >>>>>>>>>> + >>>>>>>>>> + >>>>>>>>>> +); + +impl core::ops::Deref for Tower { + type Target = T; + + fn deref(&self) -> &Self::Target { + // We could let auto-deref handle this, but where’s the fun in that -- + // er, I mean, rust-analyzer wouldn’t type check it properly. + &**************************************************self.0 + } +} + +const fn assert_send_sync_is_cheap() +where + Tower: Send, + Tower: Sync, +{ +} + +const _: () = { + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + // RenderBundleEncoder is never Send or Sync + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); + assert_send_sync_is_cheap::(); +};