NOSSLETTER.techLIVE
back to archive

Compressed `ParamEnv`s for common marker traits

Language: Rust+1180 -543performance
concepts demonstrated
Bitsets for trait representationGeneric trait boundsCompiler optimization techniquesTrait system internals
The Issue & Context

Before this PR, ParamEnv stored many similar trait bounds redundantly for each generic parameter, leading to large, repetitive predicate sets that slowed trait checking and increased memory usage.

Root Cause Analysis

The old ParamEnv representation enumerated all trait bounds explicitly per generic parameter, including Sized, MetaSized, and others, causing duplication and bloated predicate lists; this was compounded by hacks to avoid certain bounds like MetaSized, and the trait solver lacked a way to quickly skip irrelevant predicates.

Solution Strategy & Architectural Pattern

This PR replaces sets of common trait bounds per generic parameter with compressed bitsets representing traits like Sized, Clone, Copy, etc., stored in rustc_middle/src/ty/mod.rs; trait queries then use specialized iterators to efficiently skip checking predicates outside these compressed sets, trading upfront interning for faster repeated lookups and enabling future implicit bound expansions without linear growth in predicate size.

Key Takeaway for Contributors

When many similar constraints repeat across parameters, representing them as compact bitsets and adding query-level filtering can reduce memory overhead and speed up common queries, especially in systems with complex trait bounds and implicit expansions.