Skip to content

Commit a9e1761

Browse files
committed
address dictionary-creation comments
don't return frozen dictionaries on modifiable call-sites. combine code for combining dictionaries
1 parent 62f9dcf commit a9e1761

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

src/Cli/dotnet/Telemetry/Telemetry.cs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -227,43 +227,37 @@ private static ActivityEvent CreateActivityEvent(
227227

228228
private IDictionary<string, double>? GetEventMeasures(IDictionary<string, double>? measurements)
229229
{
230-
if (measurements is null)
230+
return (measurements, _commonMeasurements) switch
231231
{
232-
return _commonMeasurements;
233-
}
234-
if (_commonMeasurements == null)
235-
{
236-
return measurements;
237-
}
232+
(null, null) => null,
233+
(null, not null) => _commonMeasurements == FrozenDictionary<string, double>.Empty ? null : new Dictionary<string, double>(_commonMeasurements),
234+
(not null, null) => measurements,
235+
(not null, not null) => Combine(_commonMeasurements, measurements),
236+
};
237+
}
238238

239-
IDictionary<string, double> eventMeasurements = new Dictionary<string, double>(_commonMeasurements);
240-
foreach (KeyValuePair<string, double> measurement in measurements)
239+
private IDictionary<string, string>? GetEventProperties(IDictionary<string, string>? properties)
240+
{
241+
return (properties, _commonProperties) switch
241242
{
242-
eventMeasurements[measurement.Key] = measurement.Value;
243-
}
244-
return eventMeasurements;
243+
(null, null) => null,
244+
(null, not null) => _commonProperties == FrozenDictionary<string, string>.Empty ? null : new Dictionary<string, string>(_commonProperties),
245+
(not null, null) => properties,
246+
(not null, not null) => Combine(_commonProperties, properties),
247+
};
245248
}
246249

247-
private IDictionary<string, string>? GetEventProperties(IDictionary<string, string>? properties)
250+
static IDictionary<TKey, TValue> Combine<TKey, TValue>(IDictionary<TKey, TValue> common, IDictionary<TKey, TValue> specific) where TKey : notnull
248251
{
249-
if (properties is null)
252+
IDictionary<TKey, TValue> eventMeasurements = new Dictionary<TKey, TValue>(capacity: common.Count + specific.Count);
253+
foreach (KeyValuePair<TKey, TValue> measurement in common)
250254
{
251-
return _commonProperties;
255+
eventMeasurements[measurement.Key] = measurement.Value;
252256
}
253-
if (_commonProperties == null)
257+
foreach (KeyValuePair<TKey, TValue> measurement in specific)
254258
{
255-
return properties;
259+
eventMeasurements[measurement.Key] = measurement.Value;
256260
}
257-
258-
var eventProperties = new Dictionary<string, string>(_commonProperties);
259-
if (properties != null)
260-
{
261-
foreach (KeyValuePair<string, string> property in properties)
262-
{
263-
eventProperties[property.Key] = property.Value;
264-
}
261+
return eventMeasurements;
265262
}
266-
267-
return eventProperties;
268-
}
269263
}

0 commit comments

Comments
 (0)