From 7e4f176c9413ad40db8ee2259b76e5066036f926 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Mon, 17 Jun 2024 15:28:24 +0300 Subject: [PATCH] [perf] More span update benchmarking --- compiler/rustc_span/src/span_encoding.rs | 44 +++++++----------------- 1 file changed, 13 insertions(+), 31 deletions(-) diff --git a/compiler/rustc_span/src/span_encoding.rs b/compiler/rustc_span/src/span_encoding.rs index 31d2a9db01df9..6b2114abbac2f 100644 --- a/compiler/rustc_span/src/span_encoding.rs +++ b/compiler/rustc_span/src/span_encoding.rs @@ -327,43 +327,25 @@ impl Span { // interner and can fall back to `Span::new`. #[inline] pub fn map_ctxt(self, update: impl FnOnce(SyntaxContext) -> SyntaxContext) -> Span { - let (updated_ctxt32, data); match_span_kind! { self, InlineCtxt(span) => { - updated_ctxt32 = update(SyntaxContext::from_u16(span.ctxt)).as_u32(); - // Any small new context including zero will preserve the format. - if updated_ctxt32 <= MAX_CTXT { - return InlineCtxt::span(span.lo, span.len, updated_ctxt32 as u16); - } - data = span.data(); - }, - InlineParent(span) => { - updated_ctxt32 = update(SyntaxContext::root()).as_u32(); - // Only if the new context is zero the format will be preserved. - if updated_ctxt32 == 0 { - // Do nothing. - return self; - } - data = span.data(); - }, - PartiallyInterned(span) => { - updated_ctxt32 = update(SyntaxContext::from_u16(span.ctxt)).as_u32(); - // Any small new context excluding zero will preserve the format. - // Zero may change the format to `InlineParent` if parent and len are small enough. - if updated_ctxt32 <= MAX_CTXT && updated_ctxt32 != 0 { - return PartiallyInterned::span(span.index, updated_ctxt32 as u16); - } - data = span.data(); - }, - Interned(span) => { - data = span.data(); - updated_ctxt32 = update(data.ctxt).as_u32(); + let updated_ctxt32 = update(SyntaxContext::from_u16(span.ctxt)).as_u32(); + // Any small new context including zero will preserve the format, + // any big new context will result in a fully interned format. + return if updated_ctxt32 <= MAX_CTXT { + InlineCtxt::span(span.lo, span.len, updated_ctxt32 as u16) + } else { + Interned::span(with_span_interner(|interner| interner.intern(&span.data()))) + }; }, + InlineParent(_span) => {}, + PartiallyInterned(_span) => {}, + Interned(_span) => {}, } - // We could not keep the span in the same inline format, fall back to the complete logic. - data.with_ctxt(SyntaxContext::from_u32(updated_ctxt32)) + let data = self.data_untracked(); + data.with_ctxt(update(data.ctxt)) } // Returns either syntactic context, if it can be retrieved without taking the interner lock,