Skip to content

Avoid repeated lang-item map lookups in sizedness_fast_path [cachegrind -0.22% serde check, suite below local noise] - #67

Draft
xmakro wants to merge 1 commit into
perf/base-0713from
perf/sizedness-langitem-lookup
Draft

Avoid repeated lang-item map lookups in sizedness_fast_path [cachegrind -0.22% serde check, suite below local noise]#67
xmakro wants to merge 1 commit into
perf/base-0713from
perf/sizedness-langitem-lookup

Conversation

@xmakro

@xmakro xmakro commented Jul 28, 2026

Copy link
Copy Markdown
Owner

sizedness_fast_path runs for a large share of all predicates and paid a reverse-hashmap lookup, hashing the DefId, just to tell Sized from MetaSized.

This resolves the lang-item table once and compares DefIds directly. The caller_bounds loop below reuses the resolved Sized DefId instead of calling tcx.is_lang_item per bound; cachegrind puts that probe at about 1% of the saved instructions, so the loop change is a consistency cleanup and the win is the head lookup.

LanguageItems::get gets #[inline] so the accessor is an array index rather than an out-of-line cross-crate call. Without it, a non-LTO build would add a call on the miss path instead of removing work.

The comparison is equivalent to the old one because each lang item resolves to at most one DefId and each DefId to at most one lang item: LanguageItems::set is the only writer of both tables, and duplicate #[lang] items abort before this code can run.

Cachegrind on serde check full attributes the effect cleanly: -18.6M instructions (-0.22%), all of it in as_lang_item and the reverse IndexMap lookup it wraps, with the rest of the compiler contributing 1.5K instructions of the 18.6M.

Suite-level instruction counts cannot resolve a change this small locally: the 15-cell geomean (5 crates x check/debug/opt, full, jemalloc) reads -0.18% against one clean-base build and +0.07% against a second build of the same base commit, because same-commit stage2 rebuilds differ by 0.25% geomean (up to 0.7% on single cells). Serde is the only crate that stays improved against both base builds in all three profiles, consistent with the cachegrind number. The suite-level verdict needs a rust-timer run.

@xmakro
xmakro force-pushed the perf/sizedness-langitem-lookup branch from 5384cc5 to f8c5549 Compare August 4, 2026 06:01
@xmakro xmakro changed the title Avoid repeated lang-item map lookups in sizedness_fast_path [-0.12%, below significance] Avoid repeated lang-item map lookups in sizedness_fast_path [-0.18%, below significance] Aug 4, 2026
@xmakro xmakro changed the title Avoid repeated lang-item map lookups in sizedness_fast_path [-0.18%, below significance] Avoid repeated lang-item map lookups in sizedness_fast_path [cachegrind -0.22% serde check, suite below local noise] Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant