Skip to content

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

/// 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
@abr-egn
Copy link
Contributor

abr-egn commented Jun 3, 2025

I know it's been a long time but I just merged in #548 based on this PR, to go live when we release 3.0 of the bson crate :)

@abr-egn abr-egn closed this Jun 3, 2025
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.

4 participants