Skip to content

Commit 222f1ea

Browse files
kornelskiteoxoy
authored andcommitted
Reduce code size of error handling
1 parent a87c8d7 commit 222f1ea

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

naga-cli/src/bin/naga.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ trait PrettyResult {
326326
fn unwrap_pretty(self) -> Self::Target;
327327
}
328328

329+
#[cold]
330+
#[inline(never)]
329331
fn print_err(error: &dyn Error) {
330332
eprint!("{error}");
331333

naga/src/front/wgsl/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ pub enum Error<'a> {
278278
}
279279

280280
impl<'a> Error<'a> {
281+
#[cold]
282+
#[inline(never)]
281283
pub(crate) fn as_parse_error(&self, source: &'a str) -> ParseError {
282284
match *self {
283285
Error::Unexpected(unexpected_span, expected) => {

wgpu-core/src/error.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ use std::{error::Error, sync::Arc};
33

44
use thiserror::Error;
55

6+
#[cfg(send_sync)]
7+
pub type ContextErrorSource = Box<dyn Error + Send + Sync + 'static>;
8+
#[cfg(not(send_sync))]
9+
pub type ContextErrorSource = Box<dyn Error + 'static>;
10+
611
#[derive(Debug, Error)]
712
#[error(
813
"In {fn_ident}{}{}{}",
@@ -13,10 +18,7 @@ use thiserror::Error;
1318
pub struct ContextError {
1419
pub fn_ident: &'static str,
1520
#[source]
16-
#[cfg(send_sync)]
17-
pub source: Box<dyn Error + Send + Sync + 'static>,
18-
#[cfg(not(send_sync))]
19-
pub source: Box<dyn Error + 'static>,
21+
pub source: ContextErrorSource,
2022
pub label: String,
2123
}
2224

wgpu-hal/src/vulkan/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,7 @@ fn get_lost_err() -> crate::DeviceError {
12961296
crate::DeviceError::Lost
12971297
}
12981298

1299+
#[cold]
12991300
fn hal_usage_error<T: fmt::Display>(txt: T) -> ! {
13001301
panic!("wgpu-hal invariant was violated (usage error): {txt}")
13011302
}

wgpu/src/backend/wgpu_core.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use std::{
2424
slice,
2525
sync::Arc,
2626
};
27+
use wgc::error::ContextErrorSource;
2728
use wgc::{
2829
command::bundle_ffi::*, device::DeviceLostClosure, id::CommandEncoderId, id::TextureViewId,
2930
pipeline::CreateShaderModuleError,
@@ -267,16 +268,18 @@ impl ContextWgpuCore {
267268
self.0.generate_report()
268269
}
269270

270-
fn handle_error(
271+
#[cold]
272+
#[inline(never)]
273+
fn handle_error_inner(
271274
&self,
272275
sink_mutex: &Mutex<ErrorSinkRaw>,
273-
source: impl Error + WasmNotSendSync + 'static,
276+
source: ContextErrorSource,
274277
label: Label<'_>,
275278
fn_ident: &'static str,
276279
) {
277280
let error = wgc::error::ContextError {
278281
fn_ident,
279-
source: Box::new(source),
282+
source,
280283
label: label.unwrap_or_default().to_string(),
281284
};
282285
let mut sink = sink_mutex.lock();
@@ -299,16 +302,29 @@ impl ContextWgpuCore {
299302
});
300303
}
301304

305+
#[inline]
306+
fn handle_error(
307+
&self,
308+
sink_mutex: &Mutex<ErrorSinkRaw>,
309+
source: impl Error + WasmNotSendSync + 'static,
310+
label: Label<'_>,
311+
fn_ident: &'static str,
312+
) {
313+
self.handle_error_inner(sink_mutex, Box::new(source), label, fn_ident)
314+
}
315+
316+
#[inline]
302317
fn handle_error_nolabel(
303318
&self,
304319
sink_mutex: &Mutex<ErrorSinkRaw>,
305320
source: impl Error + WasmNotSendSync + 'static,
306321
fn_ident: &'static str,
307322
) {
308-
self.handle_error(sink_mutex, source, None, fn_ident)
323+
self.handle_error_inner(sink_mutex, Box::new(source), None, fn_ident)
309324
}
310325

311326
#[track_caller]
327+
#[cold]
312328
fn handle_error_fatal(
313329
&self,
314330
cause: impl Error + WasmNotSendSync + 'static,
@@ -317,6 +333,7 @@ impl ContextWgpuCore {
317333
panic!("Error in {operation}: {f}", f = self.format_error(&cause));
318334
}
319335

336+
#[inline(never)]
320337
fn format_error(&self, err: &(impl Error + 'static)) -> String {
321338
let mut output = String::new();
322339
let mut level = 1;

0 commit comments

Comments
 (0)