mirror of https://git.sr.ht/~stygianentity/bincode
Optimize performance of decoding u8 arrays
This commit is contained in:
parent
539906f441
commit
62b8f39f8f
|
|
@ -10,6 +10,7 @@ use crate::{
|
||||||
error::{DecodeError, IntegerType},
|
error::{DecodeError, IntegerType},
|
||||||
};
|
};
|
||||||
use core::{
|
use core::{
|
||||||
|
any::TypeId,
|
||||||
cell::{Cell, RefCell},
|
cell::{Cell, RefCell},
|
||||||
num::{
|
num::{
|
||||||
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
|
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
|
||||||
|
|
@ -384,7 +385,7 @@ impl<'a, 'de: 'a> BorrowDecode<'de> for &'a str {
|
||||||
|
|
||||||
impl<T, const N: usize> Decode for [T; N]
|
impl<T, const N: usize> Decode for [T; N]
|
||||||
where
|
where
|
||||||
T: Decode + Sized,
|
T: Decode + Sized + 'static,
|
||||||
{
|
{
|
||||||
fn decode<D: Decoder>(mut decoder: D) -> Result<Self, DecodeError> {
|
fn decode<D: Decoder>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||||
if !D::C::SKIP_FIXED_ARRAY_LENGTH {
|
if !D::C::SKIP_FIXED_ARRAY_LENGTH {
|
||||||
|
|
@ -397,6 +398,17 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if TypeId::of::<u8>() == TypeId::of::<T>() {
|
||||||
|
let mut buf = [0u8; N];
|
||||||
|
decoder.reader().read(&mut buf)?;
|
||||||
|
let ptr = &mut buf as *mut _ as *mut [T; N];
|
||||||
|
|
||||||
|
// Safety: we know that T is a u8, so it is perfectly safe to
|
||||||
|
// translate an array of u8 into an array of T
|
||||||
|
let res = unsafe { ptr.read() };
|
||||||
|
core::mem::forget(buf);
|
||||||
|
Ok(res)
|
||||||
|
} else {
|
||||||
let result =
|
let result =
|
||||||
super::impl_core::collect_into_array(&mut (0..N).map(|_| T::decode(&mut decoder)));
|
super::impl_core::collect_into_array(&mut (0..N).map(|_| T::decode(&mut decoder)));
|
||||||
|
|
||||||
|
|
@ -404,6 +416,7 @@ where
|
||||||
// So this unsafe should never occur
|
// So this unsafe should never occur
|
||||||
result.unwrap()
|
result.unwrap()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Decode for core::marker::PhantomData<T> {
|
impl<T> Decode for core::marker::PhantomData<T> {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue