Skip to content
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 startup byte[] allocation in EventSource #32276

Merged
merged 1 commit into from
Feb 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1579,7 +1579,11 @@ public void Append(byte input)
/// <param name="input">
/// Data to include in the hash. Must not be null.
/// </param>
#if ES_BUILD_STANDALONE
public void Append(byte[] input)
#else
public void Append(ReadOnlySpan<byte> input)
#endif
{
foreach (byte b in input)
{
Expand Down Expand Up @@ -1691,13 +1695,21 @@ private void Drain()

private static Guid GenerateGuidFromName(string name)
{
#if ES_BUILD_STANDALONE
if (namespaceBytes == null)
{
namespaceBytes = new byte[] {
0x48, 0x2C, 0x2D, 0xB2, 0xC3, 0x90, 0x47, 0xC8,
0x87, 0xF8, 0x1A, 0x15, 0xBF, 0xC1, 0x30, 0xFB,
};
}
#else
ReadOnlySpan<byte> namespaceBytes = new byte[] // rely on C# compiler optimization to remove byte[] allocation
{
0x48, 0x2C, 0x2D, 0xB2, 0xC3, 0x90, 0x47, 0xC8,
0x87, 0xF8, 0x1A, 0x15, 0xBF, 0xC1, 0x30, 0xFB,
};
#endif

byte[] bytes = Encoding.BigEndianUnicode.GetBytes(name);
Sha1ForNonSecretPurposes hash = default;
Expand Down Expand Up @@ -3882,10 +3894,10 @@ private bool SelfDescribingEvents
// WARNING: Do not depend upon initialized statics during creation of EventSources, as it is possible for creation of an EventSource to trigger
// creation of yet another EventSource. When this happens, these statics may not yet be initialized.
// Rather than depending on initialized statics, use lazy initialization to ensure that the statics are initialized exactly when they are needed.

#if ES_BUILD_STANDALONE
// used for generating GUID from eventsource name
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
private static byte[]? namespaceBytes;

#endif
#endregion
}

Expand Down Expand Up @@ -4020,8 +4032,6 @@ public EventListener()
/// is the only way to actually make the listen die. Thus it is important that users of EventListener
/// call Dispose when they are done with their logging.
/// </summary>
#if ES_BUILD_STANDALONE
#endif
public virtual void Dispose()
{
lock (EventListenersLock)
Expand Down