Repository: DataDog/dd-trace-php
Pull request #3248: style(prof): use local_key_cell_methods
Author: login: morrisonlevi
Labels: name: profiling
description: Relates to the Continuous Profiler
name: tracing
Created: 2025-05-13T20:23:05Z | Merged: 2025-05-14T04:33:54Z

### Description

Rust added these in v1.73. Cleans up our code a bit. I didn't want mingle them in with a bug fix.

### Reviewer checklist
- [ ] Test coverage seems ok.
- [ ] Appropriate labels assigned.



Diff:
diff --git a/profiling/src/allocation/allocation_ge84.rs b/profiling/src/allocation/allocation_ge84.rs
index a246c69c6a2..71b67da1d45 100644
--- a/profiling/src/allocation/allocation_ge84.rs
+++ b/profiling/src/allocation/allocation_ge84.rs
@@ -359,10 +359,7 @@ unsafe extern "C" fn alloc_prof_malloc(len: size_t) -> *mut c_void {
         return ptr;
     }
 
-    ALLOCATION_PROFILING_STATS.with(|cell| {
-        let mut allocations = cell.borrow_mut();
-        allocations.track_allocation(len)
-    });
+    ALLOCATION_PROFILING_STATS.with_borrow_mut(|allocations| allocations.track_allocation(len));
 
     ptr
 }
@@ -427,10 +424,7 @@ unsafe extern "C" fn alloc_prof_realloc(prev_ptr: *mut c_void, len: size_t) -> *
         return ptr;
     }
 
-    ALLOCATION_PROFILING_STATS.with(|cell| {
-        let mut allocations = cell.borrow_mut();
-        allocations.track_allocation(len)
-    });
+    ALLOCATION_PROFILING_STATS.with_borrow_mut(|allocations| allocations.track_allocation(len));
 
     ptr
 }
diff --git a/profiling/src/allocation/allocation_le83.rs b/profiling/src/allocation/allocation_le83.rs
index afb8da684c0..1f03812b33f 100644
--- a/profiling/src/allocation/allocation_le83.rs
+++ b/profiling/src/allocation/allocation_le83.rs
@@ -350,10 +350,7 @@ unsafe extern "C" fn alloc_prof_malloc(len: size_t) -> *mut c_void {
         return ptr;
     }
 
-    ALLOCATION_PROFILING_STATS.with(|cell| {
-        let mut allocations = cell.borrow_mut();
-        allocations.track_allocation(len)
-    });
+    ALLOCATION_PROFILING_STATS.with_borrow_mut(|allocations| allocations.track_allocation(len));
 
     ptr
 }
@@ -408,10 +405,7 @@ unsafe extern "C" fn alloc_prof_realloc(prev_ptr: *mut c_void, len: size_t) -> *
         return ptr;
     }
 
-    ALLOCATION_PROFILING_STATS.with(|cell| {
-        let mut allocations = cell.borrow_mut();
-        allocations.track_allocation(len)
-    });
+    ALLOCATION_PROFILING_STATS.with_borrow_mut(|allocations| allocations.track_allocation(len));
 
     ptr
 }
diff --git a/profiling/src/allocation/mod.rs b/profiling/src/allocation/mod.rs
index 11efc8f424a..f2e06a6a724 100644
--- a/profiling/src/allocation/mod.rs
+++ b/profiling/src/allocation/mod.rs
@@ -18,7 +18,8 @@ pub mod allocation_le83;
 pub const DEFAULT_ALLOCATION_SAMPLING_INTERVAL: u64 = 1024 * 4096;
 
 /// Sampling distance feed into poison sampling algo
-pub static ALLOCATION_PROFILING_INTERVAL: AtomicU64 = AtomicU64::new(DEFAULT_ALLOCATION_SAMPLING_INTERVAL);
+pub static ALLOCATION_PROFILING_INTERVAL: AtomicU64 =
+    AtomicU64::new(DEFAULT_ALLOCATION_SAMPLING_INTERVAL);
 
 /// This will store the count of allocations (including reallocations) during
 /// a profiling period. This will overflow when doing more than u64::MAX
@@ -122,7 +123,10 @@ pub fn alloc_prof_first_rinit() {
 
     ALLOCATION_PROFILING_INTERVAL.store(sampling_distance as u64, Ordering::SeqCst);
 
-    trace!("Memory allocation profiling initialized with a sampling distance of {} bytes.", ALLOCATION_PROFILING_INTERVAL.load(Ordering::SeqCst));
+    trace!(
+        "Memory allocation profiling initialized with a sampling distance of {} bytes.",
+        ALLOCATION_PROFILING_INTERVAL.load(Ordering::SeqCst)
+    );
 }
 
 pub fn alloc_prof_rinit() {
diff --git a/profiling/src/config.rs b/profiling/src/config.rs
index e1618b09c55..1ecce35921c 100644
--- a/profiling/src/config.rs
+++ b/profiling/src/config.rs
@@ -530,7 +530,6 @@ unsafe fn profiling_allocation_sampling_distance() -> u32 {
     )
 }
 
-
 /// # Safety
 /// This function must only be called after config has been initialized in
 /// rinit, and before it is uninitialized in mshutdown.
diff --git a/profiling/src/exception.rs b/profiling/src/exception.rs
index f10b6e6d8d8..4e217a162dc 100644
--- a/profiling/src/exception.rs
+++ b/profiling/src/exception.rs
@@ -200,10 +200,8 @@ unsafe extern "C" fn exception_profiling_throw_exception_hook(
     // traversed. Fortunately, this behavior can be easily identified by checking for a NULL
     // pointer.
     if exception_profiling && !exception.is_null() {
-        EXCEPTION_PROFILING_STATS.with(|cell| {
-            let mut exceptions = cell.borrow_mut();
-            exceptions.track_exception(exception)
-        });
+        EXCEPTION_PROFILING_STATS
+            .with_borrow_mut(|exceptions| exceptions.track_exception(exception));
     }
 
     if let Some(prev) = PREV_ZEND_THROW_EXCEPTION_HOOK {
diff --git a/profiling/src/io.rs b/profiling/src/io.rs
index 31d6d4eedfa..458f4a516cf 100644
--- a/profiling/src/io.rs
+++ b/profiling/src/io.rs
@@ -245,30 +245,19 @@ unsafe extern "C" fn observed_poll(fds: *mut libc::pollfd, nfds: u64, timeout: c
     let duration = start.elapsed();
 
     if !fds.is_null() {
+        let duration_nanos = duration.as_nanos() as u64;
         if (*fds).revents & 1 == 1 {
             // requested events contains reading
-            SOCKET_READ_TIME_PROFILING_STATS.with(|cell| {
-                let mut io = cell.borrow_mut();
-                io.track(duration.as_nanos() as u64)
-            });
+            SOCKET_READ_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration_nanos));
         } else if (*fds).revents & 4 == 4 {
             // requested events contains writing
-            SOCKET_WRITE_TIME_PROFILING_STATS.with(|cell| {
-                let mut io = cell.borrow_mut();
-                io.track(duration.as_nanos() as u64)
-            });
+            SOCKET_WRITE_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration_nanos));
         } else if (*fds).events & 1 == 1 {
             // socket became readable
-            SOCKET_READ_TIME_PROFILING_STATS.with(|cell| {
-                let mut io = cell.borrow_mut();
-                io.track(duration.as_nanos() as u64)
-            });
+            SOCKET_READ_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration_nanos));
         } else if (*fds).events & 4 == 4 {
             // socket became writeable
-            SOCKET_WRITE_TIME_PROFILING_STATS.with(|cell| {
-                let mut io = cell.borrow_mut();
-                io.track(duration.as_nanos() as u64)
-            });
+            SOCKET_WRITE_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration_nanos));
         }
     }
 
@@ -287,15 +276,9 @@ unsafe extern "C" fn observed_recv(
     let len = ORIG_RECV(socket, buf, length, flags);
     let duration = start.elapsed();
 
-    SOCKET_READ_TIME_PROFILING_STATS.with(|cell| {
-        let mut io = cell.borrow_mut();
-        io.track(duration.as_nanos() as u64)
-    });
+    SOCKET_READ_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration.as_nanos() as u64));
     if len > 0 {
-        SOCKET_READ_SIZE_PROFILING_STATS.with(|cell| {
-            let mut io = cell.borrow_mut();
-            io.track(len as u64)
-        });
+        SOCKET_READ_SIZE_PROFILING_STATS.with_borrow_mut(|io| io.track(len as u64));
     }
 
     len
@@ -313,15 +296,9 @@ unsafe extern "C" fn observed_recvmsg(
     let len = ORIG_RECVMSG(socket, msg, flags);
     let duration = start.elapsed();
 
-    SOCKET_READ_TIME_PROFILING_STATS.with(|cell| {
-        let mut io = cell.borrow_mut();
-        io.track(duration.as_nanos() as u64)
-    });
+    SOCKET_READ_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration.as_nanos() as u64));
     if len > 0 {
-        SOCKET_READ_SIZE_PROFILING_STATS.with(|cell| {
-            let mut io = cell.borrow_mut();
-            io.track(len as u64);
-        });
+        SOCKET_READ_SIZE_PROFILING_STATS.with_borrow_mut(|io| io.track(len as u64));
     }
 
     len
@@ -348,15 +325,9 @@ unsafe extern "C" fn observed_recvfrom(
     let len = ORIG_RECVFROM(socket, buf, length, flags, address, address_len);
     let duration = start.elapsed();
 
-    SOCKET_READ_TIME_PROFILING_STATS.with(|cell| {
-        let mut io = cell.borrow_mut();
-        io.track(duration.as_nanos() as u64)
-    });
+    SOCKET_READ_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration.as_nanos() as u64));
     if len > 0 {
-        SOCKET_READ_SIZE_PROFILING_STATS.with(|cell| {
-            let mut io = cell.borrow_mut();
-            io.track(len as u64)
-        });
+        SOCKET_READ_SIZE_PROFILING_STATS.with_borrow_mut(|io| io.track(len as u64));
     }
 
     len
@@ -374,15 +345,9 @@ unsafe extern "C" fn observed_send(
     let len = ORIG_SEND(socket, buf, length, flags);
     let duration = start.elapsed();
 
-    SOCKET_WRITE_TIME_PROFILING_STATS.with(|cell| {
-        let mut io = cell.borrow_mut();
-        io.track(duration.as_nanos() as u64)
-    });
+    SOCKET_WRITE_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration.as_nanos() as u64));
     if len > 0 {
-        SOCKET_WRITE_SIZE_PROFILING_STATS.with(|cell| {
-            let mut io = cell.borrow_mut();
-            io.track(len as u64)
-        });
+        SOCKET_WRITE_SIZE_PROFILING_STATS.with_borrow_mut(|io| io.track(len as u64));
     }
 
     len
@@ -399,15 +364,9 @@ unsafe extern "C" fn observed_sendmsg(
     let len = ORIG_SENDMSG(socket, msg, flags);
     let duration = start.elapsed();
 
-    SOCKET_WRITE_TIME_PROFILING_STATS.with(|cell| {
-        let mut io = cell.borrow_mut();
-        io.track(duration.as_nanos() as u64)
-    });
+    SOCKET_WRITE_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration.as_nanos() as u64));
     if len > 0 {
-        SOCKET_WRITE_SIZE_PROFILING_STATS.with(|cell| {
-            let mut io = cell.borrow_mut();
-            io.track(len as u64)
-        });
+        SOCKET_WRITE_SIZE_PROFILING_STATS.with_borrow_mut(|io| io.track(len as u64));
     }
 
     len
@@ -429,15 +388,9 @@ unsafe extern "C" fn observed_fwrite(
     let len = ORIG_FWRITE(ptr, size, nobj, stream);
     let duration = start.elapsed();
 
-    FILE_WRITE_TIME_PROFILING_STATS.with(|cell| {
-        let mut io = cell.borrow_mut();
-        io.track(duration.as_nanos() as u64)
-    });
+    FILE_WRITE_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration.as_nanos() as u64));
     if len > 0 {
-        FILE_WRITE_SIZE_PROFILING_STATS.with(|cell| {
-            let mut io = cell.borrow_mut();
-            io.track(len as u64)
-        });
+        FILE_WRITE_SIZE_PROFILING_STATS.with_borrow_mut(|io| io.track(len as u64));
     }
 
     len
@@ -450,26 +403,15 @@ unsafe extern "C" fn observed_write(fd: c_int, buf: *const c_void, count: usize)
     let duration = start.elapsed();
 
     if fd_is_socket(fd) {
-        SOCKET_WRITE_TIME_PROFILING_STATS.with(|cell| {
-            let mut io = cell.borrow_mut();
-            io.track(duration.as_nanos() as u64)
-        });
+        SOCKET_WRITE_TIME_PROFILING_STATS
+            .with_borrow_mut(|io| io.track(duration.as_nanos() as u64));
         if len > 0 {
-            SOCKET_WRITE_SIZE_PROFILING_STATS.with(|cell| {
-                let mut io = cell.borrow_mut();
-                io.track(len as u64)
-            });
+            SOCKET_WRITE_SIZE_PROFILING_STATS.with_borrow_mut(|io| io.track(len as u64));
         }
     } else {
-        FILE_WRITE_TIME_PROFILING_STATS.with(|cell| {
-            let mut io = cell.borrow_mut();
-            io.track(duration.as_nanos() as u64)
-        });
+        FILE_WRITE_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration.as_nanos() as u64));
         if len > 0 {
-            FILE_WRITE_SIZE_PROFILING_STATS.with(|cell| {
-                let mut io = cell.borrow_mut();
-                io.track(len as u64)
-            });
+            FILE_WRITE_SIZE_PROFILING_STATS.with_borrow_mut(|io| io.track(len as u64));
         }
     }
 
@@ -491,14 +433,10 @@ unsafe extern "C" fn observed_fread(
     let len = ORIG_FREAD(ptr, size, nobj, stream);
     let duration = start.elapsed();
 
-    FILE_READ_TIME_PROFILING_STATS.with(|cell| {
-        let mut io = cell.borrow_mut();
-        io.track(duration.as_nanos() as u64)
-    });
-    FILE_READ_SIZE_PROFILING_STATS.with(|cell| {
-        let mut io = cell.borrow_mut();
-        io.track(len as u64)
-    });
+    FILE_READ_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration.as_nanos() as u64));
+    if len > 0 {
+        FILE_READ_SIZE_PROFILING_STATS.with_borrow_mut(|io| io.track(len as u64));
+    }
 
     len
 }
@@ -510,26 +448,14 @@ unsafe extern "C" fn observed_read(fd: c_int, buf: *mut c_void, count: usize) ->
     let duration = start.elapsed();
 
     if fd_is_socket(fd) {
-        SOCKET_READ_TIME_PROFILING_STATS.with(|cell| {
-            let mut io = cell.borrow_mut();
-            io.track(duration.as_nanos() as u64)
-        });
+        SOCKET_READ_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration.as_nanos() as u64));
         if len > 0 {
-            SOCKET_READ_SIZE_PROFILING_STATS.with(|cell| {
-                let mut io = cell.borrow_mut();
-                io.track(len as u64)
-            });
+            SOCKET_READ_SIZE_PROFILING_STATS.with_borrow_mut(|io| io.track(len as u64));
         }
     } else {
-        FILE_READ_TIME_PROFILING_STATS.with(|cell| {
-            let mut io = cell.borrow_mut();
-            io.track(duration.as_nanos() as u64)
-        });
+        FILE_READ_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration.as_nanos() as u64));
         if len > 0 {
-            FILE_READ_SIZE_PROFILING_STATS.with(|cell| {
-                let mut io = cell.borrow_mut();
-                io.track(len as u64)
-            });
+            FILE_READ_SIZE_PROFILING_STATS.with_borrow_mut(|io| io.track(len as u64));
         }
     }
 
@@ -723,10 +649,7 @@ impl<C: IOCollector> IOProfilingStats<C> {
     }
 
     fn track(&mut self, value: u64) {
-        let zend_thread = REQUEST_LOCALS.with(|cell| {
-            let locals = cell.borrow();
-            !locals.vm_interrupt_addr.is_null()
-        });
+        let zend_thread = REQUEST_LOCALS.with_borrow(|locals| !locals.vm_interrupt_addr.is_null());
         if !zend_thread {
             // `curl_exec()` for example will spawn a new thread for name resolution. GOT hooking
             // follows threads and as such we might sample from another (non PHP) thread even in a
diff --git a/profiling/src/lib.rs b/profiling/src/lib.rs
index 08fb054885f..1c2fd346e0c 100644
--- a/profiling/src/lib.rs
+++ b/profiling/src/lib.rs
@@ -493,8 +493,7 @@ extern "C" fn rinit(_type: c_int, _module_number: c_int) -> ZendResult {
     let mut system_settings = unsafe { SystemSettings::get() };
 
     // initialize the thread local storage and cache some items
-    REQUEST_LOCALS.with(|cell| {
-        let mut locals = cell.borrow_mut();
+    REQUEST_LOCALS.with_borrow_mut(|locals| {
         // SAFETY: we are in rinit on a PHP thread.
         locals.vm_interrupt_addr = unsafe { zend::datadog_php_profiling_vm_interrupt_addr() };
         locals.interrupt_count.store(0, Ordering::SeqCst);
@@ -592,13 +591,12 @@ extern "C" fn rinit(_type: c_int, _module_number: c_int) -> ZendResult {
     Profiler::init(system_settings);
 
     if system_settings.profiling_enabled {
-        REQUEST_LOCALS.with(|cell| {
-            let locals = cell.borrow();
+        REQUEST_LOCALS.with_borrow(|locals| {
             let cpu_time_enabled = system_settings.profiling_experimental_cpu_time_enabled;
             let wall_time_enabled = system_settings.profiling_wall_time_enabled;
-            CLOCKS.with(|cell| cell.borrow_mut().initialize(cpu_time_enabled));
+            CLOCKS.with_borrow_mut(|clocks| clocks.initialize(cpu_time_enabled));
 
-            TAGS.with(|cell| {
+            TAGS.set({
                 let mut tags = LAZY_STATICS_TAGS.clone();
                 add_optional_tag(&mut tags, "service", &locals.service);
                 add_optional_tag(&mut tags, "env", &locals.env);
@@ -619,7 +617,7 @@ extern "C" fn rinit(_type: c_int, _module_number: c_int) -> ZendResult {
                 };
                 add_tag(&mut tags, "runtime_engine", runtime_engine);
                 tags.extend_from_slice(&locals.tags);
-                cell.replace(Arc::new(tags));
+                Arc::new(tags)
             });
 
             // Only add interrupt if cpu- or wall-time is enabled.
@@ -636,7 +634,7 @@ extern "C" fn rinit(_type: c_int, _module_number: c_int) -> ZendResult {
             }
         });
     } else {
-        TAGS.with(|cell| cell.replace(Arc::default()));
+        TAGS.set(Arc::default());
     }
 
     #[cfg(feature = "allocation_profiling")]
@@ -684,8 +682,7 @@ extern "C" fn rshutdown(_type: c_int, _module_number: c_int) -> ZendResult {
 
     profiling::stack_walking::rshutdown();
 
-    REQUEST_LOCALS.with(|cell| {
-        let locals = cell.borrow();
+    REQUEST_LOCALS.with_borrow(|locals| {
         let system_settings = locals.system_settings();
 
         // The interrupt is only added if CPU- or wall-time are enabled BUT
@@ -721,8 +718,7 @@ unsafe extern "C" fn minfo(module_ptr: *mut zend::ModuleEntry) {
 
     let module = &*module_ptr;
 
-    REQUEST_LOCALS.with(|cell| {
-        let locals = cell.borrow();
+    REQUEST_LOCALS.with_borrow(|locals| {
         let system_settings = locals.system_settings();
         let yes = c"true".as_ptr();
         let yes_exp = c"true (all experimental features enabled)".as_ptr();
@@ -1004,8 +1000,7 @@ extern "C" fn shutdown(extension: *mut ZendExtension) {
 /// Notifies the profiler a trace has finished so it can update information
 /// for Endpoint Profiling.
 fn notify_trace_finished(local_root_span_id: u64, span_type: Cow<str>, resource: Cow<str>) {
-    REQUEST_LOCALS.with(|cell| {
-        let locals = cell.borrow();
+    REQUEST_LOCALS.with_borrow(|locals| {
         let system_settings = locals.system_settings();
         if system_settings.profiling_enabled && system_settings.profiling_endpoint_collection_enabled {
             // Only gather Endpoint Profiling data for web spans, partly for PII reasons.
diff --git a/profiling/src/profiling/mod.rs b/profiling/src/profiling/mod.rs
index d0288af1956..eb04ebf777e 100644
--- a/profiling/src/profiling/mod.rs
+++ b/profiling/src/profiling/mod.rs
@@ -17,7 +17,7 @@ use crate::bindings::ddog_php_prof_get_active_fiber_test as ddog_php_prof_get_ac
 
 use crate::bindings::{datadog_php_profiling_get_profiling_context, zend_execute_data};
 use crate::config::SystemSettings;
-use crate::{CLOCKS, TAGS};
+use crate::{Clocks, CLOCKS, TAGS};
 use chrono::Utc;
 use core::mem::forget;
 use crossbeam_channel::{Receiver, Sender, TrySendError};
@@ -916,7 +916,7 @@ impl Profiler {
         match result {
             Ok(frames) => {
                 let depth = frames.len();
-                let (wall_time, cpu_time) = CLOCKS.with(|cell| cell.borrow_mut().rotate_clocks());
+                let (wall_time, cpu_time) = CLOCKS.with_borrow_mut(Clocks::rotate_clocks);
 
                 let labels = Profiler::common_labels(0);
                 let n_labels = labels.len();
@@ -1537,7 +1537,7 @@ impl Profiler {
         let sample_types = self.sample_types_filter.sample_types();
         let sample_values = self.sample_types_filter.filter(samples);
 
-        let tags = TAGS.with(|cell| Arc::clone(&cell.borrow()));
+        let tags = TAGS.with_borrow(Arc::clone);
 
         SampleMessage {
             key: ProfileIndex { sample_types, tags },
diff --git a/profiling/src/profiling/stack_walking.rs b/profiling/src/profiling/stack_walking.rs
index ea7d2d27502..ad962785377 100644
--- a/profiling/src/profiling/stack_walking.rs
+++ b/profiling/src/profiling/stack_walking.rs
@@ -203,8 +203,7 @@ mod detail {
 
     #[inline]
     pub fn rshutdown() {
-        FUNCTION_CACHE_STATS.with(|cell| {
-            let stats = cell.borrow();
+        FUNCTION_CACHE_STATS.with_borrow(|stats| {
             let hit_rate = stats.hit_rate();
             debug!("Process cumulative {stats:?} hit_rate: {hit_rate}");
         });
@@ -240,8 +239,7 @@ mod detail {
         #[cfg(feature = "tracing")]
         let _span = tracing::trace_span!("collect_stack_sample").entered();
 
-        CACHED_STRINGS.with(|cell| {
-            let string_set: &mut StringSet = &mut cell.borrow_mut();
+        CACHED_STRINGS.with_borrow_mut(|string_set| {
             let max_depth = 512;
             let mut samples = Vec::with_capacity(max_depth >> 3);
             let mut execute_data_ptr = top_execute_data;
@@ -322,8 +320,7 @@ mod detail {
                 let (file, line) = handle_file_cache_slot(execute_data, &mut string_cache);
 
                 let cache_slots = string_cache.cache_slots;
-                FUNCTION_CACHE_STATS.with(|cell| {
-                    let mut stats = cell.borrow_mut();
+                FUNCTION_CACHE_STATS.with_borrow_mut(|stats| {
                     if cache_slots[0] == 0 {
                         stats.missed += 1;
                     } else {
@@ -335,10 +332,7 @@ mod detail {
             }
 
             None => {
-                FUNCTION_CACHE_STATS.with(|cell| {
-                    let mut stats = cell.borrow_mut();
-                    stats.not_applicable += 1;
-                });
+                FUNCTION_CACHE_STATS.with_borrow_mut(|stats| stats.not_applicable += 1);
                 let function = extract_function_name(func);
                 let (file, line) = extract_file_and_line(execute_data);
                 (function, file, line)
diff --git a/profiling/src/timeline.rs b/profiling/src/timeline.rs
index 1f957f65695..34fd0c4c05b 100644
--- a/profiling/src/timeline.rs
+++ b/profiling/src/timeline.rs
@@ -583,9 +583,7 @@ pub(crate) fn timeline_ginit() {
     // During GINIT in "this" thread, the request locals are not initialized, which happens in
     // RINIT, so we currently do not know if profile is enabled at all and if, if timeline is
     // enabled. That's why we raise this flag here and read it in RINIT.
-    IS_NEW_THREAD.with(|cell| {
-        cell.set(true);
-    });
+    IS_NEW_THREAD.set(true);
 }
 
 #[cfg(php_zts)]


Reviews:
user: login: morrisonlevi
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-05-13T20:23:44Z
user: login: bwoebi
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-05-14T00:07:40Z
user: login: bwoebi
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-05-14T00:08:42Z
user: login: bwoebi
body: Looks good :-)
state: APPROVED
author_association: COLLABORATOR
submitted_at: 2025-05-14T00:11:29Z
user: login: realFlowControl
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-14T05:19:27Z

Review comments:
diff_hunk: @@ -491,14 +433,10 @@ unsafe extern "C" fn observed_fread(
     let len = ORIG_FREAD(ptr, size, nobj, stream);
     let duration = start.elapsed();
 
-    FILE_READ_TIME_PROFILING_STATS.with(|cell| {
-        let mut io = cell.borrow_mut();
-        io.track(duration.as_nanos() as u64)
-    });
-    FILE_READ_SIZE_PROFILING_STATS.with(|cell| {
-        let mut io = cell.borrow_mut();
-        io.track(len as u64)
-    });
+    FILE_READ_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration.as_nanos() as u64));
+    if len > 0 {
path: profiling/src/io.rs
user: login: morrisonlevi
body: Most other place guard this with a `len > 0` check, but this one didn't. Added for clarity, let me know if it's not desired.
created_at: 2025-05-13T20:23:44Z
author_association: COLLABORATOR
diff_hunk: @@ -491,14 +433,10 @@ unsafe extern "C" fn observed_fread(
     let len = ORIG_FREAD(ptr, size, nobj, stream);
     let duration = start.elapsed();
 
-    FILE_READ_TIME_PROFILING_STATS.with(|cell| {
-        let mut io = cell.borrow_mut();
-        io.track(duration.as_nanos() as u64)
-    });
-    FILE_READ_SIZE_PROFILING_STATS.with(|cell| {
-        let mut io = cell.borrow_mut();
-        io.track(len as u64)
-    });
+    FILE_READ_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration.as_nanos() as u64));
+    if len > 0 {
path: profiling/src/io.rs
user: login: bwoebi
body: This seems to make sense :-)
created_at: 2025-05-14T00:07:40Z
author_association: COLLABORATOR
diff_hunk: @@ -723,11 +649,7 @@ impl<C: IOCollector> IOProfilingStats<C> {
     }
 
     fn track(&mut self, value: u64) {
-        let zend_thread = REQUEST_LOCALS.with(|cell| {
-            let locals = cell.borrow();
-            !locals.vm_interrupt_addr.is_null()
-        });
-        if !zend_thread {
+        if !REQUEST_LOCALS.with_borrow(|locals| !locals.vm_interrupt_addr.is_null()) {
path: profiling/src/io.rs
user: login: bwoebi
body: You may want to keep the variable for documentation purposes. !vm_interrupt_addr.is_null() does not convey that a "is zend_thread" check was meant.
created_at: 2025-05-14T00:08:42Z
author_association: COLLABORATOR
diff_hunk: @@ -491,14 +433,10 @@ unsafe extern "C" fn observed_fread(
     let len = ORIG_FREAD(ptr, size, nobj, stream);
     let duration = start.elapsed();
 
-    FILE_READ_TIME_PROFILING_STATS.with(|cell| {
-        let mut io = cell.borrow_mut();
-        io.track(duration.as_nanos() as u64)
-    });
-    FILE_READ_SIZE_PROFILING_STATS.with(|cell| {
-        let mut io = cell.borrow_mut();
-        io.track(len as u64)
-    });
+    FILE_READ_TIME_PROFILING_STATS.with_borrow_mut(|io| io.track(duration.as_nanos() as u64));
+    if len > 0 {
path: profiling/src/io.rs
user: login: realFlowControl
body: Your patch is correct, thanks
created_at: 2025-05-14T05:19:27Z
author_association: MEMBER

PR comments:
user: login: codecov-commenter
created_at: 2025-05-13T20:31:39Z
body: ## [Codecov](https://app.codecov.io/gh/DataDog/dd-trace-php/pull/3248?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=DataDog) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 79.28%. Comparing base [(`0af25f0`)](https://app.codecov.io/gh/DataDog/dd-trace-php/commit/0af25f0955e7f073e41b9568c02f1ba5b1553554?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=DataDog) to head [(`03a1edb`)](https://app.codecov.io/gh/DataDog/dd-trace-php/commit/03a1edb95e77f6bb189e27184d197ff926fe44b3?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=DataDog).
> Report is 65 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


[![Impacted file tree graph](https://app.codecov.io/gh/DataDog/dd-trace-php/pull/3248/graphs/tree.svg?width=650&height=150&src=pr&token=eXio8H7vwF&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=DataDog)](https://app.codecov.io/gh/DataDog/dd-trace-php/pull/3248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=DataDog)

```diff
@@             Coverage Diff              @@
##             master    #3248      +/-   ##
============================================
+ Coverage     79.26%   79.28%   +0.01%     
  Complexity     2948     2948              
============================================
  Files           118      118              
  Lines         11633    11633              
============================================
+ Hits           9221     9223       +2     
+ Misses         2412     2410       -2     
```

| [Flag](https://app.codecov.io/gh/DataDog/dd-trace-php/pull/3248/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=DataDog) | Coverage Δ | |
|---|---|---|
| [tracer-php](https://app.codecov.io/gh/DataDog/dd-trace-php/pull/3248/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=DataDog) | `79.28% <ø> (+0.01%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=DataDog#carryforward-flags-in-the-pull-request-comment) to find out more.

[see 1 file with indirect coverage changes](https://app.codecov.io/gh/DataDog/dd-trace-php/pull/3248/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=DataDog)

------

[Continue to review full report in Codecov by Sentry](https://app.codecov.io/gh/DataDog/dd-trace-php/pull/3248?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=DataDog).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=DataDog)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://app.codecov.io/gh/DataDog/dd-trace-php/pull/3248?dropdown=coverage&src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=DataDog). Last update [0af25f0...03a1edb](https://app.codecov.io/gh/DataDog/dd-trace-php/pull/3248?dropdown=coverage&src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=DataDog). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=DataDog).

</details>
<details><summary> :rocket: New features to boost your workflow: </summary>

- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
author_association: NONE
user: login: pr-commenter[bot]
created_at: 2025-05-13T20:48:32Z
body: ## Benchmarks [ profiler ]

Benchmark execution time: 2025-05-14 00:28:28

Comparing candidate commit 03a1edb95e77f6bb189e27184d197ff926fe44b3 in PR branch `levi/format` with baseline commit 0af25f0955e7f073e41b9568c02f1ba5b1553554 in branch `master`.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 30 metrics, 6 unstable metrics.


author_association: NONE