Repository navigation
Replace unsafe code with safe APIs in CoreLib and libraries - #134956
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 508e84f0-bee1-48c3-809f-3dffe53a7703
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @dotnet/area-system-runtime |
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The StreamWriter and SharedArrayPool hot-path substitutions need benchmark evidence demonstrating that they do not restore known overhead.
Review effort: Balanced
Findings: 2
Open (2)
What changed in this PR
Replaces selected unsafe pointer/reference operations with safe span, indexing, endianness, and SIMD APIs across CoreLib, networking, and XML.
Changes:
- Converts pointer-based buffer and string operations to spans and indexing.
- Reworks SIMD/table accesses using safe vector and binary APIs.
- Removes unsafe code from WebSocket masking and socket byte operations.
Two hot-path substitutions require performance validation before approval.
| File | Description |
|---|---|
XmlCharType.cs |
Uses indexed character-property lookup. |
UTF8Encoding.Sealed.cs |
Uses stack-allocated spans for small conversions. |
String.Searching.cs |
Uses string span search APIs. |
String.Manipulation.cs |
Converts trimming helper to ReadOnlySpan<char>. |
StringSearchValuesHelper.cs |
Uses safe vector and scalar reads. |
IndexOfAnyAsciiSearcher.cs |
Replaces pointer bitmap mutation with vector APIs. |
BitmapCharSearchValues.cs |
Uses span-based iteration. |
StreamWriter.cs |
Replaces pointer copying with span copying. |
CharUnicodeInfo.cs |
Uses indexed and endian-aware table reads. |
SharedArrayPool.cs |
Replaces indirect reference storage with array indexing. |
BinaryPrimitives.ReverseEndianness.cs |
Uses span-based vector loads and stores. |
BitConverter.cs |
Uses direct byte indexing for Boolean conversion. |
ManagedWebSocket.cs |
Implements masking with safe scalar and vector APIs. |
NetworkStream.cs |
Uses single-element spans for byte operations. |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 508e84f0-bee1-48c3-809f-3dffe53a7703
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 508e84f0-bee1-48c3-809f-3dffe53a7703
|
@MihaZupan the current subset seems to be bound check free (only some minor extra instructions or heavier encodings) |
| while (--searchSpaceLength >= 0) | ||
| { | ||
| char c = Unsafe.Add(ref searchSpace, searchSpaceLength); | ||
| char c = span[searchSpaceLength]; |
There was a problem hiding this comment.
I think this will add an extra bounds check in the loop
https://godbolt.org/z/n9a99Tjn1
There was a problem hiding this comment.
hm.. let me see if it's fixable in JIT. The loop looks trivial enough
There was a problem hiding this comment.
@MihaZupan it seems like it's more or less trivial to remove in jit, will do there 🙂 #135008

Replace selected unsafe operations with safe span, indexing, and SIMD APIs in CoreLib and networking.