-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
feat(ops): Fast zero copy string arguments #16777
Conversation
I recommend testing some Latin-1 (V8's OneByte) and UTF-8 clashing characters, eg. © ("\u00A9") is a good one. EDIT: Not sure if that's actually a clashing character... But at least I've recently been bitten by presuming that V8's OneByteStrings are directly usable as UTF-8, specifically with the copyright symbol as the test character that broke. |
… 'main' of github.com:denoland/deno into fast-string-args
@littledivy did you add such a test? |
@bartlomieju There are tests for that in the V8 patch: https://chromium-review.googlesource.com/c/v8/v8/+/4036884 |
fn op_encoding_encode_into( | ||
scope: &mut v8::HandleScope, | ||
input: serde_v8::Value, | ||
input: Cow<'_, str>, |
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.
Why not &str
?
ext/web/lib.rs
Outdated
// ops guarantee this to be a SeqOneByteString. | ||
Cow::Borrowed(v) => v[..boundary].len() as u32, | ||
Cow::Owned(v) => v[..boundary].encode_utf16().count() as u32, |
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.
Cow::Borrowed
means SeqOneByteString
and Cow::Owned
means utf16? I think a more verbose comment here is warranted.
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.
+1 please explain which variant should be used in which situation
fn is_string(ty: impl ToTokens) -> Option<bool> { | ||
let toks = tokens(ty); | ||
if toks == "String" { | ||
return Some(false); | ||
} | ||
if toks == "& str" { | ||
return Some(true); | ||
} | ||
None | ||
} | ||
|
||
fn is_option_string(ty: impl ToTokens) -> bool { | ||
tokens(ty) == "Option < String >" | ||
} | ||
|
||
fn is_cow_str(ty: impl ToTokens) -> bool { | ||
tokens(&ty).starts_with("Cow <") && tokens(&ty).ends_with("str >") | ||
} | ||
|
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.
is_string
shouldn't also include Cow strings? Maybe it should be called is_non_cow_string
?
It would be nice to have unit tests for these little utility functions. Is that hard?
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.
LGTM - some comments...
awesome work @littledivy - this is super cool
This reverts commit 9b2b8df.
This reverts commit 9b2b8df. Closes dsherret/ts-morph#1372 Closes #16979
Uses SeqOneByteString optimization to do zero-copy
&str
arguments in fast calls.String
with an extra alloc in fast path.Cow<'_, str>
. Owned for slow case, Borrowed for fast case