Skip to content

Commit ecbbdf6

Browse files
authored
Attempt to optimize computing the length of a TLV's length (#561)
1 parent 4eaf45f commit ecbbdf6

File tree

1 file changed

+1
-7
lines changed

1 file changed

+1
-7
lines changed

src/writer.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,7 @@ impl WriteBuf {
7373
}
7474

7575
fn _length_length(length: usize) -> u8 {
76-
let mut i = length;
77-
let mut num_bytes = 1;
78-
while i > 255 {
79-
num_bytes += 1;
80-
i >>= 8;
81-
}
82-
num_bytes
76+
(usize::BITS - length.leading_zeros()).div_ceil(8) as u8
8377
}
8478

8579
/// Calculate the number of bytes needed to encode a length field for the given content length.

0 commit comments

Comments
 (0)