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

System.Diagnostics.Activity Perf Improvement Part 2 #40544

Merged
merged 7 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -255,9 +255,6 @@ public string? RootId
/// </summary>
public IEnumerable<KeyValuePair<string, object?>> TagObjects
{
#if ALLOW_PARTIALLY_TRUSTED_CALLERS
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tarekgh I removed the SecuritySafeCritical. Assuming this is no longer needed because we removed the Unsafe.As on the last PR?

Copy link
Member

Choose a reason for hiding this comment

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

make sense. If you run the tests locally should catch any issue with the security attributes when running against NetFX.

[System.Security.SecuritySafeCriticalAttribute]
#endif
get => _tags ?? s_emptyTagObjects;
}

Expand Down Expand Up @@ -1272,9 +1269,10 @@ public void Add(T value)
}
}

// Note: Some customers use this GetEnumerator dynamically to avoid allocations.
public Enumerator<T> GetEnumerator() => new Enumerator<T>(_first);
IEnumerator<T> IEnumerable<T>.GetEnumerator() => new Enumerator<T>(_first);
IEnumerator IEnumerable.GetEnumerator() => new Enumerator<T>(_first);
IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();
Copy link
Member

Choose a reason for hiding this comment

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

minor nit - you may want to add a comment here and below describing the situation here - "in order to preserve the non-allocating GetEnumerator() so OpenTelemetry can call it..."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added comments & tests.

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

private class TagsLinkedList : IEnumerable<KeyValuePair<string, object?>>
Expand Down Expand Up @@ -1379,9 +1377,10 @@ public void Set(KeyValuePair<string, object?> value)
}
}

// Note: Some customers use this GetEnumerator dynamically to avoid allocations.
public Enumerator<KeyValuePair<string, object?>> GetEnumerator() => new Enumerator<KeyValuePair<string, object?>>(_first);
IEnumerator<KeyValuePair<string, object?>> IEnumerable<KeyValuePair<string, object?>>.GetEnumerator() => new Enumerator<KeyValuePair<string, object?>>(_first);
IEnumerator IEnumerable.GetEnumerator() => new Enumerator<KeyValuePair<string, object?>>(_first);
IEnumerator<KeyValuePair<string, object?>> IEnumerable<KeyValuePair<string, object?>>.GetEnumerator() => GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

public IEnumerable<KeyValuePair<string, string?>> EnumerateStringValues()
{
Expand All @@ -1399,6 +1398,11 @@ public void Set(KeyValuePair<string, object?> value)
}
}

/*
* Note:
* Some customers use this Enumerator dynamically to avoid allocations.
* See: https://github.com/dotnet/runtime/pull/40362
*/
private struct Enumerator<T> : IEnumerator<T>
{
private readonly LinkedListNode<T>? _head;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,54 @@ public void TestInsertingFirstTag(string key, object value, bool add, int result
Assert.Equal(resultCount, a.TagObjects.Count());
}

[Fact]
public void StructEnumerator_TagsLinkedList()
{
/*
* This test verifies the presence of the struct Enumerator on TagsLinkedList used by customers dynamically to avoid allocations.
*/
Activity a = new Activity("TestActivity");
a.AddTag("Tag1", true);

IEnumerable<KeyValuePair<string, object>> enumerable = a.TagObjects;

bool foundGetEnumerator = false;
foreach (MethodInfo method in enumerable.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic))
{
Copy link
Member

Choose a reason for hiding this comment

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

you don't need foreach here. yu can request the method name directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

if(method.Name == "GetEnumerator" && !method.ReturnType.IsInterface && method.ReturnType.IsValueType)
{
foundGetEnumerator = true;
break;
}
}

Assert.True(foundGetEnumerator);
}

[Fact]
public void StructEnumerator_GenericLinkedList()
{
/*
* This test verifies the presence of the struct Enumerator on LinkedList<T> used by customers dynamically to avoid allocations.
*/
Activity a = new Activity("TestActivity");
a.AddEvent(new ActivityEvent());

IEnumerable<ActivityEvent> enumerable = a.Events;

bool foundGetEnumerator = false;
foreach (MethodInfo method in enumerable.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic))
{
if (method.Name == "GetEnumerator" && !method.ReturnType.IsInterface && method.ReturnType.IsValueType)
{
foundGetEnumerator = true;
break;
}
}

Assert.True(foundGetEnumerator);
}

public void Dispose()
{
Activity.Current = null;
Expand Down