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 12, 2024
1 parent c7a7d7c commit d06f75e
Show file tree
Hide file tree
Showing 5 changed files with 356 additions and 130 deletions.
45 changes: 26 additions & 19 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 @@ -1085,14 +1095,6 @@ mod tests {
use datadog_profiling::exporter::Uri;
use log::LevelFilter;

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

pub fn get_system_settings() -> SystemSettings {
SystemSettings {
profiling_enabled: true,
Expand Down Expand Up @@ -1126,7 +1128,12 @@ mod tests {
#[test]
#[cfg(all(feature = "timeline", not(miri)))]
fn profiler_prepare_sample_message_works_cpu_time_and_timeline() {
let frames = get_frames();
let frames = vec![ZendFrame {
function: Some("foobar()".into()),
file: Some("foobar.php".into()),
line: 42,
extra: StaticNames::Empty,
}];
let samples = get_samples();
let labels = Profiler::common_labels(0);
let mut settings = get_system_settings();
Expand Down
Loading

0 comments on commit d06f75e

Please sign in to comment.