-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Use Span to drop byte[1] allocations #15680
Changes from 3 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 |
---|---|---|
|
@@ -46,7 +46,8 @@ unsafe static internal byte ConvertToNative(char managedChar, bool fBestFit, boo | |
|
||
static internal char ConvertToManaged(byte nativeChar) | ||
{ | ||
byte[] bytes = new byte[1] { nativeChar }; | ||
Span<byte> bytes = new Span<byte>(ref nativeChar, 1); | ||
bytes[0] = nativeChar; | ||
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.
|
||
string str = Encoding.Default.GetString(bytes); | ||
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. Same question 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. This is in corelib. Presumably it can just use the Span(ref T, int) ctor. Or it could use DangerousCreate that just calls it. 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. I was trying to figure out why DangerousCreate wasn't showing up in intellisense: [EditorBrowsable(EditorBrowsableState.Never)]
public static Span<T> DangerousCreate(object obj, ref T objectData, int length); This totally trolled me 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. @KrzysztofCwalina, @ahsonkhan, why is this marked as EditorBrowsableState.Never? We seem to be too quick to do that. It's already named |
||
return str[0]; | ||
} | ||
|
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 is span needed here? fixed works fine with zero-length arrays).
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.
Pinning zero length array gives back
null
.Initialize
method does not accept null.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 see. Though that could be addressed as well just by passing
ptr != null ? ptr : (byte*)1
instead ofptr
.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.
Yes, there are several ways to address this. Another option in this case is to fix or delete the null check in the
Initialize
method.(byte*)1
is a very unusual pointer value. If I saw it somewhere, I would be looking for memory corruption. It would be nice for the unmanaged pointers to either be null or point to valid memory.ptr != null ? ptr : (byte*)1
pattern is larger overall native code compared toMemoryMarshal.GetReference
- we should measure.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.
As now (
MemoryMarshal.GetReference
) itsChanging to
It becomes
Which seems more involved?