Skip to content

Commit 69d9061

Browse files
committed
refactor(profiling): use libdd-profiling's ThinStr
1 parent cf7ba04 commit 69d9061

4 files changed

Lines changed: 26 additions & 239 deletions

File tree

profiling/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ pub mod module_globals;
77
pub mod profiling;
88
mod pthread;
99
mod sapi;
10-
mod thin_str;
1110
mod wall_time;
1211

1312
#[cfg(php_run_time_cache)]

profiling/src/profiling/stack_walking.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ unsafe fn extract_file_and_line(
162162
mod detail {
163163
use super::*;
164164
use crate::string_set::StringSet;
165-
use crate::thin_str::ThinStr;
166165
use crate::{RefCellExt, RefCellExtError};
166+
use libdd_profiling::profiles::collections::ThinStr;
167167
use log::{debug, trace};
168168
use std::cell::RefCell;
169-
use std::ptr::NonNull;
169+
use std::ffi::c_void;
170170

171171
struct StringCache<'a> {
172172
/// Refers to a function's run time cache reserved by this extension.
@@ -188,23 +188,21 @@ mod detail {
188188
debug_assert!(slot < self.cache_slots.len());
189189
let cached = unsafe { self.cache_slots.get_unchecked_mut(slot) };
190190

191-
let ptr = *cached as *mut u8;
192-
match NonNull::new(ptr) {
191+
let ptr = *cached as *mut c_void;
192+
match std::ptr::NonNull::new(ptr) {
193193
Some(non_null) => {
194-
// SAFETY: transmuting ThinStr from its repr.
195-
let thin_str: ThinStr = unsafe { core::mem::transmute(non_null) };
196-
// SAFETY: the string set is only reset between requests,
197-
// so this ThinStr points into the same string set that
198-
// created it.
194+
// SAFETY: the raw pointer was produced by ThinStr::into_raw
195+
// and the string set (which owns the backing memory) is
196+
// still alive because it is only reset between requests.
197+
let thin_str: ThinStr = unsafe { ThinStr::from_raw(non_null) };
199198
let str = unsafe { self.string_set.get_thin_str(thin_str) };
200199
Some(str.to_string())
201200
}
202201
None => {
203202
let string = f()?;
204203
let thin_str = self.string_set.insert(&string);
205-
// SAFETY: transmuting ThinStr into its repr.
206-
let non_null: NonNull<u8> = unsafe { core::mem::transmute(thin_str) };
207-
*cached = non_null.as_ptr() as usize;
204+
let raw = thin_str.into_raw();
205+
*cached = raw.as_ptr() as usize;
208206
Some(string)
209207
}
210208
}

profiling/src/string_set.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
use crate::thin_str::ThinStr;
21
use core::hash;
32
use core::ops::Deref;
4-
use libdd_alloc::{ChainAllocator, VirtualAllocator};
3+
use libdd_alloc::{AllocError, Allocator, ChainAllocator, VirtualAllocator};
4+
use libdd_profiling::profiles::collections::ThinStr;
55

66
type Hasher = hash::BuildHasherDefault<rustc_hash::FxHasher>;
77
type HashSet<K> = std::collections::HashSet<K, Hasher>;
88

9+
/// Allocates and constructs a [`ThinStr`] in one step.
10+
///
11+
/// This combines [`ThinStr::try_allocate_for`] and [`ThinStr::try_from_str_in`]
12+
/// for convenience. The returned [`ThinStr`] borrows from the allocation made
13+
/// by `alloc`, so the allocator must outlive the returned reference.
14+
fn try_new_thin_str_in<'a, A: Allocator>(s: &str, alloc: &'a A) -> Result<ThinStr<'a>, AllocError> {
15+
let obj = ThinStr::try_allocate_for(s, alloc)?;
16+
// SAFETY: `obj` was just allocated by us, no other references exist,
17+
// so we can safely create a `&mut [MaybeUninit<u8>]` from it.
18+
let uninit = unsafe { &mut *obj.as_ptr() };
19+
ThinStr::try_from_str_in(s, uninit)
20+
}
21+
922
/// Holds unique strings and provides [StringId]s that correspond to the order
1023
/// that the strings were inserted.
1124
pub struct StringSet {
@@ -77,7 +90,7 @@ impl StringSet {
7790
// No match. Make a new string in the arena, and fudge its
7891
// lifetime to appease the borrow checker.
7992
let new_str = {
80-
let s = ThinStr::try_from_str_in(str, &self.arena)
93+
let s = try_new_thin_str_in(str, &self.arena)
8194
.expect("allocation for StringSet::insert to succeed");
8295

8396
// SAFETY: all references to this value get re-narrowed to

profiling/src/thin_str.rs

Lines changed: 0 additions & 223 deletions
This file was deleted.

0 commit comments

Comments
 (0)