Skip to content

Commit 5912391

Browse files
committed
Improve numbers encoding
Copy the bytes into a buffer instead of destructuring them. On my machine it's an about 15% performance improvement for f64. For all types, even if it's 16-bit only, the resulting assembly has less instructions: https://godbolt.org/z/ov97Pq43j
1 parent 30fe080 commit 5912391

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/core/enc.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ impl Encode for TypeNum<u16> {
6969
match u8::try_from(self.value) {
7070
Ok(x) => TypeNum::new(self.type_, x).encode(writer)?,
7171
Err(_) => {
72-
let [x0, x1] = self.value.to_be_bytes();
73-
writer.push(&[self.type_ | 0x19, x0, x1])?
72+
let mut buf = [self.type_ | 0x19, 0, 0];
73+
buf[1..3].copy_from_slice(&self.value.to_be_bytes());
74+
writer.push(&buf)?
7475
}
7576
}
7677
Ok(())
@@ -83,8 +84,9 @@ impl Encode for TypeNum<u32> {
8384
match u16::try_from(self.value) {
8485
Ok(x) => TypeNum::new(self.type_, x).encode(writer)?,
8586
Err(_) =>{
86-
let [x0, x1, x2, x3] = self.value.to_be_bytes();
87-
writer.push(&[self.type_ | 0x1a, x0, x1, x2, x3])?;
87+
let mut buf = [self.type_ | 0x1a, 0, 0, 0, 0];
88+
buf[1..5].copy_from_slice(&self.value.to_be_bytes());
89+
writer.push(&buf)?;
8890
}
8991
}
9092
Ok(())
@@ -97,8 +99,9 @@ impl Encode for TypeNum<u64> {
9799
match u32::try_from(self.value) {
98100
Ok(x) => TypeNum::new(self.type_, x).encode(writer)?,
99101
Err(_) => {
100-
let [x0, x1, x2, x3, x4, x5, x6, x7] = self.value.to_be_bytes();
101-
writer.push(&[self.type_ | 0x1b, x0, x1, x2, x3, x4, x5, x6, x7])?;
102+
let mut buf = [self.type_ | 0x1b, 0, 0, 0, 0, 0, 0, 0, 0];
103+
buf[1..9].copy_from_slice(&self.value.to_be_bytes());
104+
writer.push(&buf)?;
102105
}
103106
}
104107
Ok(())
@@ -358,26 +361,29 @@ impl Encode for half::f16 {
358361
impl Encode for types::F16 {
359362
#[inline]
360363
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), Error<W::Error>> {
361-
let [x0, x1] = self.0.to_be_bytes();
362-
writer.push(&[marker::F16, x0, x1])?;
364+
let mut buf = [marker::F16, 0, 0];
365+
buf[1..3].copy_from_slice(&self.0.to_be_bytes());
366+
writer.push(&buf)?;
363367
Ok(())
364368
}
365369
}
366370

367371
impl Encode for f32 {
368372
#[inline]
369373
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), Error<W::Error>> {
370-
let [x0, x1, x2, x3] = self.to_be_bytes();
371-
writer.push(&[marker::F32, x0, x1, x2, x3])?;
374+
let mut buf = [marker::F32, 0, 0, 0, 0];
375+
buf[1..5].copy_from_slice(&self.to_be_bytes());
376+
writer.push(&buf)?;
372377
Ok(())
373378
}
374379
}
375380

376381
impl Encode for f64 {
377382
#[inline]
378383
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), Error<W::Error>> {
379-
let [x0, x1, x2, x3, x4, x5, x6, x7] = self.to_be_bytes();
380-
writer.push(&[marker::F64, x0, x1, x2, x3, x4, x5, x6, x7])?;
384+
let mut buf = [marker::F64, 0, 0, 0, 0, 0, 0, 0, 0];
385+
buf[1..9].copy_from_slice(&self.to_be_bytes());
386+
writer.push(&buf)?;
381387
Ok(())
382388
}
383389
}

0 commit comments

Comments
 (0)