mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-08 13:33:44 +02:00
Dead code removal, perf stuff
This commit is contained in:
parent
887585f6fa
commit
04b2adcf5f
2 changed files with 105 additions and 29 deletions
|
@ -1355,15 +1355,6 @@ fn create_initial_offer<SendFunction: FnMut(&mut [u8])>(
|
|||
}))
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(C, packed)]
|
||||
struct PackedHeader {
|
||||
counter: u32,
|
||||
recipient_session_id_low32: u32,
|
||||
recipient_session_id_high16_packet_type_fragment_info: u32,
|
||||
zero: u32,
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn create_packet_header(
|
||||
header: &mut [u8],
|
||||
|
@ -1383,15 +1374,12 @@ fn create_packet_header(
|
|||
debug_assert!(recipient_session_id <= 0xffffffffffff); // session ID is 48 bits
|
||||
|
||||
if fragment_count <= MAX_FRAGMENTS {
|
||||
header[..16].copy_from_slice(memory::as_byte_array::<[u32; 4], 16>(&[
|
||||
counter.to_u32().to_le(),
|
||||
0,
|
||||
(recipient_session_id as u32).to_le(),
|
||||
((recipient_session_id.wrapping_shr(32) as u32)
|
||||
| (packet_type as u32).wrapping_shl(16)
|
||||
| ((fragment_count - 1) as u32).wrapping_shl(20))
|
||||
.to_le(),
|
||||
]));
|
||||
// CCCC____IIIIIITF
|
||||
memory::u64_to_le_bytes(counter.to_u32() as u64, header);
|
||||
memory::u64_to_le_bytes(
|
||||
recipient_session_id | (packet_type as u64).wrapping_shl(48) | ((fragment_count - 1) as u64).wrapping_shl(52),
|
||||
&mut header[8..],
|
||||
);
|
||||
Ok(())
|
||||
} else {
|
||||
unlikely_branch();
|
||||
|
|
|
@ -6,28 +6,58 @@ use std::mem::size_of;
|
|||
#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"))]
|
||||
#[allow(unused)]
|
||||
mod fast_int_memory_access {
|
||||
#[inline(always)]
|
||||
pub fn u64_to_le_bytes(i: u64, b: &mut [u8]) {
|
||||
assert!(b.len() >= 8);
|
||||
unsafe { *b.as_mut_ptr().cast() = i.to_le() };
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u32_to_le_bytes(i: u32, b: &mut [u8]) {
|
||||
assert!(b.len() >= 4);
|
||||
unsafe { *b.as_mut_ptr().cast() = i.to_le() };
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u16_to_le_bytes(i: u16, b: &mut [u8]) {
|
||||
assert!(b.len() >= 2);
|
||||
unsafe { *b.as_mut_ptr().cast() = i.to_le() };
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u64_from_le_bytes(b: &[u8]) -> u64 {
|
||||
assert!(b.len() >= 8);
|
||||
unsafe { *b.as_ptr().cast() }
|
||||
unsafe { u64::from_le(*b.as_ptr().cast()) }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u32_from_le_bytes(b: &[u8]) -> u32 {
|
||||
assert!(b.len() >= 4);
|
||||
unsafe { *b.as_ptr().cast() }
|
||||
unsafe { u32::from_le(*b.as_ptr().cast()) }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u16_from_le_bytes(b: &[u8]) -> u16 {
|
||||
assert!(b.len() >= 2);
|
||||
unsafe { *b.as_ptr().cast() }
|
||||
unsafe { u16::from_le(*b.as_ptr().cast()) }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u128_from_ne_bytes(b: &[u8]) -> u128 {
|
||||
assert!(b.len() >= 16);
|
||||
unsafe { *b.as_ptr().cast() }
|
||||
pub fn u64_to_ne_bytes(i: u64, b: &mut [u8]) {
|
||||
assert!(b.len() >= 8);
|
||||
unsafe { *b.as_mut_ptr().cast() = i };
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u32_to_ne_bytes(i: u32, b: &mut [u8]) {
|
||||
assert!(b.len() >= 4);
|
||||
unsafe { *b.as_mut_ptr().cast() = i };
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u16_to_ne_bytes(i: u16, b: &mut [u8]) {
|
||||
assert!(b.len() >= 2);
|
||||
unsafe { *b.as_mut_ptr().cast() = i };
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
@ -48,22 +78,40 @@ mod fast_int_memory_access {
|
|||
unsafe { *b.as_ptr().cast() }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u64_to_be_bytes(i: u64, b: &mut [u8]) {
|
||||
assert!(b.len() >= 8);
|
||||
unsafe { *b.as_mut_ptr().cast() = i.to_be() };
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u32_to_be_bytes(i: u32, b: &mut [u8]) {
|
||||
assert!(b.len() >= 4);
|
||||
unsafe { *b.as_mut_ptr().cast() = i.to_be() };
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u16_to_be_bytes(i: u16, b: &mut [u8]) {
|
||||
assert!(b.len() >= 2);
|
||||
unsafe { *b.as_mut_ptr().cast() = i.to_be() };
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u64_from_be_bytes(b: &[u8]) -> u64 {
|
||||
assert!(b.len() >= 8);
|
||||
unsafe { *b.as_ptr().cast::<u64>() }.swap_bytes()
|
||||
unsafe { *b.as_ptr().cast::<u64>() }.to_be()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u32_from_be_bytes(b: &[u8]) -> u32 {
|
||||
assert!(b.len() >= 4);
|
||||
unsafe { *b.as_ptr().cast::<u32>() }.swap_bytes()
|
||||
unsafe { *b.as_ptr().cast::<u32>() }.to_be()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u16_from_be_bytes(b: &[u8]) -> u16 {
|
||||
assert!(b.len() >= 2);
|
||||
unsafe { *b.as_ptr().cast::<u16>() }.swap_bytes()
|
||||
unsafe { *b.as_ptr().cast::<u16>() }.to_be()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -71,6 +119,21 @@ mod fast_int_memory_access {
|
|||
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")))]
|
||||
#[allow(unused)]
|
||||
mod fast_int_memory_access {
|
||||
#[inline(always)]
|
||||
pub fn u64_to_le_bytes(i: u64, b: &mut [u8]) {
|
||||
b[..8].copy_from_slice(&i.to_le_bytes());
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u32_to_le_bytes(i: u32, b: &mut [u8]) {
|
||||
b[..4].copy_from_slice(&i.to_le_bytes());
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u16_to_le_bytes(i: u16, b: &mut [u8]) {
|
||||
b[..2].copy_from_slice(&i.to_le_bytes());
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u64_from_le_bytes(b: &[u8]) -> u64 {
|
||||
u64::from_le_bytes(b[..8].try_into().unwrap())
|
||||
|
@ -87,8 +150,18 @@ mod fast_int_memory_access {
|
|||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u128_from_ne_bytes(b: &[u8]) -> u64 {
|
||||
u128::from_ne_bytes(b[..16].try_into().unwrap())
|
||||
pub fn u64_to_ne_bytes(i: u64, b: &mut [u8]) {
|
||||
b[..8].copy_from_slice(&i.to_ne_bytes());
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u32_to_ne_bytes(i: u32, b: &mut [u8]) {
|
||||
b[..4].copy_from_slice(&i.to_ne_bytes());
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u16_to_ne_bytes(i: u16, b: &mut [u8]) {
|
||||
b[..2].copy_from_slice(&i.to_ne_bytes());
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
@ -106,6 +179,21 @@ mod fast_int_memory_access {
|
|||
u16::from_ne_bytes(b[..2].try_into().unwrap())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u64_to_be_bytes(i: u64, b: &mut [u8]) {
|
||||
b[..8].copy_from_slice(&i.to_be_bytes());
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u32_to_be_bytes(i: u32, b: &mut [u8]) {
|
||||
b[..4].copy_from_slice(&i.to_be_bytes());
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u16_to_be_bytes(i: u16, b: &mut [u8]) {
|
||||
b[..2].copy_from_slice(&i.to_be_bytes());
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn u64_from_be_bytes(b: &[u8]) -> u64 {
|
||||
u64::from_be_bytes(b[..8].try_into().unwrap())
|
||||
|
|
Loading…
Add table
Reference in a new issue