Skip to content

Commit 9ddb3ef

Browse files
authored
perf: Only compute checksums on debug builds (#2446)
Signed-off-by: jthomson04 <jwillthomson19@gmail.com>
1 parent 3a3f5bf commit 9ddb3ef

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

lib/runtime/src/pipeline/network/codec/two_part.rs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,26 @@ impl Decoder for TwoPartCodec {
8484
// Advance the buffer past the lengths and checksum
8585
src.advance(24);
8686

87-
let bytes_to_hash = header_len + body_len;
88-
let data_to_hash = &src[..bytes_to_hash];
89-
let computed_checksum = xxh3_64(data_to_hash);
90-
91-
// Compare checksums
92-
if checksum != computed_checksum {
93-
return Err(TwoPartCodecError::ChecksumMismatch);
87+
#[cfg(debug_assertions)]
88+
{
89+
// If the server sent a dummy checksum, skip it.
90+
if checksum != 0 {
91+
let bytes_to_hash =
92+
header_len
93+
.checked_add(body_len)
94+
.ok_or(TwoPartCodecError::InvalidMessage(
95+
"Message exceeds max allowed length.".to_string(),
96+
))?;
97+
98+
let data_to_hash = &src[..bytes_to_hash];
99+
100+
let computed_checksum = xxh3_64(data_to_hash);
101+
102+
// Compare checksums
103+
if checksum != computed_checksum {
104+
return Err(TwoPartCodecError::ChecksumMismatch);
105+
}
106+
}
94107
}
95108

96109
// Read header and body data
@@ -117,16 +130,25 @@ impl Encoder<TwoPartMessage> for TwoPartCodec {
117130
}
118131
}
119132

120-
// Compute checksum of the data
121-
let mut data_to_hash = BytesMut::with_capacity(header_len + body_len);
122-
data_to_hash.extend_from_slice(&item.header);
123-
data_to_hash.extend_from_slice(&item.data);
124-
let checksum = xxh3_64(&data_to_hash);
125-
126-
// Write header and body sizes and checksum
127133
dst.put_u64(header_len as u64);
128134
dst.put_u64(body_len as u64);
129-
dst.put_u64(checksum);
135+
136+
// Only compute the checksum in debug mode.
137+
// If we're in release mode, put a dummy value.
138+
#[cfg(debug_assertions)]
139+
{
140+
// Compute checksum of the data
141+
let mut data_to_hash = BytesMut::with_capacity(header_len + body_len);
142+
data_to_hash.extend_from_slice(&item.header);
143+
data_to_hash.extend_from_slice(&item.data);
144+
let checksum = xxh3_64(&data_to_hash);
145+
146+
dst.put_u64(checksum);
147+
}
148+
#[cfg(not(debug_assertions))]
149+
{
150+
dst.put_u64(0);
151+
}
130152

131153
// Write header and body
132154
dst.put_slice(&item.header);

0 commit comments

Comments
 (0)