Skip to content

Commit 659848d

Browse files
perf(profiling): avoid FFI call on allocation hot path (#4068)
1 parent 5bf45ce commit 659848d

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

profiling/src/allocation/allocation_ge84.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::allocation::{
2-
allocation_profiling_stats_should_collect, collect_allocation, untrack_allocation,
2+
allocation_profiling_stats_should_collect, collect_allocation, current_execute_data,
3+
untrack_allocation,
34
};
45
use crate::bindings as zend;
56
use crate::PROFILER_NAME;
@@ -306,7 +307,7 @@ unsafe fn alloc_prof_malloc_impl(len: size_t) -> *mut c_void {
306307

307308
// during startup, minit, rinit, ... current_execute_data is null
308309
// we are only interested in allocations during userland operations
309-
if zend::ddog_php_prof_get_current_execute_data().is_null() {
310+
if current_execute_data().is_null() {
310311
return ptr;
311312
}
312313

@@ -505,7 +506,7 @@ unsafe fn alloc_prof_realloc_no_untrack_impl(prev_ptr: *mut c_void, len: size_t)
505506
unsafe fn alloc_prof_realloc_sample(ptr: *mut c_void, len: size_t) -> *mut c_void {
506507
// during startup, minit, rinit, ... current_execute_data is null
507508
// we are only interested in allocations during userland operations
508-
if zend::ddog_php_prof_get_current_execute_data().is_null() {
509+
if current_execute_data().is_null() {
509510
return ptr;
510511
}
511512

profiling/src/allocation/allocation_le83.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::allocation::{
2-
allocation_profiling_stats_should_collect, collect_allocation, untrack_allocation,
2+
allocation_profiling_stats_should_collect, collect_allocation, current_execute_data,
3+
untrack_allocation,
34
};
45
use crate::bindings::{
56
self as zend, datadog_php_install_handler, datadog_php_zif_handler,
@@ -300,7 +301,7 @@ unsafe extern "C" fn alloc_prof_malloc(len: size_t) -> *mut c_void {
300301

301302
// during startup, minit, rinit, ... current_execute_data is null
302303
// we are only interested in allocations during userland operations
303-
if zend::ddog_php_prof_get_current_execute_data().is_null() {
304+
if current_execute_data().is_null() {
304305
return ptr;
305306
}
306307

@@ -431,7 +432,7 @@ unsafe fn alloc_prof_realloc_no_untrack_impl(prev_ptr: *mut c_void, len: size_t)
431432
unsafe fn alloc_prof_realloc_sample(ptr: *mut c_void, len: size_t) -> *mut c_void {
432433
// during startup, minit, rinit, ... current_execute_data is null
433434
// we are only interested in allocations during userland operations
434-
if zend::ddog_php_prof_get_current_execute_data().is_null() {
435+
if current_execute_data().is_null() {
435436
return ptr;
436437
}
437438

profiling/src/allocation/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ pub(crate) unsafe fn get_zend_mm_state() -> *mut Cell<ZendMMState> {
3939
ptr::addr_of_mut!((*globals).zend_mm_state)
4040
}
4141

42+
#[inline(always)]
43+
pub(crate) unsafe fn current_execute_data() -> *mut zend::zend_execute_data {
44+
#[cfg(not(php_zts))]
45+
return ptr::addr_of!(zend::executor_globals.current_execute_data).read();
46+
47+
#[cfg(php_zts)]
48+
zend::ddog_php_prof_get_current_execute_data()
49+
}
50+
4251
/// Macros for accessing ZendMMState from PHP globals.
4352
/// These are shared between PHP 8.3- and 8.4+ implementations.
4453
/// They are exported at the crate root and can be used in submodules.

0 commit comments

Comments
 (0)