Can this be optimized?

#[inline]
/// Applies a weak cipher onto the unencrypted header of a packet. Designed to make deep packet inspections harder
fn apply_cipher(val: u128, inverse: bool, packet: &mut BytesMut) {
    let ref bytes = val.to_be_bytes();
    let (bytes0, bytes1) = bytes.split_at(8);
    let packet = &mut packet[..HDP_HEADER_BYTE_LEN];
    bytes0.iter().zip(bytes1.iter())
        .cycle()
        .zip(packet.iter_mut())
        .for_each(|((a, b), c)| cipher_inner(*a, *b, c, inverse))
}

#[inline]
fn cipher_inner(a: u8, b: u8, c: &mut u8, inverse: bool) {
    if inverse {
        *c = (*c ^ b).wrapping_sub(a);
    } else {
        *c = c.wrapping_add(a) ^ b;
    }
}

You can use Rust with (i think) --emit=asm to get the generated assembly and see if it is optimised enough. Also make sure you are using --release as well

You should be aware that --emit=asm forces a single codegen unit (-Ccodegen-units=1) to be used. This may affect performance. On Linux something like objdump -d could be used to get a disassembly of the actually compiled executable.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.