Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUST-1798 use simd to optimize utf8 validation #440

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exclude = [
"rustfmt.toml",
".travis.yml",
".evergreen/**",
".gitignore"
".gitignore",
]

[features]
Expand All @@ -54,20 +54,31 @@ name = "bson"

[dependencies]
ahash = "0.8.0"
chrono = { version = "0.4.15", features = ["std"], default-features = false, optional = true }
chrono = { version = "0.4.15", features = [
"std",
], default-features = false, optional = true }
rand = "0.8"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["preserve_order"] }
simdutf8 = "0.1.4"
indexmap = "1.6.2"
hex = "0.4.2"
base64 = "0.13.0"
once_cell = "1.5.1"
uuid-0_8 = { package = "uuid", version = "0.8.1", features = ["serde", "v4"], optional = true }
uuid-0_8 = { package = "uuid", version = "0.8.1", features = [
"serde",
"v4",
], optional = true }
uuid = { version = "1.1.2", features = ["serde", "v4"] }
serde_bytes = "0.11.5"
serde_with = { version = "1.3.1", optional = true }
serde_with-3 = { package = "serde_with", version = "3.1.0", optional = true }
time = { version = "0.3.9", features = ["formatting", "parsing", "macros", "large-dates"] }
time = { version = "0.3.9", features = [
"formatting",
"parsing",
"macros",
"large-dates",
] }
bitvec = "1.0.1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand All @@ -78,7 +89,11 @@ criterion = "0.3.0"
pretty_assertions = "0.6.1"
proptest = "1.0.0"
serde_bytes = "0.11"
chrono = { version = "0.4", features = ["serde", "clock", "std"], default-features = false }
chrono = { version = "0.4", features = [
"serde",
"clock",
"std",
], default-features = false }

[package.metadata.docs.rs]
all-features = true
Expand Down
9 changes: 5 additions & 4 deletions src/de/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{error, fmt, fmt::Display, io, string, sync::Arc};
use std::{error, fmt, fmt::Display, io, sync::Arc};

use serde::de::{self, Unexpected};
use simdutf8::basic::Utf8Error;

use crate::Bson;

Expand All @@ -13,7 +14,7 @@ pub enum Error {

/// A [`std::string::FromUtf8Error`](https://doc.rust-lang.org/std/string/struct.FromUtf8Error.html) encountered
/// while decoding a UTF-8 String from the input data.
InvalidUtf8String(string::FromUtf8Error),
InvalidUtf8String(Utf8Error),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the type contained in this error variant is a breaking change and would require a major version release, which we are not planning to do in the near term. Can a simdutf8::simple::Utf8Error be mapped to a string::FromUtf8Error to avoid this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The std's String::FromUtf8Error does not expose any constructor. So may be this can not be avoided ...

Copy link
Contributor

@isabelatkinson isabelatkinson Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Unfortunately we'll need to hold off on this until we can change our error types. I've added the 3.0 version to RUST-1798 to pick up this work again when we do our next major version release.


/// While decoding a [`Document`](crate::Document) from bytes, an unexpected or unsupported
/// element type was encountered.
Expand Down Expand Up @@ -44,8 +45,8 @@ impl From<io::Error> for Error {
}
}

impl From<string::FromUtf8Error> for Error {
fn from(err: string::FromUtf8Error) -> Error {
impl From<Utf8Error> for Error {
fn from(err: Utf8Error) -> Error {
Error::InvalidUtf8String(err)
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{
Decimal128,
};

use ::serde::{
use serde::{
de::{DeserializeOwned, Error as _, Unexpected},
Deserialize,
};
Expand Down Expand Up @@ -108,14 +108,12 @@ pub(crate) fn read_string<R: Read + ?Sized>(reader: &mut R, utf8_lossy: bool) ->
));
}

let mut buf = Vec::with_capacity(len as usize - 1);
reader.take(len as u64 - 1).read_to_end(&mut buf)?;
let s = if utf8_lossy {
let mut buf = Vec::with_capacity(len as usize - 1);
reader.take(len as u64 - 1).read_to_end(&mut buf)?;
String::from_utf8_lossy(&buf).to_string()
} else {
let mut s = String::with_capacity(len as usize - 1);
reader.take(len as u64 - 1).read_to_string(&mut s)?;
s
to_string(buf)?
};

// read the null terminator
Expand Down Expand Up @@ -152,7 +150,13 @@ fn read_cstring<R: Read + ?Sized>(reader: &mut R) -> Result<String> {
v.push(c);
}

Ok(String::from_utf8(v)?)
to_string(v)
}

fn to_string(v: Vec<u8>) -> Result<String> {
let _ = simdutf8::basic::from_utf8(&v)?;
// Safety: `v` is a valid UTF-8 string.
unsafe { Ok(String::from_utf8_unchecked(v)) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason the result of the first line of this function cannot be converted to a String and returned directly?

fn to_string(v: Vec<u8>) -> Result<String> {
    let str = simdutf8::basic::from_utf8(&v)?;
    Ok(str.to_string())
}

I'd like to avoid unsafe code if we do not need it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str.to_string() will make a copy of original bytes. It may be even slower then the std's String::from_utf8.

Also, you can see the issue here.

}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/de/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1759,7 +1759,7 @@ impl<'a> BsonBuf<'a> {
let s = if utf8_lossy_override.unwrap_or(self.utf8_lossy) {
String::from_utf8_lossy(bytes)
} else {
Cow::Borrowed(std::str::from_utf8(bytes).map_err(Error::custom)?)
Cow::Borrowed(simdutf8::basic::from_utf8(bytes).map_err(Error::custom)?)
};

// consume the null byte
Expand Down
2 changes: 1 addition & 1 deletion src/raw/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::str::Utf8Error;
use simdutf8::basic::Utf8Error;

use crate::spec::ElementType;

Expand Down
3 changes: 2 additions & 1 deletion src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ fn read_lenencoded(buf: &[u8]) -> Result<&str> {
}

fn try_to_str(data: &[u8]) -> Result<&str> {
std::str::from_utf8(data).map_err(|e| Error::new_without_key(ErrorKind::Utf8EncodingError(e)))
simdutf8::basic::from_utf8(data)
.map_err(|e| Error::new_without_key(ErrorKind::Utf8EncodingError(e)))
}

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