-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Conversation
BaseSize for System.String was not set correctly. It caused unnecessary extra 8 bytes to be allocated at the end of strings that had `Length % 4 < 2` on 64-bit platforms. This change makes affected strings proportionally cheaper. For example, `new string('a', 1)` in a long-running loop is 7% faster.
Would it be worth considering a 2.1 port, after bake time? That's a nice space win for some apps. |
@dotnet-bot test Windows_NT arm64 Cross Checked Innerloop Build and Test please |
The typical win is going to be a few percent at best. I do not think we are back porting improvements of this magnitude. |
src/vm/object.inl
Outdated
{ | ||
LIMITED_METHOD_DAC_CONTRACT; | ||
|
||
return ObjSizeOf(Object) + sizeof(DWORD) + sizeof(WCHAR); |
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 the extra sizeof(WCHAR)
is for the terminating null, would be nice to copy the comment from before
Although not exactly a string overflow, the x64 implementation of String.GetHashCode in 4.7.2 runs over into its zero-terminator There wouldn't be a bug danger from 60e9eba (and it's possible that the hashing code 'of sketchy elegance' itself, that I just mentioned, may have since been updated) but I wanted to point out the type of hazard that can lurk, perhaps especially in backporting this sort of thing. This class of read-overrun hazard, say, 'unsafe' code carelessly scanning an odd-sized string by (e.g.) To mitigate this, one would want to continue to guarantee that at least four (4) bytes beyond the zero-terminator of every Of course ensuring this 4-byte padding guarantee only becomes relevant for strings which abut the end of a VM block. All other strings can be tightly packed. |
@glenn-slayden, which line from that source file runs past the null terminator? I just looked through the source but I don't see an overrun. |
@GrabYourPitchforks I didn't say it runs past the null terminator, I said it incorporates its (putative) zero value. If I recall correctly, I was probably referring to https://referencesource.microsoft.com/#mscorlib/system/string.cs,875. If the 'referencesource' code I just cited hasn't yet been entirely deprecated, and you need a much-improved replacement that exactly preserves the legacy behavior, you're welcome to use the code I posted at https://stackoverflow.com/a/48775953/147511. |
BaseSize for System.String was not set correctly. It caused unnecessary extra 8 bytes to be allocated at the end of strings that had
Length % 4 < 2
on 64-bit platforms.This change makes affected strings proportionally cheaper. For example,
new string('a', 1)
in a long-running loop is 7% faster.