Skip to content

Commit

Permalink
refactor: implement AsRef<str> for ConstStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
morrisonlevi committed Dec 23, 2024
1 parent 291378e commit 656b84e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
6 changes: 1 addition & 5 deletions profiling/benches/concatenation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
});
});
Expand Down
61 changes: 30 additions & 31 deletions profiling/src/well_known.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use datadog_alloc::Global;
use std::borrow::Cow;
use std::ops::Deref;

use datadog_thin_str::{ConstStorage, ThinString};

Expand Down Expand Up @@ -44,45 +43,45 @@ pub enum WellKnown {

impl From<WellKnown> 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)
}
}

Expand Down
7 changes: 7 additions & 0 deletions thin-str/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,10 @@ impl<const N: usize> ConstStorage<N> {
unsafe { Storage::from_header(header_ptr) }
}
}

impl<'a, const N: usize> AsRef<str> for ConstStorage<N> {
fn as_ref(&self) -> &str {
// SAFETY: ConstStorage always hold valid strings.
unsafe { core::str::from_utf8_unchecked(&self.data) }
}
}

0 comments on commit 656b84e

Please sign in to comment.