Skip to content

Commit

Permalink
Merge pull request #880 from ollpu/escape-simd
Browse files Browse the repository at this point in the history
(Re)introduce simd feature to pulldown-cmark-escape
  • Loading branch information
Martin1887 committed Apr 23, 2024
2 parents 7a1e53c + 7316da2 commit 056c9e1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
3 changes: 3 additions & 0 deletions pulldown-cmark-escape/Cargo.toml
Expand Up @@ -13,3 +13,6 @@ categories = ["text-processing"]
edition = "2021"
rust-version = "1.70" # Update README.md and azure-pipelines.yml if this changes.
readme = "../README.md"

[features]
simd = []
13 changes: 6 additions & 7 deletions pulldown-cmark-escape/src/lib.rs
Expand Up @@ -271,7 +271,6 @@ fn escape_html_scalar<W: StrWrite>(
mod simd {
use super::StrWrite;
use std::arch::x86_64::*;
use std::io;
use std::mem::size_of;

const VECTOR_SIZE: usize = size_of::<__m128i>();
Expand All @@ -280,7 +279,7 @@ mod simd {
mut w: W,
s: &str,
table: &'static [u8; 256],
) -> io::Result<()> {
) -> Result<(), W::Error> {
// The SIMD accelerated code uses the PSHUFB instruction, which is part
// of the SSSE3 instruction set. Further, we can only use this code if
// the buffer is at least one VECTOR_SIZE in length to prevent reading
Expand Down Expand Up @@ -361,13 +360,13 @@ mod simd {
/// Make sure to only call this when `bytes.len() >= 16`, undefined behaviour may
/// occur otherwise.
#[target_feature(enable = "ssse3")]
unsafe fn foreach_special_simd<F>(
unsafe fn foreach_special_simd<E, F>(
bytes: &[u8],
mut offset: usize,
mut callback: F,
) -> io::Result<()>
) -> Result<(), E>
where
F: FnMut(usize) -> io::Result<()>,
F: FnMut(usize) -> Result<(), E>,
{
// The strategy here is to walk the byte buffer in chunks of VECTOR_SIZE (16)
// bytes at a time starting at the given offset. For each chunk, we compute a
Expand Down Expand Up @@ -410,7 +409,7 @@ mod simd {
unsafe {
super::foreach_special_simd("&aXaaaa.a'aa9a<>aab&".as_bytes(), 0, |ix| {
#[allow(clippy::unit_arg)]
Ok(vec.push(ix))
Ok::<_, std::fmt::Error>(vec.push(ix))
})
.unwrap();
}
Expand All @@ -427,7 +426,7 @@ mod simd {
unsafe {
super::foreach_special_simd(&vek, 0, |_| {
match_count += 1;
Ok(())
Ok::<_, std::fmt::Error>(())
})
.unwrap();
}
Expand Down
2 changes: 1 addition & 1 deletion pulldown-cmark/Cargo.toml
Expand Up @@ -81,5 +81,5 @@ bincode = "1.3.1"
[features]
default = ["getopts", "html"]
gen-tests = []
simd = []
simd = ["pulldown-cmark-escape?/simd"]
html = ["pulldown-cmark-escape"]

0 comments on commit 056c9e1

Please sign in to comment.