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

Optimize the allocation and speed of ActivitySet/GetCustomProperty #41840

Merged
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 @@ -80,7 +80,7 @@ public partial class Activity : IDisposable
private LinkedList<KeyValuePair<string, string?>>? _baggage;
private LinkedList<ActivityLink>? _links;
private LinkedList<ActivityEvent>? _events;
private ConcurrentDictionary<string, object>? _customProperties;
private Dictionary<string, object>? _customProperties;
private string? _displayName;

/// <summary>
Expand Down Expand Up @@ -894,16 +894,19 @@ public void SetCustomProperty(string propertyName, object? propertyValue)
{
if (_customProperties == null)
{
Interlocked.CompareExchange(ref _customProperties, new ConcurrentDictionary<string, object>(), null);
Interlocked.CompareExchange(ref _customProperties, new Dictionary<string, object>(), null);
}

if (propertyValue == null)
lock (_customProperties)
{
_customProperties.TryRemove(propertyName, out object _);
}
else
{
_customProperties[propertyName] = propertyValue!;
if (propertyValue == null)
{
_customProperties.Remove(propertyName);
}
else
{
_customProperties[propertyName] = propertyValue!;
}
}
}

Expand All @@ -921,7 +924,13 @@ public void SetCustomProperty(string propertyName, object? propertyValue)
return null;
}

return _customProperties.TryGetValue(propertyName, out object? o) ? o! : null;
object? ret;
lock (_customProperties)
{
ret = _customProperties.TryGetValue(propertyName, out object? o) ? o! : null;
}

return ret;
}

internal static Activity CreateAndStart(ActivitySource source, string name, ActivityKind kind, string? parentId, ActivityContext parentContext,
Expand Down