-
Notifications
You must be signed in to change notification settings - Fork 134
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ use crate::{ | |
Decimal128, | ||
}; | ||
|
||
use ::serde::{ | ||
use serde::{ | ||
de::{DeserializeOwned, Error as _, Unexpected}, | ||
Deserialize, | ||
}; | ||
|
@@ -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 | ||
|
@@ -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)) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
I'd like to avoid unsafe code if we do not need it here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Also, you can see the issue here. |
||
} | ||
|
||
#[inline] | ||
|
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; | ||
|
||
|
There was a problem hiding this comment.
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 astring::FromUtf8Error
to avoid this?There was a problem hiding this comment.
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 ...There was a problem hiding this comment.
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.