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 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 @@ -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,15 +1398,15 @@ public void Set(KeyValuePair<string, object?> value)
}
}

// Note: Some customers use this Enumerator dynamically to avoid allocations.
private struct Enumerator<T> : IEnumerator<T>
{
private readonly LinkedListNode<T>? _head;
private LinkedListNode<T>? _nextNode;
[AllowNull, MaybeNull] private T _currentItem;

public Enumerator(LinkedListNode<T>? head)
{
_nextNode = _head = head;
_nextNode = head;
_currentItem = default;
}

Expand All @@ -1428,7 +1427,7 @@ public bool MoveNext()
return true;
}

public void Reset() => _nextNode = _head;
public void Reset() => throw new NotSupportedException();

public void Dispose()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,40 @@ public void TestInsertingFirstTag(string key, object value, bool add, int result
Assert.Equal(resultCount, a.TagObjects.Count());
}

[Fact]
public void StructEnumerator_TagsLinkedList()
{
// Note: 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;

MethodInfo method = enumerable.GetType().GetMethod("GetEnumerator", BindingFlags.Instance | BindingFlags.Public);

Assert.NotNull(method);
Assert.False(method.ReturnType.IsInterface);
Assert.True(method.ReturnType.IsValueType);
}

[Fact]
public void StructEnumerator_GenericLinkedList()
{
// Note: 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;

MethodInfo method = enumerable.GetType().GetMethod("GetEnumerator", BindingFlags.Instance | BindingFlags.Public);

Assert.NotNull(method);
Assert.False(method.ReturnType.IsInterface);
Assert.True(method.ReturnType.IsValueType);
}

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