Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Use C# compiler's static data support in Encoding.Preamble #20768

Merged
merged 2 commits into from Nov 3, 2018

Conversation

stephentoub
Copy link
Member

Also avoid Array.Empty and just use default span for an empty preamble.

Example IL (for UTF8Encoding.PreambleSpan):

  IL_0000:  ldsflda    valuetype '<PrivateImplementationDetails>'/'__StaticArrayInitTypeSize=3' '<PrivateImplementationDetails>'::'57218C316B6921E2CD61027A2387EDC31A2D9471'
  IL_0005:  ldc.i4.3
  IL_0006:  newobj     instance void valuetype System.ReadOnlySpan`1<uint8>::.ctor(void*, int32)
  IL_000b:  ret

cc: @jkotas, @tarekgh

Also avoid Array.Empty and just use default span for an empty preamble.
@@ -39,8 +39,8 @@ public sealed class UTF32Encoding : Encoding
internal static readonly UTF32Encoding s_default = new UTF32Encoding(bigEndian: false, byteOrderMark: true);
internal static readonly UTF32Encoding s_bigEndianDefault = new UTF32Encoding(bigEndian: true, byteOrderMark: true);

private static readonly byte[] s_bigEndianPreamble = new byte[4] { 0x00, 0x00, 0xFE, 0xFF };
private static readonly byte[] s_littleEndianPreamble = new byte[4] { 0xFF, 0xFE, 0x00, 0x00 };
private static ReadOnlySpan<byte> BigEndianPreamble => new byte[4] { 0x00, 0x00, 0xFE, 0xFF }; // uses C# compiler's optimization for static byte[] data
Copy link
Member

@jkotas jkotas Nov 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this just be inline in the Preamble property, similar to how GetPreamble() method is done? No need to define trivial properties for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@ahsonkhan
Copy link
Member

ahsonkhan commented Nov 2, 2018

I had gone through and made changes across the board within the coreclr repo some time ago but never submitted it. Is it worth pursuing and submitting a PR?

2832a0d

I had similar changes for corefx in some branch that I need to find on my old machine. Let me know if I should dig it up.

For example: ahsonkhan/corefx@21c3abb

@jkotas
Copy link
Member

jkotas commented Nov 3, 2018

Is it worth pursuing and submitting a PR?

Sounds good to me. (BTW: The Hungarian p prefix should be deleted when you are turning it into property, not upper cased.)

@stephentoub
Copy link
Member Author

Is it worth pursuing and submitting a PR?

This particular case is one where the consumer was already using the array as a span, so there was very little downside (though I did measure just to be sure, and microbenchmarks on, e.g. Encoding.UTF8.Preamble.Length, saw nice bumps). The other cases I found in corelib, though, which show up in your changes, too, all had a consumer expecting a byte[], which meant there could in theory be a greater chance of regression. Since there is a downside to the churn, and IMO a readability hit (it really looks like these call sites allocate), I think it's only worth doing for cases where you believe there will be measurable throughout or startup wins, or some other measurable benefit, and can then validate that. And in most of these cases, I think that's probably unlikely. But if you want to prove otherwise, great :)

@stephentoub
Copy link
Member Author

@dotnet-bot test Windows_NT x86 Release Innerloop Build and Test please
@dotnet-bot test Ubuntu16.04 arm64 Cross Checked Innerloop Build and Test please

@stephentoub
Copy link
Member Author

ps Just saw Jan's response. If he thinks it's worthwhile, I defer to him :)

@jkotas
Copy link
Member

jkotas commented Nov 3, 2018

This should be pretty much always worthwhile, in particular for large data blobs like the ones found in globalization. We should spot check the code generated by the JIT for this in several places to make sure that it works as expected and it is not hitting JIT optimization bug as it is often case with new constructs.

@stephentoub stephentoub merged commit faa4c87 into dotnet:master Nov 3, 2018
@stephentoub stephentoub deleted the encodingspans branch November 3, 2018 02:08
A-And pushed a commit to A-And/coreclr that referenced this pull request Nov 20, 2018
)

* Use C# compiler's static data support in Encoding.Preamble

Also avoid Array.Empty and just use default span for an empty preamble.

* Address PR feedback
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…reclr#20768)

* Use C# compiler's static data support in Encoding.Preamble

Also avoid Array.Empty and just use default span for an empty preamble.

* Address PR feedback


Commit migrated from dotnet/coreclr@faa4c87
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants