Backtrace capture and printing add ~24 KB to wasm32-unknown-unknown builds that can never capture one

Open Beginner friendly
#470 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
rust, wasm
Domain
backend

Research direction

Start with src/backtrace.rs and src/fmt.rs, where the issue provides the proposed cfg changes. Check how backtrace! and the backtrace printing block are compiled for wasm32-unknown-unknown, then compare a wasm build with and without the change. Done means backtrace formatting code is omitted from that target while behavior remains unchanged.

Written by the indexing model from the issue text.

Description

On wasm32-unknown-unknown, std::backtrace::Backtrace::capture() never captures anything, but anyhow's Debug impl still references the backtrace printer, which pulls in std's backtrace formatting and rustc_demangle.

In a web page built with Dioxus's --wasm-split (which exports the whole function table, so nothing whose address is taken gets dropped), this measured about 24 KB of code before compression, 16 KB gzip, out of a ~130 KB gzip page (anyhow 1.0.104, Rust 1.93, Dioxus 0.7.10, opt-level = "z", LTO).

Gating the backtrace! macro to None, and the printing block in fmt.rs, on not(target_family = "wasm") removes it with no change in behaviour, since nothing could be captured there anyway. Would you take that, or a feature/cfg to opt out?

Related but opposite: #381 asks for a way to switch capture on in wasm.

--- a/src/backtrace.rs
+++ b/src/backtrace.rs
@@ -4,13 +4,20 @@
 #[cfg(not(feature = "std"))]
 pub(crate) enum Backtrace {}
 
-#[cfg(feature = "std")]
+#[cfg(all(feature = "std", not(target_family = "wasm")))]
 macro_rules! backtrace {
     () => {
         Some(std::backtrace::Backtrace::capture())
     };
 }
 
+#[cfg(all(feature = "std", target_family = "wasm"))]
+macro_rules! backtrace {
+    () => {
+        None
+    };
+}
+
 #[cfg(not(feature = "std"))]
 macro_rules! backtrace {
     () => {
--- a/src/fmt.rs
+++ b/src/fmt.rs
@@ -40,7 +40,7 @@
             }
         }
 
-        #[cfg(feature = "std")]
+        #[cfg(all(feature = "std", not(target_family = "wasm")))]
         {
             use alloc::string::ToString;
             use std::backtrace::BacktraceStatus;
Dominant language
Rust
Stars
6.7k
Forks
232
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dtolnay/anyhow

All issues in dtolnay/anyhow

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.