Skip to content

Commit 4e328bc

Browse files
committed
refactor: shorten names
1 parent 73b638e commit 4e328bc

File tree

8 files changed

+60
-79
lines changed

8 files changed

+60
-79
lines changed

datadog-profiling-protobuf/src/function.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use super::{StringOffset, Value, Varint, WireType};
5-
use crate::Identifiable;
65
use std::io::{self, Write};
76

87
#[repr(C)]
@@ -18,13 +17,11 @@ pub struct Function {
1817
impl Value for Function {
1918
const WIRE_TYPE: WireType = WireType::LengthDelimited;
2019

21-
fn encoded_len(&self) -> u64 {
22-
Varint(self.id).field(1).encoded_len()
23-
+ Varint(self.name.to_u64()).field(2).encoded_len_small()
24-
+ Varint(self.system_name.to_u64())
25-
.field(3)
26-
.encoded_len_small()
27-
+ Varint(self.filename.to_u64()).field(4).encoded_len_small()
20+
fn proto_len(&self) -> u64 {
21+
Varint(self.id).field(1).proto_len()
22+
+ Varint(self.name.to_u64()).field(2).proto_len_small()
23+
+ Varint(self.system_name.to_u64()).field(3).proto_len_small()
24+
+ Varint(self.filename.to_u64()).field(4).proto_len_small()
2825
}
2926

3027
fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
@@ -37,12 +34,6 @@ impl Value for Function {
3734
}
3835
}
3936

40-
impl Identifiable for Function {
41-
fn id(&self) -> u64 {
42-
self.id
43-
}
44-
}
45-
4637
#[cfg(feature = "prost_impls")]
4738
impl From<&Function> for crate::prost_impls::Function {
4839
fn from(value: &Function) -> Self {

datadog-profiling-protobuf/src/label.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ pub struct Label {
1919
impl Value for Label {
2020
const WIRE_TYPE: WireType = WireType::LengthDelimited;
2121

22-
fn encoded_len(&self) -> u64 {
23-
Varint::from(self.key.to_u64()).field(1).encoded_len()
24-
+ Varint(self.str.to_u64()).field(2).encoded_len_small()
25-
+ Varint(self.num as u64).field(3).encoded_len_small()
26-
+ Varint(self.num_unit.to_u64()).field(4).encoded_len_small()
22+
fn proto_len(&self) -> u64 {
23+
Varint::from(self.key.to_u64()).field(1).proto_len()
24+
+ Varint(self.str.to_u64()).field(2).proto_len_small()
25+
+ Varint(self.num as u64).field(3).proto_len_small()
26+
+ Varint(self.num_unit.to_u64()).field(4).proto_len_small()
2727
}
2828

2929
fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {

datadog-profiling-protobuf/src/lib.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct Tag {
4242
pub trait Value {
4343
const WIRE_TYPE: WireType;
4444

45-
fn encoded_len(&self) -> u64;
45+
fn proto_len(&self) -> u64;
4646

4747
fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()>;
4848

@@ -72,21 +72,21 @@ impl<V: Value> Pair<V> {
7272
Pair { field, value }
7373
}
7474

75-
pub fn encoded_len(&self) -> u64 {
76-
let tag = Tag::new(self.field, V::WIRE_TYPE).encoded_len();
75+
pub fn proto_len(&self) -> u64 {
76+
let tag = Tag::new(self.field, V::WIRE_TYPE).proto_len();
7777
let len_prefix = if V::WIRE_TYPE == WireType::LengthDelimited {
78-
self.value.encoded_len()
78+
self.value.proto_len()
7979
} else {
8080
0
8181
};
82-
let value = self.value.encoded_len();
82+
let value = self.value.proto_len();
8383
tag + len_prefix + value
8484
}
8585

8686
#[inline]
87-
pub fn encoded_len_small(&self) -> u64 {
88-
if self.value.encoded_len() != 0 {
89-
self.encoded_len()
87+
pub fn proto_len_small(&self) -> u64 {
88+
if self.value.proto_len() != 0 {
89+
self.proto_len()
9090
} else {
9191
0
9292
}
@@ -95,15 +95,15 @@ impl<V: Value> Pair<V> {
9595
pub fn encode(&self, writer: &mut impl Write) -> io::Result<()> {
9696
Tag::new(self.field, V::WIRE_TYPE).encode(writer)?;
9797
if V::WIRE_TYPE == WireType::LengthDelimited {
98-
let len = self.value.encoded_len();
98+
let len = self.value.proto_len();
9999
Varint(len).encode(writer)?;
100100
}
101101
self.value.encode(writer)
102102
}
103103

104104
#[inline]
105105
pub fn encode_small(&self, writer: &mut impl Write) -> io::Result<()> {
106-
let len = self.value.encoded_len();
106+
let len = self.value.proto_len();
107107
if len == 0 {
108108
return Ok(());
109109
}
@@ -120,10 +120,6 @@ pub trait TagEncodable {
120120
fn encode_with_tag<W: Write>(&self, w: &mut W, field: u32) -> io::Result<()>;
121121
}
122122

123-
pub trait Identifiable: Value {
124-
fn id(&self) -> u64;
125-
}
126-
127123
/// The smallest possible protobuf field number.
128124
const MIN_FIELD: u32 = 1;
129125

@@ -144,16 +140,17 @@ pub enum WireType {
144140
impl Varint {
145141
/// Returns the number of bytes it takes to encode a varint. This is
146142
/// between 1 and 10 bytes, inclusive.
147-
pub const fn encoded_len(&self) -> u64 {
143+
pub const fn proto_len(&self) -> u64 {
148144
// https://github.com/google/protobuf/blob/3.3.x/src/google/protobuf/io/coded_stream.h#L1301-L1309
149145
((((self.0 | 1).leading_zeros() ^ 63) * 9 + 73) / 64) as u64
150146
}
151147
}
148+
152149
impl Value for Varint {
153150
const WIRE_TYPE: WireType = WireType::Varint;
154151

155-
fn encoded_len(&self) -> u64 {
156-
self.encoded_len()
152+
fn proto_len(&self) -> u64 {
153+
self.proto_len()
157154
}
158155

159156
/// Encodes a varint according to protobuf semantics.
@@ -216,8 +213,8 @@ impl Tag {
216213
}
217214

218215
#[inline]
219-
pub const fn encoded_len(self) -> u64 {
220-
self.into_varint().encoded_len()
216+
pub const fn proto_len(self) -> u64 {
217+
self.into_varint().proto_len()
221218
}
222219

223220
#[inline]
@@ -250,10 +247,10 @@ where
250247
{
251248
const WIRE_TYPE: WireType = WireType::LengthDelimited;
252249

253-
fn encoded_len(&self) -> u64 {
250+
fn proto_len(&self) -> u64 {
254251
self.values
255252
.iter()
256-
.map(|x| Varint::from(x).encoded_len())
253+
.map(|x| Varint::from(x).proto_len())
257254
.sum()
258255
}
259256

@@ -272,6 +269,6 @@ mod tests {
272269
#[test]
273270
fn max_varint_len() {
274271
assert_eq!(MAX_VARINT_LEN, 10);
275-
assert_eq!(MAX_VARINT_LEN, Varint(u64::MAX).encoded_len());
272+
assert_eq!(MAX_VARINT_LEN, Varint(u64::MAX).proto_len());
276273
}
277274
}

datadog-profiling-protobuf/src/location.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ pub struct Line {
2525
impl Value for Line {
2626
const WIRE_TYPE: WireType = WireType::LengthDelimited;
2727

28-
fn encoded_len(&self) -> u64 {
29-
Varint(self.function_id).field(1).encoded_len_small()
30-
+ Varint(self.lineno as u64).field(2).encoded_len_small()
28+
fn proto_len(&self) -> u64 {
29+
Varint(self.function_id).field(1).proto_len_small()
30+
+ Varint(self.lineno as u64).field(2).proto_len_small()
3131
}
3232

3333
fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
@@ -53,20 +53,20 @@ impl From<Line> for crate::prost_impls::Line {
5353
impl Value for Location {
5454
const WIRE_TYPE: WireType = WireType::LengthDelimited;
5555

56-
fn encoded_len(&self) -> u64 {
56+
fn proto_len(&self) -> u64 {
5757
let value = self.address;
5858
let value1 = self.mapping_id;
59-
let base = Varint(self.mapping_id).field(1).encoded_len()
60-
+ Varint(value1).field(2).encoded_len_small()
61-
+ Varint(value).field(3).encoded_len_small();
59+
let base = Varint(self.mapping_id).field(1).proto_len()
60+
+ Varint(value1).field(2).proto_len_small()
61+
+ Varint(value).field(3).proto_len_small();
6262

6363
let needed = {
6464
let self1 = &self.line;
6565
let value = self1.lineno as u64;
6666
let value1 = self1.function_id;
67-
let len = Varint(value1).field(1).encoded_len_small()
68-
+ Varint(value).field(2).encoded_len_small();
69-
len + Varint(len).encoded_len() + Tag::new(4, WireType::LengthDelimited).encoded_len()
67+
let len = Varint(value1).field(1).proto_len_small()
68+
+ Varint(value).field(2).proto_len_small();
69+
len + Varint(len).proto_len() + Tag::new(4, WireType::LengthDelimited).proto_len()
7070
};
7171
base + needed
7272
}

datadog-profiling-protobuf/src/mapping.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use super::{StringOffset, Value, Varint, WireType};
5-
use crate::Identifiable;
65
use std::io::{self, Write};
76

87
#[repr(C)]
@@ -22,13 +21,13 @@ impl Mapping {}
2221
impl Value for Mapping {
2322
const WIRE_TYPE: WireType = WireType::LengthDelimited;
2423

25-
fn encoded_len(&self) -> u64 {
26-
Varint(self.id).field(1).encoded_len()
27-
+ Varint(self.memory_start).field(2).encoded_len_small()
28-
+ Varint(self.memory_limit).field(3).encoded_len_small()
29-
+ Varint(self.file_offset).field(4).encoded_len_small()
30-
+ Varint(self.filename.to_u64()).field(5).encoded_len_small()
31-
+ Varint(self.build_id.to_u64()).field(6).encoded_len_small()
24+
fn proto_len(&self) -> u64 {
25+
Varint(self.id).field(1).proto_len()
26+
+ Varint(self.memory_start).field(2).proto_len_small()
27+
+ Varint(self.memory_limit).field(3).proto_len_small()
28+
+ Varint(self.file_offset).field(4).proto_len_small()
29+
+ Varint(self.filename.to_u64()).field(5).proto_len_small()
30+
+ Varint(self.build_id.to_u64()).field(6).proto_len_small()
3231
}
3332

3433
fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
@@ -41,12 +40,6 @@ impl Value for Mapping {
4140
}
4241
}
4342

44-
impl Identifiable for Mapping {
45-
fn id(&self) -> u64 {
46-
self.id
47-
}
48-
}
49-
5043
#[cfg(feature = "prost_impls")]
5144
impl From<&Mapping> for crate::prost_impls::Mapping {
5245
fn from(mapping: &Mapping) -> Self {
@@ -85,7 +78,7 @@ mod tests {
8578
assert_eq!(i64::from(mapping.build_id), prost_mapping.build_id);
8679

8780
let roundtrip = {
88-
let mut buffer = Vec::with_capacity(mapping.encoded_len() as usize);
81+
let mut buffer = Vec::with_capacity(mapping.proto_len() as usize);
8982
mapping.encode(&mut buffer).unwrap();
9083
prost_impls::Mapping::decode(buffer.as_slice()).unwrap()
9184
};

datadog-profiling-protobuf/src/sample.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ pub struct Sample<'a> {
1414
impl Value for Sample<'_> {
1515
const WIRE_TYPE: WireType = WireType::LengthDelimited;
1616

17-
fn encoded_len(&self) -> u64 {
18-
let locations = PackedVarint::new(self.location_ids).field(1).encoded_len();
19-
let values = PackedVarint::new(self.values).field(2).encoded_len();
17+
fn proto_len(&self) -> u64 {
18+
let locations = PackedVarint::new(self.location_ids).field(1).proto_len();
19+
let values = PackedVarint::new(self.values).field(2).proto_len();
2020
let labels = self
2121
.labels
2222
.iter()
23-
.map(|label| label.field(3).encoded_len())
23+
.map(|label| label.field(3).proto_len())
2424
.sum::<u64>();
2525
locations + values + labels
2626
}
@@ -72,7 +72,7 @@ mod tests {
7272
};
7373

7474
use prost::Message;
75-
let len = sample.encoded_len() as usize;
75+
let len = sample.proto_len() as usize;
7676
let mut buffer = Vec::with_capacity(len);
7777
sample.encode(&mut buffer).unwrap();
7878
let roundtrip = prost_impls::Sample::decode(buffer.as_slice()).unwrap();
@@ -96,12 +96,12 @@ mod tests {
9696

9797
let prost_sample = prost_impls::Sample::from(sample);
9898

99-
let mut buffer = Vec::with_capacity(sample.encoded_len() as usize);
99+
let mut buffer = Vec::with_capacity(sample.proto_len() as usize);
100100
sample.encode(&mut buffer).unwrap();
101101
let roundtrip = prost_impls::Sample::decode(buffer.as_slice()).unwrap();
102102
assert_eq!(prost_sample, roundtrip);
103103

104-
let mut buffer2 = Vec::with_capacity(sample.encoded_len() as usize);
104+
let mut buffer2 = Vec::with_capacity(sample.proto_len() as usize);
105105
prost_sample.encode(&mut buffer2).unwrap();
106106
let roundtrip2 = prost_impls::Sample::decode(buffer2.as_slice()).unwrap();
107107
assert_eq!(roundtrip, roundtrip2);

datadog-profiling-protobuf/src/string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::io::{self, Write};
77
impl Value for &str {
88
const WIRE_TYPE: WireType = WireType::LengthDelimited;
99

10-
fn encoded_len(&self) -> u64 {
10+
fn proto_len(&self) -> u64 {
1111
self.len() as u64
1212
}
1313

datadog-profiling-protobuf/src/value_type.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ pub struct ValueType {
1515
impl Value for ValueType {
1616
const WIRE_TYPE: WireType = WireType::LengthDelimited;
1717

18-
fn encoded_len(&self) -> u64 {
19-
Varint(self.r#type.to_u64()).field(1).encoded_len_small()
20-
+ Varint(self.unit.to_u64()).field(2).encoded_len_small()
18+
fn proto_len(&self) -> u64 {
19+
Varint(self.r#type.to_u64()).field(1).proto_len_small()
20+
+ Varint(self.unit.to_u64()).field(2).proto_len_small()
2121
}
2222

2323
fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
@@ -59,8 +59,8 @@ mod tests {
5959

6060
let value = value_type.unit.to_u64();
6161
let value1 = value_type.r#type.to_u64();
62-
let len = (Varint(value1).field(1).encoded_len_small()
63-
+ Varint(value).field(2).encoded_len_small()) as usize;
62+
let len = (Varint(value1).field(1).proto_len_small()
63+
+ Varint(value).field(2).proto_len_small()) as usize;
6464
let mut buffer = Vec::with_capacity(len);
6565
value_type.encode(&mut buffer).unwrap();
6666
let roundtrip = prost_impls::ValueType::decode(buffer.as_slice()).unwrap();

0 commit comments

Comments
 (0)