Skip to content

Commit

Permalink
Better buffer management to improve codegen speed slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
koute committed Dec 11, 2023
1 parent c6450e3 commit 9fc22cd
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions crates/polkavm-assembler/src/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,23 @@ impl InstBuf {
core::ptr::write_unaligned(output.add(8).cast::<u64>(), u64::from_le(self.out_2));
}

#[cold]
fn reserve(output: &mut Vec<u8>) {
output.reserve(16);
assert!(output.spare_capacity_mut().len() >= 16);
}

#[inline]
pub fn encode_into(self, output: &mut Vec<u8>) {
// NOTE: This `if` actually matters and should not be removed, even though `reserve` would be a no-op anyway in such case.
if output.spare_capacity_mut().len() < 16 {
output.reserve(16);
assert!(output.spare_capacity_mut().len() >= 16);
Self::reserve(output);
if output.spare_capacity_mut().len() < 16 {
// SAFETY: `reserve` made sure that we have this much capacity, so this is safe.
unsafe {
core::hint::unreachable_unchecked();
}
}
}

// SAFETY: We've made sure that there is at least 16 bytes of spare capacity,
Expand Down

0 comments on commit 9fc22cd

Please sign in to comment.