From bde08f4e4a37a023dca6e836ad6dda966651b365 Mon Sep 17 00:00:00 2001 From: xxchan Date: Mon, 27 Jan 2025 12:19:07 +0800 Subject: [PATCH] feat: disable backtrace for NotFound error https://github.com/apache/opendal/discussions/5569 Signed-off-by: xxchan --- core/src/types/error.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/core/src/types/error.rs b/core/src/types/error.rs index 4d55142732c6..01c489bfcbd9 100644 --- a/core/src/types/error.rs +++ b/core/src/types/error.rs @@ -93,6 +93,14 @@ impl ErrorKind { pub fn into_static(self) -> &'static str { self.into() } + + /// Capturing a backtrace can be a quite expensive runtime operation. + /// For some kinds of errors, backtrace is not useful and we can skip it (e.g., check if a file exists). + /// + /// See + fn disable_backtrace(&self) -> bool { + matches!(self, ErrorKind::NotFound) + } } impl Display for ErrorKind { @@ -314,7 +322,11 @@ impl Error { source: None, // `Backtrace::capture()` will check if backtrace has been enabled // internally. It's zero cost if backtrace is disabled. - backtrace: Backtrace::capture(), + backtrace: if kind.disable_backtrace() { + Backtrace::disabled() + } else { + Backtrace::capture() + }, } }