Commit db7ac58
committed
subscriber: make Registry::enter/exit much faster (#1058)
## Motivation
We've tried very hard to make sure that entering and exiting spans is
lightweight...in the `tracing-core` core machinery. Unfortunately, we
haven't done any benchmarking of how subscriber implementations actually
handle enter/exit events. It turns out that in `tracing-subscriber`'s
`Registry`, there's actually significant overhead for entering a span:
calling `span.enter()` may take as long as 69 ns (on my machine).
## Solution
I've written some microbenchmarks for entering and exiting enabled spans
using `tracing-subscriber::fmt`, comparing them with the overhead of
calling `enter` on an enabled span. Based on this, I've made some
performance improvements. These optimizations include:
- Removing the `HashSet` that's used for tracking previously entered
span IDs, in favor of linear search. Span stacks probably never deep
enough for a hashmap to be faster than iterating over a couple of
vec indices.
- Preallocating the vec used for the span stack to hold at least 64
elements. This means we'll never have a lag spike from reallocating,
as I think it'll almost never be deeper than 64 IDs.
- Only cloning/dropping an ID's ref count for the _first_ ID in the stack.
This makes entering and exiting enabled spans significantly faster:

It would be nice to continue optimizing this, but this might be about
the best it gets given the other requirements that we're now making
assertions about.
Signed-off-by: Eliza Weisman <eliza@buoyant.io>1 parent 1aa9eff commit db7ac58
File tree
4 files changed
+90
-27
lines changed- tracing-subscriber
- benches
- src/registry
4 files changed
+90
-27
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
202 | 202 | | |
203 | 203 | | |
204 | 204 | | |
205 | | - | |
| 205 | + | |
| 206 | + | |
206 | 207 | | |
207 | 208 | | |
208 | | - | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
209 | 213 | | |
210 | 214 | | |
211 | 215 | | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
218 | 220 | | |
219 | 221 | | |
220 | 222 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | 1 | | |
4 | 2 | | |
5 | 3 | | |
| |||
15 | 13 | | |
16 | 14 | | |
17 | 15 | | |
18 | | - | |
19 | 16 | | |
20 | 17 | | |
21 | 18 | | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
28 | 24 | | |
29 | 25 | | |
30 | | - | |
| 26 | + | |
| 27 | + | |
31 | 28 | | |
32 | 29 | | |
33 | 30 | | |
34 | 31 | | |
35 | 32 | | |
36 | 33 | | |
37 | 34 | | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
| 35 | + | |
| 36 | + | |
45 | 37 | | |
| 38 | + | |
46 | 39 | | |
47 | 40 | | |
48 | 41 | | |
| |||
65 | 58 | | |
66 | 59 | | |
67 | 60 | | |
68 | | - | |
| 61 | + | |
69 | 62 | | |
70 | 63 | | |
71 | 64 | | |
| |||
75 | 68 | | |
76 | 69 | | |
77 | 70 | | |
78 | | - | |
| 71 | + | |
79 | 72 | | |
80 | 73 | | |
0 commit comments