Skip to content

Commit

Permalink
perf: shrink the size of ZendFrame
Browse files Browse the repository at this point in the history
Previously, each frame was 7 * 64 bits. They are now 3 * 64 bits, at
the cost that every `String` gets copied to a `ThinString`.
  • Loading branch information
morrisonlevi committed Jun 11, 2024
1 parent c7a7d7c commit 43f7779
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 109 deletions.
33 changes: 22 additions & 11 deletions profiling/src/profiling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ 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::thin_str::ThinString;
use crate::timeline::IncludeType;
use crate::{CLOCKS, TAGS};
use chrono::Utc;
use core::{ptr, str};
Expand Down Expand Up @@ -391,7 +393,10 @@ impl TimeCollector {
for frame in &message.value.frames {
let location = Location {
function: Function {
name: frame.function.as_ref(),
name: frame
.function
.as_deref()
.unwrap_or_else(|| frame.extra.as_ref()),
system_name: "",
filename: frame.file.as_deref().unwrap_or(""),
start_line: 0,
Expand Down Expand Up @@ -515,9 +520,6 @@ pub enum UploadMessage {
Upload(Box<UploadRequest>),
}

#[cfg(feature = "timeline")]
const COW_EVAL: Cow<str> = Cow::Borrowed("[eval]");

impl Profiler {
pub fn new(system_settings: &SystemSettings) -> Self {
let fork_barrier = Arc::new(Barrier::new(3));
Expand Down Expand Up @@ -833,9 +835,10 @@ impl Profiler {

match self.prepare_and_send_message(
vec![ZendFrame {
function: COW_EVAL,
file: Some(filename),
function: None,
file: Some(ThinString::from(&filename)),
line,
extra: StaticNames::Eval,
}],
SampleValues {
timeline: duration,
Expand All @@ -861,7 +864,7 @@ impl Profiler {
now: i64,
duration: i64,
filename: String,
include_type: &str,
include_type: Result<IncludeType, u32>,
) {
let mut labels = Profiler::common_labels(Self::TIMELINE_COMPILE_FILE_LABELS.len() + 1);
labels.extend_from_slice(Self::TIMELINE_COMPILE_FILE_LABELS);
Expand All @@ -874,9 +877,14 @@ impl Profiler {

match self.prepare_and_send_message(
vec![ZendFrame {
function: format!("[{include_type}]").into(),
function: None,
file: None,
line: 0,
extra: match include_type {
Ok(IncludeType::Include) => StaticNames::Include,
Ok(IncludeType::Require) => StaticNames::Require,
Err(_type) => StaticNames::UnknownIncludeType,
},
}],
SampleValues {
timeline: duration,
Expand Down Expand Up @@ -910,9 +918,10 @@ impl Profiler {

match self.prepare_and_send_message(
vec![ZendFrame {
function: "[idle]".into(),
function: None,
file: None,
line: 0,
extra: StaticNames::Idle,
}],
SampleValues {
timeline: duration,
Expand Down Expand Up @@ -966,9 +975,10 @@ impl Profiler {

match self.prepare_and_send_message(
vec![ZendFrame {
function: "[gc]".into(),
function: None,
file: None,
line: 0,
extra: StaticNames::Gc,
}],
SampleValues {
timeline: duration,
Expand Down Expand Up @@ -1087,9 +1097,10 @@ mod tests {

fn get_frames() -> Vec<ZendFrame> {
vec![ZendFrame {
function: "foobar()".into(),
function: Some("foobar()".into()),
file: Some("foobar.php".into()),
line: 42,
extra: StaticNames::Empty,
}]
}

Expand Down
Loading

0 comments on commit 43f7779

Please sign in to comment.