Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2e9d926

Browse filesBrowse files
committed
StringSequence
1 parent ea5222b commit 2e9d926
Copy full SHA for 2e9d926

File tree

Expand file treeCollapse file tree

3 files changed

+43
-8
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+43
-8
lines changed
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using BenchmarkDotNet.Attributes;
2+
using Prometheus;
3+
4+
namespace Benchmark.NetCore;
5+
6+
[MemoryDiagnoser]
7+
public class LabelSequenceBenchmarks
8+
{
9+
private static readonly StringSequence Names3Array = StringSequence.From(new[] { "aaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbb", "cccccccccccccc" });
10+
private static readonly StringSequence Values3Array = StringSequence.From(new[] { "valueaaaaaaaaaaaaaaaaa", "valuebbbbbbbbbbbbbb", "valuecccccccccccccc" });
11+
12+
[Benchmark]
13+
public void Create_From3Array()
14+
{
15+
LabelSequence.From(Names3Array, Values3Array);
16+
}
17+
}
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using BenchmarkDotNet.Attributes;
2+
using Prometheus;
3+
4+
namespace Benchmark.NetCore;
5+
6+
[MemoryDiagnoser]
7+
public class StringSequenceBenchmarks
8+
{
9+
private static readonly string[] Values3Array = ["aaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbb", "cccccccccccccc"];
10+
11+
[Benchmark]
12+
public void Create_From3Array()
13+
{
14+
StringSequence.From(Values3Array);
15+
}
16+
}

‎Prometheus/StringSequence.cs

Copy file name to clipboardExpand all lines: Prometheus/StringSequence.cs
+10-8Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Prometheus;
1818

1919
public Enumerator GetEnumerator()
2020
{
21-
return new Enumerator(_values.Span, _inheritedValues ?? []);
21+
return new Enumerator(_values.Span, _inheritedValueArrays ?? []);
2222
}
2323

2424
public ref struct Enumerator
@@ -152,7 +152,9 @@ private StringSequence(StringSequence inheritFrom, StringSequence thenFrom, Read
152152
}
153153

154154
_values = andFinallyPrepend;
155-
_inheritedValues = InheritFrom(inheritFrom, thenFrom);
155+
156+
if (!inheritFrom.IsEmpty || !thenFrom.IsEmpty)
157+
_inheritedValueArrays = InheritFrom(inheritFrom, thenFrom);
156158

157159
Length = _values.Length + inheritFrom.Length + thenFrom.Length;
158160

@@ -167,7 +169,7 @@ public static StringSequence From(params string[] values)
167169
return new StringSequence(Empty, Empty, values);
168170
}
169171

170-
public static StringSequence From(ReadOnlyMemory<string> values)
172+
public static StringSequence From(in ReadOnlyMemory<string> values)
171173
{
172174
if (values.Length == 0)
173175
return Empty;
@@ -198,7 +200,7 @@ public StringSequence Concat(StringSequence concatenatedValues)
198200

199201
// Inherited values from one or more parent instances.
200202
// It may be null because structs have a default ctor that zero-initializes them, so watch out.
201-
private readonly ReadOnlyMemory<string>[]? _inheritedValues;
203+
private readonly ReadOnlyMemory<string>[]? _inheritedValueArrays;
202204

203205
private readonly int _hashCode;
204206

@@ -215,13 +217,13 @@ public StringSequence Concat(StringSequence concatenatedValues)
215217
if (!first.IsEmpty)
216218
{
217219
firstOwnArrayCount = first._values.Length > 0 ? 1 : 0;
218-
firstInheritedArrayCount = first._inheritedValues?.Length ?? 0;
220+
firstInheritedArrayCount = first._inheritedValueArrays?.Length ?? 0;
219221
}
220222

221223
if (!second.IsEmpty)
222224
{
223225
secondOwnArrayCount = second._values.Length > 0 ? 1 : 0;
224-
secondInheritedArrayCount = second._inheritedValues?.Length ?? 0;
226+
secondInheritedArrayCount = second._inheritedValueArrays?.Length ?? 0;
225227
}
226228

227229
var totalSegmentCount = firstOwnArrayCount + firstInheritedArrayCount + secondOwnArrayCount + secondInheritedArrayCount;
@@ -240,7 +242,7 @@ public StringSequence Concat(StringSequence concatenatedValues)
240242

241243
if (secondInheritedArrayCount != 0)
242244
{
243-
Array.Copy(second._inheritedValues!, 0, result, targetIndex, secondInheritedArrayCount);
245+
Array.Copy(second._inheritedValueArrays!, 0, result, targetIndex, secondInheritedArrayCount);
244246
targetIndex += secondInheritedArrayCount;
245247
}
246248

@@ -251,7 +253,7 @@ public StringSequence Concat(StringSequence concatenatedValues)
251253

252254
if (firstInheritedArrayCount != 0)
253255
{
254-
Array.Copy(first._inheritedValues!, 0, result, targetIndex, firstInheritedArrayCount);
256+
Array.Copy(first._inheritedValueArrays!, 0, result, targetIndex, firstInheritedArrayCount);
255257
targetIndex += firstInheritedArrayCount;
256258
}
257259

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.