Skip to content

Commit 8ae3d85

Browse files
committed
perf(crc32): enable sse4.2 for x86 platform
1 parent 0cca4f7 commit 8ae3d85

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

.cargo/config.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
[target.x86_64-unknown-linux-gnu]
2+
rustflags = ["-C", "target-feature=+sse4.2"]
3+
[target.x86_64-pc-windows-msvc]
4+
rustflags = ["-C", "target-feature=+sse4.2"]
5+
[target.x86_64-apple-darwin]
6+
rustflags = ["-C", "target-feature=+sse4.2"]
17
[target.aarch64-unknown-linux-musl]
28
linker = "aarch64-linux-musl-gcc"
39
rustflags = ["-C", "target-feature=-crt-static"]

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ members = [
1212
]
1313

1414
[profile.release]
15-
codegen-units = 4
15+
codegen-units = 1
1616
lto = true
1717
overflow-checks = false
1818
strip = 'symbols'

packages/crc32/src/lib.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,28 @@ extern crate global_alloc;
55

66
use crc32c::crc32c_append;
77
use crc32fast::Hasher;
8-
use napi::bindgen_prelude::{Buffer, Either};
8+
use napi::{bindgen_prelude::Either, JsBuffer, Result};
99
use napi_derive::*;
1010

1111
#[napi(js_name = "crc32c")]
12-
pub fn crc32c(input: Either<String, Buffer>, initial_state: Option<u32>) -> u32 {
13-
crc32c_append(
14-
initial_state.unwrap_or(0),
15-
match &input {
16-
Either::A(s) => s.as_bytes(),
17-
Either::B(b) => b.as_ref(),
18-
},
19-
)
12+
pub fn crc32c(input: Either<String, JsBuffer>, initial_state: Option<u32>) -> Result<u32> {
13+
Ok(match input {
14+
Either::A(s) => crc32c_append(initial_state.unwrap_or(0), s.as_bytes()),
15+
Either::B(b) => crc32c_append(initial_state.unwrap_or(0), &b.into_value()?),
16+
})
2017
}
2118

2219
#[napi]
23-
pub fn crc32(input: Either<String, Buffer>, initial_state: Option<u32>) -> u32 {
20+
pub fn crc32(input: Either<String, JsBuffer>, initial_state: Option<u32>) -> Result<u32> {
2421
let mut hasher = Hasher::new_with_initial(initial_state.unwrap_or(0));
25-
hasher.update(match &input {
26-
Either::A(s) => s.as_bytes(),
27-
Either::B(b) => b.as_ref(),
28-
});
29-
hasher.finalize()
22+
match input {
23+
Either::A(s) => {
24+
hasher.update(s.as_bytes());
25+
}
26+
Either::B(b) => {
27+
let b = b.into_value()?;
28+
hasher.update(&b);
29+
}
30+
};
31+
Ok(hasher.finalize())
3032
}

0 commit comments

Comments
 (0)