Skip to content

Commit f2c07cd

Browse files
abr-egnLiyixin95
andauthored
RUST-1798 use simd to optimize utf8 validation (#548)
Co-authored-by: Liyixin95 <[email protected]>
1 parent cc56d1c commit f2c07cd

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ serde_with = { version = "3.1.0", optional = true }
6565
time = { version = "0.3.9", features = ["formatting", "parsing", "macros", "large-dates"] }
6666
bitvec = "1.0.1"
6767
serde_path_to_error = { version = "0.1.16", optional = true }
68+
simdutf8 = "0.1.5"
6869

6970
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
7071
js-sys = "0.3"

src/raw/error.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::str::Utf8Error;
2-
31
use crate::spec::ElementType;
42

53
/// An error that occurs when attempting to parse raw BSON bytes.
@@ -44,7 +42,7 @@ pub enum ErrorKind {
4442
MalformedValue { message: String },
4543

4644
/// Improper UTF-8 bytes were found when proper UTF-8 was expected.
47-
Utf8EncodingError(Utf8Error),
45+
Utf8EncodingError,
4846
}
4947

5048
impl std::fmt::Display for Error {
@@ -60,7 +58,7 @@ impl std::fmt::Display for Error {
6058
ErrorKind::MalformedValue { message } => {
6159
write!(f, "{}malformed value: {:?}", prefix, message)
6260
}
63-
ErrorKind::Utf8EncodingError(e) => write!(f, "{}utf-8 encoding error: {}", prefix, e),
61+
ErrorKind::Utf8EncodingError => write!(f, "{}utf-8 encoding error", prefix),
6462
}
6563
}
6664
}

src/raw/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ fn read_lenencode(buf: &[u8]) -> Result<&str> {
271271
}
272272

273273
fn try_to_str(data: &[u8]) -> Result<&str> {
274-
std::str::from_utf8(data).map_err(|e| Error::new(ErrorKind::Utf8EncodingError(e)))
274+
simdutf8::basic::from_utf8(data).map_err(|_| Error::new(ErrorKind::Utf8EncodingError))
275275
}
276276

277277
fn usize_try_from_i32(i: i32) -> Result<usize> {

0 commit comments

Comments
 (0)