Minor perf improvement for metric (#3862)

This commit is contained in:
Cijo Thomas 2022-11-03 14:51:10 -04:00 committed by GitHub
parent 6c3beef649
commit 91a9b7c549
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 12 deletions

View File

@ -201,22 +201,25 @@ namespace OpenTelemetry.Metrics
return -1;
}
// Note: We are using storage from ThreadStatic for both the input order of tags and the sorted order of tags,
// Note: We are using storage from ThreadStatic (for upto MaxTagCacheSize tags) for both the input order of tags and the sorted order of tags,
// so we need to make a deep copy for Dictionary storage.
var givenKeys = new string[length];
tagKeys.CopyTo(givenKeys, 0);
if (length <= ThreadStaticStorage.MaxTagCacheSize)
{
var givenKeys = new string[length];
tagKeys.CopyTo(givenKeys, 0);
var givenValues = new object[length];
tagValues.CopyTo(givenValues, 0);
var givenValues = new object[length];
tagValues.CopyTo(givenValues, 0);
var sortedTagKeys = new string[length];
tempSortedTagKeys.CopyTo(sortedTagKeys, 0);
var sortedTagKeys = new string[length];
tempSortedTagKeys.CopyTo(sortedTagKeys, 0);
var sortedTagValues = new object[length];
tempSortedTagValues.CopyTo(sortedTagValues, 0);
var sortedTagValues = new object[length];
tempSortedTagValues.CopyTo(sortedTagValues, 0);
givenTags = new Tags(givenKeys, givenValues);
sortedTags = new Tags(sortedTagKeys, sortedTagValues);
givenTags = new Tags(givenKeys, givenValues);
sortedTags = new Tags(sortedTagKeys, sortedTagValues);
}
lock (this.tagsToMetricPointIndexDictionary)
{

View File

@ -23,7 +23,7 @@ namespace OpenTelemetry.Metrics
{
internal sealed class ThreadStaticStorage
{
private const int MaxTagCacheSize = 8;
internal const int MaxTagCacheSize = 8;
[ThreadStatic]
private static ThreadStaticStorage storage;