Commit 3987dcb
perf: avoid per-observation array allocation in ObservableInstrument (#128039)
## Description
Eliminate the per-observation `Measurement<T>[1]` allocation in the
built-in observable instruments `ObservableCounter<T>`,
`ObservableGauge<T>`, `ObservableUpDownCounter<T>`).
`ObservableInstrument<T>.Observe(MeterListener)` now pattern-matches on
`this` to read the built-in's `_callback` directly and dispatches
single-value callbacks straight to `MeterListener.NotifyMeasurement`,
skipping the array wrapper and the foreach allocation. The
`Func<IEnumerable<Measurement<T>>>` shape is unchanged in behavior — the
user owns the enumerable.
## Benchmark
BenchmarkDotNet on .NET 11 preview, one `RecordObservableInstruments()`
call per benchmark:
| Callback shape | Baseline | Patched |
| --- | --- | --- |
| `Func<T>` | 22.91 ns / 72 B | 5.71 ns / 0 B |
| `Func<Measurement<T>>` | 24.52 ns / 72 B | 6.78 ns / 0 B |
| `Func<IEnumerable<Measurement<T>>>` | 37.49 ns / 104 B | 28.75 ns / 72
B |
The IEnumerable row also improves (−32 B) because the simpler call chain
lets the JIT elide the enumerator the baseline kept. The user-allocated
array itself (72 B) is unchanged.
## Compatibility
No public API change. The three built-ins are `public sealed` with
`internal` constructors, so the `this switch` is exhaustive for
instances that flow through this method.
---------
Co-authored-by: Tarek Mahmoud Sayed <10833894+tarekgh@users.noreply.github.com>1 parent 6969309 commit 3987dcb
4 files changed
Lines changed: 30 additions & 25 deletions
File tree
- src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
| |||
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
53 | | - | |
| 55 | + | |
54 | 56 | | |
55 | 57 | | |
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
| |||
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
53 | | - | |
| 55 | + | |
54 | 56 | | |
55 | 57 | | |
Lines changed: 21 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
| |||
50 | 49 | | |
51 | 50 | | |
52 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
53 | 56 | | |
54 | 57 | | |
55 | 58 | | |
56 | 59 | | |
57 | 60 | | |
58 | | - | |
59 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
60 | 67 | | |
| 68 | + | |
61 | 69 | | |
62 | 70 | | |
63 | 71 | | |
64 | | - | |
| 72 | + | |
65 | 73 | | |
| 74 | + | |
66 | 75 | | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
| 76 | + | |
77 | 77 | | |
78 | 78 | | |
79 | | - | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
80 | 83 | | |
81 | | - | |
| 84 | + | |
82 | 85 | | |
83 | 86 | | |
84 | | - | |
| 87 | + | |
85 | 88 | | |
86 | | - | |
| 89 | + | |
87 | 90 | | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | 91 | | |
92 | | - | |
93 | 92 | | |
94 | 93 | | |
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
| |||
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
53 | | - | |
| 55 | + | |
54 | 56 | | |
55 | 57 | | |
0 commit comments