0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-25 16:49:29 -05:00

fix: don't allocate for zero-length strings (#1309)

This commit is contained in:
Matt Mastracci 2023-09-01 08:54:52 -04:00 committed by GitHub
parent aa203e032c
commit 987d520221
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -552,9 +552,15 @@ impl String {
&self, &self,
scope: &mut Isolate, scope: &mut Isolate,
) -> std::string::String { ) -> std::string::String {
let len_utf8 = self.utf8_length(scope);
let len_utf16 = self.length(); let len_utf16 = self.length();
// No need to allocate or do any work for zero-length strings
if len_utf16 == 0 {
return std::string::String::new();
}
let len_utf8 = self.utf8_length(scope);
// If len_utf8 == len_utf16 and the string is one-byte, we can take the fast memcpy path. This is true iff the // If len_utf8 == len_utf16 and the string is one-byte, we can take the fast memcpy path. This is true iff the
// string is 100% 7-bit ASCII. // string is 100% 7-bit ASCII.
if self.is_onebyte() && len_utf8 == len_utf16 { if self.is_onebyte() && len_utf8 == len_utf16 {
@ -612,9 +618,15 @@ impl String {
scope: &mut Isolate, scope: &mut Isolate,
buffer: &'a mut [MaybeUninit<u8>; N], buffer: &'a mut [MaybeUninit<u8>; N],
) -> Cow<'a, str> { ) -> Cow<'a, str> {
let len_utf16 = self.length();
// No need to allocate or do any work for zero-length strings
if len_utf16 == 0 {
return "".into();
}
// TODO(mmastrac): Ideally we should be able to access the string's internal representation // TODO(mmastrac): Ideally we should be able to access the string's internal representation
let len_utf8 = self.utf8_length(scope); let len_utf8 = self.utf8_length(scope);
let len_utf16 = self.length();
// If len_utf8 == len_utf16 and the string is one-byte, we can take the fast memcpy path. This is true iff the // If len_utf8 == len_utf16 and the string is one-byte, we can take the fast memcpy path. This is true iff the
// string is 100% 7-bit ASCII. // string is 100% 7-bit ASCII.