-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Remove unsafe perf quirks from System.Text.Encodings.Web #109896
Conversation
Tagging subscribers to this area: @dotnet/area-system-text-encodings-web |
c4217fe
to
452857b
Compare
ugh, .NET Framework 😐 |
src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/SpanUtility.cs
Outdated
Show resolved
Hide resolved
How does the performance compare with Mono? |
This is a part of the global security initiative to improve code safety. We assume that extra nanoseconds in these APIs for mono workloads won't be a big deal. Not sure performance matters for quite old |
src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/SpanUtility.cs
Outdated
Show resolved
Hide resolved
97bb412
to
a58a31f
Compare
@@ -68,22 +68,22 @@ internal override int EncodeUtf8(Rune value, Span<byte> destination) | |||
{ | |||
if (value.Value == '<') | |||
{ | |||
if (!SpanUtility.TryWriteBytes(destination, (byte)'&', (byte)'l', (byte)'t', (byte)';')) { goto OutOfSpace; } | |||
if (!"<"u8.TryCopyTo(destination)) { goto OutOfSpace; } |
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.
This library builds for .NET Framework as well. Did these changes regress performance there? Do we care?
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.
I guess it's impossible to avoid regressions on Mono and .NET Framework during "de-unsafization", the only part where it won't happen is the tiny coreclr-specific part of the corelib 😞
""<"u8.TryCopyTo"
seems to be 5ns slower than the old hack on .NET Framework for me locally. But all the public APIs System.Text.Encodings.Web contains aren't fast anyway (e.g. UrlEncoder.Default.Encode takes ~ms) so hopefully it won't be noticed? I've got an impression that this package is not often used on hot path, that's why I started from it.
cc @jkotas (e.g. we recently introduced a similar regression for Mono in #108572 (comment))
Contributes to #94941 effort.
Should be ~zero performance difference as the JIT is smart enough to handle the safer equivalent, i.e.:
Codegen for
New
andOld
:Same for writing chars vs
"str".TryCopyTo
.PS:
SpanUtility.IsValidIndex
can also be just removed (propagated at all uses) so we can remove the whole file, but it's out of scope for this PR sinceIsValidIndex
is perfectly safe.