Skip to content

Commit ec26aa7

Browse files
committed
refactor(prof): Labels have strs directly, not Option<str>
1 parent 64fd851 commit ec26aa7

File tree

8 files changed

+100
-133
lines changed

8 files changed

+100
-133
lines changed

profiling-ffi/src/profiles/datatypes.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,7 @@ impl<'a> TryFrom<&'a Label<'a>> for api::Label<'a> {
319319
fn try_from(label: &'a Label<'a>) -> Result<Self, Self::Error> {
320320
let key = label.key.try_to_utf8()?;
321321
let str = label.str.try_to_utf8()?;
322-
let str = if str.is_empty() { None } else { Some(str) };
323322
let num_unit = label.num_unit.try_to_utf8()?;
324-
let num_unit = if num_unit.is_empty() {
325-
None
326-
} else {
327-
Some(num_unit)
328-
};
329323

330324
Ok(Self {
331325
key,
@@ -340,13 +334,7 @@ impl<'a> From<&'a Label<'a>> for api::StringIdLabel {
340334
fn from(label: &'a Label<'a>) -> Self {
341335
let key = label.key_id;
342336
let str = label.str_id;
343-
let str = if str.value == 0 { None } else { Some(str) };
344337
let num_unit = label.num_unit_id;
345-
let num_unit = if num_unit.value == 0 {
346-
None
347-
} else {
348-
Some(num_unit)
349-
};
350338

351339
Self {
352340
key,

profiling-ffi/src/profiles/interning_api.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ pub unsafe extern "C" fn ddog_prof_Profile_intern_label_num(
7070
key: GenerationalId<StringId>,
7171
val: i64,
7272
) -> Result<GenerationalId<LabelId>> {
73-
wrap_with_ffi_result!({ profile_ptr_to_inner(profile)?.intern_label_num(key, val, None) })
73+
wrap_with_ffi_result!({
74+
profile_ptr_to_inner(profile)?.intern_label_num(
75+
key,
76+
val,
77+
GenerationalId::new_immortal(StringId::ZERO),
78+
)
79+
})
7480
}
7581

7682
/// This function interns its argument into the profiler.
@@ -93,7 +99,7 @@ pub unsafe extern "C" fn ddog_prof_Profile_intern_label_num_with_unit(
9399
val: i64,
94100
unit: GenerationalId<StringId>,
95101
) -> Result<GenerationalId<LabelId>> {
96-
wrap_with_ffi_result!({ profile_ptr_to_inner(profile)?.intern_label_num(key, val, Some(unit)) })
102+
wrap_with_ffi_result!({ profile_ptr_to_inner(profile)?.intern_label_num(key, val, unit) })
97103
}
98104

99105
/// This function interns its argument into the profiler.

profiling-replayer/src/replayer.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,9 @@ impl<'pprof> Replayer<'pprof> {
9595
.map(|label| {
9696
Ok(api::Label {
9797
key: profile_index.get_string(label.key)?,
98-
str: if label.str == 0 {
99-
None
100-
} else {
101-
Some(profile_index.get_string(label.str)?)
102-
},
98+
str: profile_index.get_string(label.str)?,
10399
num: label.num,
104-
num_unit: if label.num_unit == 0 {
105-
None
106-
} else {
107-
Some(profile_index.get_string(label.num_unit)?)
108-
},
100+
num_unit: profile_index.get_string(label.num_unit)?,
109101
})
110102
})
111103
.collect();
@@ -126,9 +118,9 @@ impl<'pprof> Replayer<'pprof> {
126118
"local root span ids of zero do not make sense"
127119
);
128120

129-
let endpoint_value = match endpoint_label.str {
130-
Some(v) => v,
131-
None => anyhow::bail!("expected trace endpoint label value to have a string"),
121+
let endpoint_value = endpoint_label.str;
122+
if endpoint_value.is_empty() {
123+
anyhow::bail!("expected trace endpoint label value to have a string")
132124
};
133125

134126
endpoint_info.replace((local_root_span_id, endpoint_value));

profiling/src/api.rs

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ pub struct ManagedStringId {
3131
}
3232

3333
impl ManagedStringId {
34-
pub fn empty() -> Self {
34+
pub const fn empty() -> Self {
3535
Self::new(0)
3636
}
3737

38-
pub fn new(value: u32) -> Self {
38+
pub const fn new(value: u32) -> Self {
3939
ManagedStringId { value }
4040
}
4141
}
@@ -130,7 +130,7 @@ pub struct Label<'a> {
130130
pub key: &'a str,
131131

132132
/// At most one of the following must be present
133-
pub str: Option<&'a str>,
133+
pub str: &'a str,
134134
pub num: i64,
135135

136136
/// Should only be present when num is present.
@@ -140,7 +140,7 @@ pub struct Label<'a> {
140140
/// Consumers may also interpret units like "bytes" and "kilobytes" as memory
141141
/// units and units like "seconds" and "nanoseconds" as time units,
142142
/// and apply appropriate unit conversions to these.
143-
pub num_unit: Option<&'a str>,
143+
pub num_unit: &'a str,
144144
}
145145

146146
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
@@ -149,16 +149,16 @@ pub struct StringIdLabel {
149149
pub key: ManagedStringId,
150150

151151
/// At most one of the following must be present
152-
pub str: Option<ManagedStringId>,
152+
pub str: ManagedStringId,
153153
pub num: i64,
154154

155155
/// Should only be present when num is present.
156-
pub num_unit: Option<ManagedStringId>,
156+
pub num_unit: ManagedStringId,
157157
}
158158

159159
impl Label<'_> {
160160
pub fn uses_at_most_one_of_str_and_num(&self) -> bool {
161-
self.str.is_none() || (self.num == 0 && self.num_unit.is_none())
161+
self.str.is_empty() || (self.num == 0 && self.num_unit.is_empty())
162162
}
163163
}
164164

@@ -400,17 +400,9 @@ impl<'a> TryFrom<&'a pprof::Profile> for Profile<'a> {
400400
for label in sample.labels.iter() {
401401
labels.push(Label {
402402
key: string_table_fetch(pprof, label.key)?,
403-
str: if label.str == 0 {
404-
None
405-
} else {
406-
Some(string_table_fetch(pprof, label.str)?)
407-
},
403+
str: string_table_fetch(pprof, label.str)?,
408404
num: label.num,
409-
num_unit: if label.num_unit == 0 {
410-
None
411-
} else {
412-
Some(string_table_fetch(pprof, label.num_unit)?)
413-
},
405+
num_unit: string_table_fetch(pprof, label.num_unit)?,
414406
})
415407
}
416408
let sample = Sample {
@@ -439,49 +431,49 @@ mod tests {
439431
fn label_uses_at_most_one_of_str_and_num() {
440432
let label = Label {
441433
key: "name",
442-
str: Some("levi"),
434+
str: "levi",
443435
num: 0,
444-
num_unit: Some("name"), // can't use num_unit with str
436+
num_unit: "name", // can't use num_unit with str
445437
};
446438
assert!(!label.uses_at_most_one_of_str_and_num());
447439

448440
let label = Label {
449441
key: "name",
450-
str: Some("levi"),
442+
str: "levi",
451443
num: 10, // can't use num with str
452-
num_unit: None,
444+
num_unit: "",
453445
};
454446
assert!(!label.uses_at_most_one_of_str_and_num());
455447

456448
let label = Label {
457449
key: "name",
458-
str: Some("levi"),
450+
str: "levi",
459451
num: 0,
460-
num_unit: None,
452+
num_unit: "",
461453
};
462454
assert!(label.uses_at_most_one_of_str_and_num());
463455

464456
let label = Label {
465457
key: "process_id",
466-
str: None,
458+
str: "",
467459
num: 0,
468-
num_unit: None,
460+
num_unit: "",
469461
};
470462
assert!(label.uses_at_most_one_of_str_and_num());
471463

472464
let label = Label {
473465
key: "local root span id",
474-
str: None,
466+
str: "",
475467
num: 10901,
476-
num_unit: None,
468+
num_unit: "",
477469
};
478470
assert!(label.uses_at_most_one_of_str_and_num());
479471

480472
let label = Label {
481473
key: "duration",
482-
str: None,
474+
str: "",
483475
num: 12345,
484-
num_unit: Some("nanoseconds"),
476+
num_unit: "nanoseconds",
485477
};
486478
assert!(label.uses_at_most_one_of_str_and_num());
487479
}

profiling/src/internal/label.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use super::*;
66
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
77
pub enum LabelValue {
88
Str(StringId),
9-
Num {
10-
num: i64,
11-
num_unit: Option<StringId>,
12-
},
9+
Num { num: i64, num_unit: StringId },
1310
}
1411

1512
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
@@ -35,7 +32,7 @@ impl Label {
3532
&self.value
3633
}
3734

38-
pub fn num(key: StringId, num: i64, num_unit: Option<StringId>) -> Self {
35+
pub fn num(key: StringId, num: i64, num_unit: StringId) -> Self {
3936
Self {
4037
key,
4138
value: LabelValue::Num { num, num_unit },
@@ -70,7 +67,7 @@ impl From<&Label> for pprof::Label {
7067
key,
7168
str: 0,
7269
num,
73-
num_unit: num_unit.map(StringId::into_raw_id).unwrap_or_default(),
70+
num_unit: num_unit.into_raw_id(),
7471
},
7572
}
7673
}

profiling/src/internal/profile/fuzz_tests.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub struct Label {
4343
pub key: Box<str>,
4444

4545
/// At most one of the following must be present
46-
pub str: Option<Box<str>>,
46+
pub str: Box<str>,
4747
pub num: i64,
4848

4949
/// Should only be present when num is present.
@@ -53,16 +53,16 @@ pub struct Label {
5353
/// Consumers may also interpret units like "bytes" and "kilobytes" as memory
5454
/// units and units like "seconds" and "nanoseconds" as time units,
5555
/// and apply appropriate unit conversions to these.
56-
pub num_unit: Option<Box<str>>,
56+
pub num_unit: Box<str>,
5757
}
5858

5959
impl<'a> From<&'a Label> for api::Label<'a> {
6060
fn from(value: &'a Label) -> Self {
6161
Self {
6262
key: &value.key,
63-
str: value.str.as_deref(),
63+
str: &value.str,
6464
num: value.num,
65-
num_unit: value.num_unit.as_deref(),
65+
num_unit: &value.num_unit,
6666
}
6767
}
6868
}
@@ -193,10 +193,10 @@ pub struct Sample {
193193

194194
#[cfg(test)]
195195
impl Sample {
196-
/// Checks if the sample is well formed. Useful in testing.
196+
/// Checks if the sample is well-formed. Useful in testing.
197197
pub fn is_well_formed(&self) -> bool {
198198
let labels_are_unique = {
199-
let mut uniq = std::collections::HashSet::new();
199+
let mut uniq = HashSet::new();
200200
self.labels.iter().map(|l| &l.key).all(|x| uniq.insert(x))
201201
};
202202
labels_are_unique
@@ -280,7 +280,7 @@ fn assert_samples_eq(
280280
Default::default()
281281
};
282282
// internal::Location::to_pprof() always creates a single line.
283-
assert!(location.lines.len() == 1);
283+
assert_eq!(1, location.lines.len());
284284
let line = location.lines[0];
285285
let function = profile.functions[line.function_id as usize - 1];
286286
assert!(!location.is_folded);
@@ -333,20 +333,16 @@ fn assert_samples_eq(
333333
let str = Box::from(profile.string_table_fetch(label.str).as_str());
334334
owned_labels.push(Label {
335335
key,
336-
str: Some(str),
336+
str,
337337
num: 0,
338-
num_unit: None,
338+
num_unit: Box::from(""),
339339
});
340340
} else {
341341
let num = label.num;
342-
let num_unit = if label.num_unit != 0 {
343-
Some(profile.string_table_fetch_owned(label.num_unit))
344-
} else {
345-
None
346-
};
342+
let num_unit = profile.string_table_fetch_owned(label.num_unit);
347343
owned_labels.push(Label {
348344
key,
349-
str: None,
345+
str: Box::from(""),
350346
num,
351347
num_unit,
352348
});
@@ -406,15 +402,15 @@ fn fuzz_failure_001() {
406402
labels: vec![
407403
Label {
408404
key: Box::from(""),
409-
str: Some(Box::from("")),
405+
str: Box::from(""),
410406
num: 0,
411-
num_unit: Some(Box::from("")),
407+
num_unit: Box::from(""),
412408
},
413409
Label {
414410
key: Box::from("local root span id"),
415-
str: None,
411+
str: Box::from(""),
416412
num: 281474976710656,
417-
num_unit: None,
413+
num_unit: Box::from(""),
418414
},
419415
],
420416
};

profiling/src/internal/profile/interning_api/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ impl Profile {
3232
&mut self,
3333
key: GenerationalId<StringId>,
3434
val: i64,
35-
unit: Option<GenerationalId<StringId>>,
35+
unit: GenerationalId<StringId>,
3636
) -> anyhow::Result<GenerationalId<LabelId>> {
3737
let key = key.get(self.generation)?;
38-
let unit = unit.map(|u| u.get(self.generation)).transpose()?;
38+
let unit = unit.get(self.generation)?;
3939
let id = self.labels.dedup(Label::num(key, val, unit));
4040
Ok(GenerationalId::new(id, self.generation))
4141
}

0 commit comments

Comments
 (0)