Skip to content

Commit 67a1c40

Browse files
Replace locks with ConcurrentDictionary in Trace class
Co-authored-by: FabianMeiswinkel <19165014+FabianMeiswinkel@users.noreply.github.com>
1 parent c8617fb commit 67a1c40

1 file changed

Lines changed: 8 additions & 16 deletions

File tree

  • Microsoft.Azure.Cosmos/src/Tracing

Microsoft.Azure.Cosmos/src/Tracing/Trace.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Microsoft.Azure.Cosmos.Tracing
66
{
77
using System;
8+
using System.Collections.Concurrent;
89
using System.Collections.Generic;
910
using System.Diagnostics;
1011
using System.Linq;
@@ -16,7 +17,7 @@ internal sealed class Trace : ITrace
1617
{
1718
private static readonly IReadOnlyDictionary<string, object> EmptyDictionary = new Dictionary<string, object>();
1819
private readonly List<ITrace> children;
19-
private readonly Lazy<Dictionary<string, object>> data;
20+
private readonly ConcurrentDictionary<string, object> data;
2021
private ValueStopwatch stopwatch;
2122

2223
private Trace(
@@ -34,7 +35,7 @@ private Trace(
3435
this.Component = component;
3536
this.Parent = parent;
3637
this.children = new List<ITrace>();
37-
this.data = new Lazy<Dictionary<string, object>>();
38+
this.data = new ConcurrentDictionary<string, object>();
3839
this.Summary = summary ?? throw new ArgumentNullException(nameof(summary));
3940
}
4041

@@ -56,7 +57,7 @@ private Trace(
5657

5758
public IReadOnlyList<ITrace> Children => this.children;
5859

59-
public IReadOnlyDictionary<string, object> Data => this.data.IsValueCreated ? this.data.Value : Trace.EmptyDictionary;
60+
public IReadOnlyDictionary<string, object> Data => this.data.Count > 0 ? this.data : Trace.EmptyDictionary;
6061

6162
public void Dispose()
6263
{
@@ -124,27 +125,18 @@ public static Trace GetRootTrace(
124125

125126
public void AddDatum(string key, TraceDatum traceDatum)
126127
{
127-
lock (this.data)
128-
{
129-
this.data.Value.Add(key, traceDatum);
130-
this.Summary.UpdateRegionContacted(traceDatum);
131-
}
128+
this.data.TryAdd(key, traceDatum);
129+
this.Summary.UpdateRegionContacted(traceDatum);
132130
}
133131

134132
public void AddDatum(string key, object value)
135133
{
136-
lock (this.data)
137-
{
138-
this.data.Value.Add(key, value);
139-
}
134+
this.data.TryAdd(key, value);
140135
}
141136

142137
public void AddOrUpdateDatum(string key, object value)
143138
{
144-
lock (this.data)
145-
{
146-
this.data.Value[key] = value;
147-
}
139+
this.data[key] = value;
148140
}
149141
}
150142
}

0 commit comments

Comments
 (0)