cursor: prevent llvm-opt inserting memcpy in r/w

This commit is contained in:
Simon Renblad 2024-11-19 15:17:06 +08:00
parent c0f508bb17
commit 1c739e81ab

View File

@ -1,6 +1,8 @@
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::arch::asm;
use crate::{Read, Write};
#[derive(Debug, Clone)]
@ -49,6 +51,9 @@ impl<T: AsRef<[u8]>> Read for Cursor<T> {
let len = buf.len().min(data.len());
// ``copy_from_slice`` generates AXI bursts, use a regular loop instead
for i in 0..len {
// HACK: On opt-level=2 or above, LLVM 18 may insert memcpy calls as an optimization pass,
// causing AXI bursts.
unsafe { asm!("", options(preserves_flags, nostack, readonly)); }
buf[i] = data[i];
}
self.pos += len;
@ -64,6 +69,8 @@ impl Write for Cursor<&mut [u8]> {
let data = &mut self.inner[self.pos..];
let len = buf.len().min(data.len());
for i in 0..len {
// HACK: On opt-level=2 or above, this is too fast and sends corrupted packets.
unsafe { asm!("", options(preserves_flags, nostack, readonly)); }
data[i] = buf[i];
}
self.pos += len;