Skip to content

Commit

Permalink
format_escaped_str() uses only one 128-bit SIMD path
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Feb 3, 2024
1 parent 6d7e1ac commit 58a8bd3
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 76 deletions.
59 changes: 1 addition & 58 deletions src/serialize/writer/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,64 +565,7 @@ where
}
}

#[cfg(all(
feature = "unstable-simd",
target_arch = "x86_64",
target_feature = "avx2"
))]
#[inline(always)]
fn format_escaped_str<W>(writer: &mut W, value: &str) -> io::Result<()>
where
W: ?Sized + io::Write + WriteExt,
{
unsafe {
let num_reserved_bytes = value.len() * 8 + 32 + 3;
writer.reserve(num_reserved_bytes);

let written = crate::serialize::writer::simd::format_escaped_str_impl_256(
writer.as_mut_buffer_ptr(),
value.as_bytes().as_ptr(),
value.len(),
);
writer.set_written(written);
}
Ok(())
}

#[cfg(all(
feature = "unstable-simd",
target_arch = "x86_64",
not(target_feature = "avx2")
))]
#[inline(always)]
fn format_escaped_str<W>(writer: &mut W, value: &str) -> io::Result<()>
where
W: ?Sized + io::Write + WriteExt,
{
unsafe {
let num_reserved_bytes = value.len() * 8 + 32 + 3;
writer.reserve(num_reserved_bytes);

if std::is_x86_feature_detected!("avx2") {
let written = crate::serialize::writer::simd::format_escaped_str_impl_256(
writer.as_mut_buffer_ptr(),
value.as_bytes().as_ptr(),
value.len(),
);
writer.set_written(written);
} else {
let written = crate::serialize::writer::simd::format_escaped_str_impl_128(
writer.as_mut_buffer_ptr(),
value.as_bytes().as_ptr(),
value.len(),
);
writer.set_written(written);
}
}
Ok(())
}

#[cfg(all(feature = "unstable-simd", not(target_arch = "x86_64")))]
#[cfg(feature = "unstable-simd")]
#[inline(always)]
fn format_escaped_str<W>(writer: &mut W, value: &str) -> io::Result<()>
where
Expand Down
18 changes: 0 additions & 18 deletions src/serialize/writer/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ macro_rules! impl_format_simd {
}

#[inline(never)]
#[cfg(not(target_feature = "avx2"))]
#[cfg_attr(target_arch = "x86_64", cold)]
pub unsafe fn format_escaped_str_impl_128(
odptr: *mut u8,
value_ptr: *const u8,
Expand All @@ -131,19 +129,3 @@ pub unsafe fn format_escaped_str_impl_128(

impl_format_simd!(odptr, value_ptr, value_len);
}

#[cfg(target_arch = "x86_64")]
#[inline(never)]
#[cfg_attr(not(target_feature = "avx2"), target_feature(enable = "avx2"))]
#[cfg_attr(not(target_feature = "avx2"), target_feature(enable = "bmi2"))]
pub unsafe fn format_escaped_str_impl_256(
odptr: *mut u8,
value_ptr: *const u8,
value_len: usize,
) -> usize {
const STRIDE: usize = 32;
const STRIDE_SATURATION: u32 = u32::MAX;
type StrVector = std::simd::u8x32;

impl_format_simd!(odptr, value_ptr, value_len);
}

0 comments on commit 58a8bd3

Please sign in to comment.