From 656b84e646b2489cee00490627569e3e953395a9 Mon Sep 17 00:00:00 2001 From: Levi Morrison Date: Mon, 23 Dec 2024 16:24:23 -0700 Subject: [PATCH] refactor: implement AsRef for ConstStorage --- profiling/benches/concatenation.rs | 6 +-- profiling/src/well_known.rs | 61 +++++++++++++++--------------- thin-str/src/lib.rs | 7 ++++ 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/profiling/benches/concatenation.rs b/profiling/benches/concatenation.rs index 96239e2576..5d9e4c22da 100644 --- a/profiling/benches/concatenation.rs +++ b/profiling/benches/concatenation.rs @@ -68,11 +68,7 @@ fn bench_concatenation_internal2(c: &mut Criterion) { c.bench_function("bench_concatenation_internal2", |b| { b.iter(|| { for _ in 1..=100 { - _ = std::hint::black_box(extract_function_name_v2( - b"standard", - b"", - b"file", - )) + _ = std::hint::black_box(extract_function_name_v2(b"standard", b"", b"file")) } }); }); diff --git a/profiling/src/well_known.rs b/profiling/src/well_known.rs index 21302e6a71..81aefc97b4 100644 --- a/profiling/src/well_known.rs +++ b/profiling/src/well_known.rs @@ -1,6 +1,5 @@ use datadog_alloc::Global; use std::borrow::Cow; -use std::ops::Deref; use datadog_thin_str::{ConstStorage, ThinString}; @@ -44,45 +43,45 @@ pub enum WellKnown { impl From for Cow<'static, str> { fn from(well_known: WellKnown) -> Self { - let storage: &'static _ = match well_known { - WellKnown::Empty => inline_strings::EMPTY.as_storage(), - - WellKnown::BracketedEval => inline_strings::BRACKETED_EVAL.as_storage(), - WellKnown::BracketedFatal => inline_strings::BRACKETED_FATAL.as_storage(), - WellKnown::BracketedGc => inline_strings::BRACKETED_GC.as_storage(), - WellKnown::BracketedIdle => inline_strings::BRACKETED_IDLE.as_storage(), - WellKnown::BracketedInclude => inline_strings::BRACKETED_INCLUDE.as_storage(), - WellKnown::BracketedRequire => inline_strings::BRACKETED_REQUIRE.as_storage(), - WellKnown::BracketedSelect => inline_strings::BRACKETED_SELECT.as_storage(), - WellKnown::BracketedSleeping => inline_strings::BRACKETED_SLEEPING.as_storage(), - WellKnown::BracketedTruncated => inline_strings::BRACKETED_TRUNCATED.as_storage(), + let str: &'static str = match well_known { + WellKnown::Empty => inline_strings::EMPTY.as_ref(), + + WellKnown::BracketedEval => inline_strings::BRACKETED_EVAL.as_ref(), + WellKnown::BracketedFatal => inline_strings::BRACKETED_FATAL.as_ref(), + WellKnown::BracketedGc => inline_strings::BRACKETED_GC.as_ref(), + WellKnown::BracketedIdle => inline_strings::BRACKETED_IDLE.as_ref(), + WellKnown::BracketedInclude => inline_strings::BRACKETED_INCLUDE.as_ref(), + WellKnown::BracketedRequire => inline_strings::BRACKETED_REQUIRE.as_ref(), + WellKnown::BracketedSelect => inline_strings::BRACKETED_SELECT.as_ref(), + WellKnown::BracketedSleeping => inline_strings::BRACKETED_SLEEPING.as_ref(), + WellKnown::BracketedTruncated => inline_strings::BRACKETED_TRUNCATED.as_ref(), WellKnown::BracketedUnknownIncludeType => { - inline_strings::BRACKETED_UNKNOWN_INCLUDE_TYPE.as_storage() + inline_strings::BRACKETED_UNKNOWN_INCLUDE_TYPE.as_ref() } - WellKnown::Compilation => inline_strings::COMPILATION.as_storage(), - WellKnown::Engine => inline_strings::ENGINE.as_storage(), - WellKnown::Fatal => inline_strings::FATAL.as_storage(), - WellKnown::Idle => inline_strings::IDLE.as_storage(), - WellKnown::Include => inline_strings::INCLUDE.as_storage(), - WellKnown::Induced => inline_strings::INDUCED.as_storage(), - WellKnown::OpcacheRestart => inline_strings::OPCACHE_RESTART.as_storage(), - WellKnown::PhpOpenTag => inline_strings::PHP_OPEN_TAG.as_storage(), - WellKnown::Require => inline_strings::REQUIRE.as_storage(), - WellKnown::Select => inline_strings::SELECT.as_storage(), - WellKnown::Sleeping => inline_strings::SLEEPING.as_storage(), - WellKnown::Unknown => inline_strings::UNKNOWN.as_storage(), + WellKnown::Compilation => inline_strings::COMPILATION.as_ref(), + WellKnown::Engine => inline_strings::ENGINE.as_ref(), + WellKnown::Fatal => inline_strings::FATAL.as_ref(), + WellKnown::Idle => inline_strings::IDLE.as_ref(), + WellKnown::Include => inline_strings::INCLUDE.as_ref(), + WellKnown::Induced => inline_strings::INDUCED.as_ref(), + WellKnown::OpcacheRestart => inline_strings::OPCACHE_RESTART.as_ref(), + WellKnown::PhpOpenTag => inline_strings::PHP_OPEN_TAG.as_ref(), + WellKnown::Require => inline_strings::REQUIRE.as_ref(), + WellKnown::Select => inline_strings::SELECT.as_ref(), + WellKnown::Sleeping => inline_strings::SLEEPING.as_ref(), + WellKnown::Unknown => inline_strings::UNKNOWN.as_ref(), #[cfg(php_zts)] - WellKnown::BracketedThreadStart => inline_strings::BRACKETED_THREAD_START.as_storage(), + WellKnown::BracketedThreadStart => inline_strings::BRACKETED_THREAD_START.as_ref(), #[cfg(php_zts)] - WellKnown::BracketedThreadStop => inline_strings::BRACKETED_THREAD_STOP.as_storage(), + WellKnown::BracketedThreadStop => inline_strings::BRACKETED_THREAD_STOP.as_ref(), #[cfg(php_zts)] - WellKnown::ThreadStart => inline_strings::THREAD_START.as_storage(), + WellKnown::ThreadStart => inline_strings::THREAD_START.as_ref(), #[cfg(php_zts)] - WellKnown::ThreadStop => inline_strings::THREAD_STOP.as_storage(), + WellKnown::ThreadStop => inline_strings::THREAD_STOP.as_ref(), }; - Cow::Borrowed(storage.deref()) + Cow::Borrowed(str) } } diff --git a/thin-str/src/lib.rs b/thin-str/src/lib.rs index 54827ce07e..b494cd97ee 100644 --- a/thin-str/src/lib.rs +++ b/thin-str/src/lib.rs @@ -123,3 +123,10 @@ impl ConstStorage { unsafe { Storage::from_header(header_ptr) } } } + +impl<'a, const N: usize> AsRef for ConstStorage { + fn as_ref(&self) -> &str { + // SAFETY: ConstStorage always hold valid strings. + unsafe { core::str::from_utf8_unchecked(&self.data) } + } +}