Commit 14f72c1
authored
Reduce allocations in
## Summary of changes
Replace `ArraySegment<Span>` with `SpanCollection`
## Reason for change
`ArraySegment<Span>` is a wrapper around an array, and by design, always
requires allocating an array.
However, if we look at the [distribution of
traces](https://app.datadoghq.com/notebook/13270656/partial-flush-worth-it-or-not?computational_onboarding=true&fullscreen_end_ts=1760111663000&fullscreen_paused=true&fullscreen_refresh_mode=paused&fullscreen_start_ts=1759829808000&fullscreen_widget=3c8idtpl)
received for processing, we see that 50% of traces only have a single
span.
Consequently, `SpanCollection` takes a similar approach [to
`StringValues`](https://andrewlock.net/a-brief-look-at-stringvalues/),
in which it is an abstraction around _either_ a single `Span` _or_ a
`Span[]`. This means we can avoid allocating the array entirely until we
need to.
For small traces (with a single span), this saves 56 Bytes per
scope/trace or ~8% of the basic trace size. For larger traces,
`SpanCollection` and `ArraySegment<Span>` are essentially identical, so
the allocation is the same.
Given `SpanCollection` (and `ArraySegment` actually) are `readonly
struct`, also added `in` to the signatures (given that both structs are
the same size and > pointer size).
> The only practical way I could see to actually make `SpanCollection`
pointer-sized is to remove the `Count` parameter. But that means we
_either_ need to allocate an "ArraySegment" wrapper around the Array, to
hold the count, _or_ we always allocate an array of the "correct"
length. I explored the latter in a separate PR, using an array pool
during the "builder" step, and then allocating an array of the correct
size subsequently, but the allocation gains were marginal, and it didn't
## Implementation details
The changes are essentially:
- `SpanCollection` holds _either_ a `Span` _or_ a `Span[]` (_or_ `null`)
- We store this in the same field (much like `StringValues` does), as it
reduces the size of the struct which brings small perf benefits
- Pass the span around via `in` to reduce chance of defensive copies
- Fix/replace uses of Moq which requires different usage for `in`/`ref`
fields, and can't provide the same functionality as a stub
## Test coverage
Added some unit tests for the implementation, but the important thing is
the benchmarks. We see an 8-8.5% reduction in the allocations for create
span/create scope:
| Benchmark | Base Allocated | Diff Allocated | Change | Change % |
|:----------|-----------:|-----------:|--------:|--------:|
| Benchmarks.Trace.SpanBenchmark.StartFinishScope‑net6.0 | 696 B |
640 B | -56 B | -8.05%
| Benchmarks.Trace.SpanBenchmark.StartFinishScope‑netcoreapp3.1 |
696 B | 640 B | -56 B | -8.05%
| Benchmarks.Trace.SpanBenchmark.StartFinishScope‑net472 | 658 B |
602 B | -56 B | -8.51%
| Benchmarks.Trace.SpanBenchmark.StartFinishSpan‑net472 | 578 B |
522 B | -56 B | -9.69%
| Benchmarks.Trace.SpanBenchmark.StartFinishSpan‑net6.0 | 576 B |
520 B | -56 B | -9.72%
| Benchmarks.Trace.SpanBenchmark.StartFinishSpan‑netcoreapp3.1 |
576 B | 520 B | -56 B | -9.72%
Other benchmarks which create a single span see similar improvements.
Note that the _two_ scope benchmark (added in #7869) shows essentially
no change (as expected).
## Other details
I tried a variety of variations on this approach:
- Keep separate fields for `Span` and `Span[]`
- Nicer as it's type safe again, but increases allocations (by a
pointer) so prob not worth it as this is hot path
- Don't pass via `in`
- Slows things down slightly
- Remove the `Count` field (and make `SpanCollection` pointer-sized)
- Requires allocating exact-sized arrays, so not practical to handle
array growth
- Same as above, but use an array pool for the build stage, and then
only allocate the fixed size on close
- Does show improvements in allocation, but more complexity. May be
worth considering (I'll create a separate PR for it)
- Don't have a builder, just use an array pool + a Count field, and rely
on reliably cleaning up the pooling,
https://datadoghq.atlassian.net/browse/LANGPLAT-841TraceContext (#7874)1 parent 9edde96 commit 14f72c1
File tree
47 files changed
+856
-341
lines changed- tracer
- src/Datadog.Trace
- Agent
- MessagePack
- TraceSamplers
- Ci
- Agent
- Processors
- Processors
- Util
- test
- Datadog.Trace.ClrProfiler.IntegrationTests/CI/Agent
- Datadog.Trace.IntegrationTests
- DiagnosticListeners
- Datadog.Trace.Security.Unit.Tests
- Datadog.Trace.Tests
- Agent
- MessagePack
- Configuration
- Util
- benchmarks/Benchmarks.Trace
- Asm
- test-applications/integrations/Samples.InstrumentedTests
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
47 files changed
+856
-341
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
131 | | - | |
| 131 | + | |
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
146 | | - | |
| 146 | + | |
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
| |||
395 | 395 | | |
396 | 396 | | |
397 | 397 | | |
398 | | - | |
| 398 | + | |
399 | 399 | | |
400 | 400 | | |
401 | 401 | | |
| |||
427 | 427 | | |
428 | 428 | | |
429 | 429 | | |
| 430 | + | |
430 | 431 | | |
431 | 432 | | |
432 | | - | |
433 | | - | |
434 | | - | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
435 | 436 | | |
436 | 437 | | |
437 | | - | |
| 438 | + | |
438 | 439 | | |
439 | | - | |
440 | | - | |
| 440 | + | |
441 | 441 | | |
442 | | - | |
| 442 | + | |
443 | 443 | | |
444 | 444 | | |
445 | 445 | | |
446 | 446 | | |
447 | 447 | | |
448 | 448 | | |
449 | | - | |
| 449 | + | |
450 | 450 | | |
451 | 451 | | |
452 | 452 | | |
| |||
456 | 456 | | |
457 | 457 | | |
458 | 458 | | |
459 | | - | |
460 | | - | |
| 459 | + | |
| 460 | + | |
461 | 461 | | |
462 | 462 | | |
463 | 463 | | |
| |||
466 | 466 | | |
467 | 467 | | |
468 | 468 | | |
469 | | - | |
| 469 | + | |
470 | 470 | | |
471 | | - | |
| 471 | + | |
472 | 472 | | |
473 | | - | |
| 473 | + | |
474 | 474 | | |
475 | 475 | | |
476 | 476 | | |
| |||
479 | 479 | | |
480 | 480 | | |
481 | 481 | | |
482 | | - | |
| 482 | + | |
483 | 483 | | |
484 | 484 | | |
485 | 485 | | |
486 | | - | |
| 486 | + | |
487 | 487 | | |
488 | 488 | | |
489 | 489 | | |
| |||
492 | 492 | | |
493 | 493 | | |
494 | 494 | | |
495 | | - | |
| 495 | + | |
496 | 496 | | |
497 | 497 | | |
498 | 498 | | |
| |||
503 | 503 | | |
504 | 504 | | |
505 | 505 | | |
506 | | - | |
| 506 | + | |
507 | 507 | | |
508 | 508 | | |
509 | 509 | | |
| |||
515 | 515 | | |
516 | 516 | | |
517 | 517 | | |
518 | | - | |
| 518 | + | |
519 | 519 | | |
520 | 520 | | |
521 | 521 | | |
522 | 522 | | |
523 | 523 | | |
524 | 524 | | |
525 | 525 | | |
526 | | - | |
| 526 | + | |
527 | 527 | | |
528 | 528 | | |
529 | | - | |
| 529 | + | |
530 | 530 | | |
531 | 531 | | |
532 | 532 | | |
533 | | - | |
| 533 | + | |
534 | 534 | | |
535 | 535 | | |
536 | 536 | | |
| |||
539 | 539 | | |
540 | 540 | | |
541 | 541 | | |
542 | | - | |
| 542 | + | |
543 | 543 | | |
544 | 544 | | |
545 | 545 | | |
| |||
602 | 602 | | |
603 | 603 | | |
604 | 604 | | |
605 | | - | |
| 605 | + | |
606 | 606 | | |
607 | 607 | | |
608 | | - | |
| 608 | + | |
609 | 609 | | |
610 | 610 | | |
611 | 611 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| 34 | + | |
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
38 | 40 | | |
39 | | - | |
| 41 | + | |
40 | 42 | | |
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
44 | 46 | | |
45 | 47 | | |
46 | | - | |
| 48 | + | |
47 | 49 | | |
48 | | - | |
| 50 | + | |
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
| |||
Lines changed: 33 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | | - | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
75 | 76 | | |
76 | 77 | | |
77 | 78 | | |
78 | | - | |
79 | | - | |
| 79 | + | |
| 80 | + | |
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
| |||
85 | 86 | | |
86 | 87 | | |
87 | 88 | | |
88 | | - | |
89 | | - | |
| 89 | + | |
| 90 | + | |
90 | 91 | | |
91 | 92 | | |
92 | 93 | | |
| |||
128 | 129 | | |
129 | 130 | | |
130 | 131 | | |
131 | | - | |
132 | | - | |
| 132 | + | |
| 133 | + | |
133 | 134 | | |
134 | 135 | | |
135 | 136 | | |
| |||
161 | 162 | | |
162 | 163 | | |
163 | 164 | | |
164 | | - | |
| 165 | + | |
165 | 166 | | |
166 | 167 | | |
167 | 168 | | |
| |||
225 | 226 | | |
226 | 227 | | |
227 | 228 | | |
228 | | - | |
| 229 | + | |
229 | 230 | | |
230 | | - | |
| 231 | + | |
231 | 232 | | |
232 | 233 | | |
233 | 234 | | |
| |||
238 | 239 | | |
239 | 240 | | |
240 | 241 | | |
241 | | - | |
| 242 | + | |
242 | 243 | | |
243 | 244 | | |
244 | 245 | | |
245 | 246 | | |
246 | | - | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
247 | 254 | | |
248 | 255 | | |
249 | 256 | | |
250 | 257 | | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
251 | 269 | | |
252 | | - | |
| 270 | + | |
253 | 271 | | |
254 | | - | |
| 272 | + | |
255 | 273 | | |
256 | 274 | | |
257 | 275 | | |
| |||
260 | 278 | | |
261 | 279 | | |
262 | 280 | | |
263 | | - | |
| 281 | + | |
264 | 282 | | |
265 | 283 | | |
266 | 284 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
| 76 | + | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | | - | |
| 94 | + | |
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
| |||
0 commit comments