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

Conversation

Liyixin95
Copy link
Contributor

Copy link
Contributor

@isabelatkinson isabelatkinson left a comment

Choose a reason for hiding this comment

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

A few suggestions, it looks like there are also some formatting errors. Can you please run the following on the files you've changed?

rustfmt +nightly --unstable-features

@@ -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.

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.

@isabelatkinson isabelatkinson changed the title use simd to optimize utf8 validation. RUST-1798 use simd to optimize utf8 validation Nov 15, 2023
@kevinAlbs kevinAlbs added the tracked-in-jira Ticket filed in Mongo's Jira system label Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tracked-in-jira Ticket filed in Mongo's Jira system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants