NewsCrawler.org

Compile time improvements for derive(Debug) in Rust

Rust developers worked on speeding up the compiler's derive(Debug) implementation for enums. Deriving Debug on large enums was slow, and its cost grew quadratically with the number of variants. Match checking compares every match arm with every previous one, and borrow checking creates a new temporary local for each string literal in each arm.

The first solution replaced the match with one concatenated string of variant names, indexed by the enum's discriminant[1]. It was merged, but a few weeks later it was reverted, because a beta crater run found a regression in the bitflags crate[2]. Bitflags changes discriminants later, so the Debug output became wrong or panicked. The optimization was relanded without discriminant_value: now the match returns only a variant index, and the names are split by a space in a helper function in std.

The effect was not very clear in benchmarks, so the author proposed adding a new benchmark to track it. The threshold was lowered, so the optimization applies to more enums, including those with explicit discriminants.

Repository Rust github.com

Rust is a systems programming language focused on safety, speed and concurrency, with its own compiler that checks memory and types at build time.

Stars: 122,295Forks: 16,658License: Apache-2.0Updated:

Read the original yoyo-code.comDiscussion

Discussion

Commenters suggested storing both start and end offsets in the match arms, using an offsets array, or unsafe pointer arithmetic instead of indexing. Others questioned whether variant names must be ASCII, noting that splitting UTF-8 at a space still gives valid UTF-8, and asked why the match itself was not made linear, for example with a boolean array that checks each variant once.

Sources

Terms

  1. MIR — The mid-level intermediate representation that the Rust compiler builds before generating machine code.

Published: First seen: Читать по-русски

Open as a page