-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Changes from 4 commits
d11af05
526817d
0af8fa9
5d0262c
dd4b901
b9a4865
0fdba3f
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 |
---|---|---|
|
@@ -255,9 +255,6 @@ public string? RootId | |
/// </summary> | ||
public IEnumerable<KeyValuePair<string, object?>> TagObjects | ||
{ | ||
#if ALLOW_PARTIALLY_TRUSTED_CALLERS | ||
[System.Security.SecuritySafeCriticalAttribute] | ||
#endif | ||
get => _tags ?? s_emptyTagObjects; | ||
} | ||
|
||
|
@@ -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(); | ||
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. 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..." 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. Added comments & tests. |
||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
} | ||
|
||
private class TagsLinkedList : IEnumerable<KeyValuePair<string, object?>> | ||
|
@@ -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() | ||
{ | ||
|
@@ -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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) | ||
{ | ||
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. you don't need foreach here. yu can request the method name directly. 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. 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; | ||
|
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.
@tarekgh I removed the
SecuritySafeCritical
. Assuming this is no longer needed because we removed theUnsafe.As
on the last PR?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.
make sense. If you run the tests locally should catch any issue with the security attributes when running against NetFX.