Skip to content

Conversation

@tonyredondo
Copy link
Member

@tonyredondo tonyredondo commented Jan 15, 2026

Summary of changes

  • Add FixedSizeArrayPool and logger overloads to reduce per-log allocations.
  • Add RefStopwatch/StopwatchHelpers/CodeDuration and apply timing to TraceClock/TracerManagerFactory/Instrumentation and CI processors.
  • Lower init logs to Debug for CI protocol writer/sender and processors.

Reason for change

Reduce allocations and improve timing diagnostics in hot paths.

Implementation details

  • New utility helpers in Datadog.Trace.Util for allocation-free timing and pooling.
  • Logging changes to avoid params array allocations in common log paths.
  • Timing/log-level tweaks in CI protocol writer/sender and trace processors.

Test coverage

CI passes then all changes are good.

Other details

@pr-commenter
Copy link

pr-commenter bot commented Jan 15, 2026

Benchmarks

Benchmark execution time: 2026-01-19 10:03:11

Comparing candidate commit 4a649b1 in PR branch tony/testoptimization-performance-work-pr1 with baseline commit 0e23a10 in branch master.

Found 10 performance improvements and 7 performance regressions! Performance is the same for 157 metrics, 18 unstable metrics.

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.AllCycleSimpleBody netcoreapp3.1

  • 🟥 execution_time [+17.098ms; +23.435ms] or [+8.685%; +11.904%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorSimpleBody netcoreapp3.1

  • 🟩 execution_time [-22.780ms; -16.499ms] or [-10.545%; -7.637%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeLegacyArgs net6.0

  • 🟥 execution_time [+26.981ms; +27.516ms] or [+15.254%; +15.556%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeLegacyArgs netcoreapp3.1

  • 🟥 execution_time [+20.001ms; +20.542ms] or [+11.039%; +11.338%]

scenario:Benchmarks.Trace.AspNetCoreBenchmark.SendRequest netcoreapp3.1

  • 🟩 throughput [+551.969op/s; +1466.207op/s] or [+5.973%; +15.866%]

scenario:Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces net472

  • 🟩 throughput [+69.211op/s; +88.575op/s] or [+6.660%; +8.524%]

scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSliceWithPool net6.0

  • 🟥 execution_time [+96.970µs; +102.510µs] or [+9.437%; +9.976%]
  • 🟥 throughput [-88.590op/s; -83.640op/s] or [-9.103%; -8.594%]

scenario:Benchmarks.Trace.CharSliceBenchmark.OriginalCharSlice net6.0

  • 🟩 execution_time [-140.225µs; -131.682µs] or [-6.701%; -6.293%]
  • 🟩 throughput [+32.154op/s; +34.254op/s] or [+6.729%; +7.168%]

scenario:Benchmarks.Trace.DbCommandBenchmark.ExecuteNonQuery netcoreapp3.1

  • 🟩 throughput [+24880.312op/s; +32293.984op/s] or [+6.558%; +8.512%]

scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatBenchmark net6.0

  • 🟩 execution_time [-9.613µs; -5.095µs] or [-19.110%; -10.127%]
  • 🟩 throughput [+2152.850op/s; +3746.736op/s] or [+10.552%; +18.365%]

scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatBenchmark netcoreapp3.1

  • 🟩 throughput [+974.713op/s; +2549.541op/s] or [+5.209%; +13.626%]

scenario:Benchmarks.Trace.SerilogBenchmark.EnrichedLog netcoreapp3.1

  • 🟩 throughput [+14389.719op/s; +16325.055op/s] or [+8.808%; +9.992%]

scenario:Benchmarks.Trace.SpanBenchmark.StartFinishScope net472

  • 🟥 throughput [-54899.290op/s; -49078.462op/s] or [-5.976%; -5.343%]

scenario:Benchmarks.Trace.SpanBenchmark.StartFinishTwoScopes net472

  • 🟥 throughput [-28225.890op/s; -26599.129op/s] or [-5.960%; -5.616%]

@tonyredondo tonyredondo force-pushed the tony/testoptimization-performance-work-pr1 branch from ad6c32d to f0ccdd2 Compare January 15, 2026 22:06
@dd-trace-dotnet-ci-bot
Copy link

dd-trace-dotnet-ci-bot bot commented Jan 15, 2026

Execution-Time Benchmarks Report ⏱️

Execution-time results for samples comparing This PR (8070) and master.

✅ No regressions detected - check the details below

Full Metrics Comparison

FakeDbCommand

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration68.20 ± (68.25 - 68.43) ms68.34 ± (68.34 - 68.58) ms+0.2%✅⬆️
.NET Framework 4.8 - Bailout
duration72.33 ± (72.16 - 72.38) ms72.29 ± (72.22 - 72.43) ms-0.1%
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1002.46 ± (1007.75 - 1017.31) ms1000.86 ± (1006.88 - 1016.15) ms-0.2%
.NET Core 3.1 - Baseline
process.internal_duration_ms21.82 ± (21.80 - 21.85) ms21.98 ± (21.95 - 22.01) ms+0.7%✅⬆️
process.time_to_main_ms78.59 ± (78.45 - 78.73) ms78.85 ± (78.70 - 79.01) ms+0.3%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.91 ± (10.90 - 10.91) MB10.90 ± (10.90 - 10.91) MB-0.0%
runtime.dotnet.threads.count12 ± (12 - 12)12 ± (12 - 12)+0.0%
.NET Core 3.1 - Bailout
process.internal_duration_ms21.83 ± (21.80 - 21.87) ms22.00 ± (21.98 - 22.02) ms+0.8%✅⬆️
process.time_to_main_ms79.72 ± (79.63 - 79.81) ms79.95 ± (79.86 - 80.03) ms+0.3%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.94 ± (10.94 - 10.95) MB10.95 ± (10.94 - 10.95) MB+0.0%✅⬆️
runtime.dotnet.threads.count13 ± (13 - 13)13 ± (13 - 13)+0.0%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms241.80 ± (238.51 - 245.08) ms245.21 ± (241.84 - 248.58) ms+1.4%✅⬆️
process.time_to_main_ms468.78 ± (468.25 - 469.31) ms471.48 ± (470.90 - 472.05) ms+0.6%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed48.39 ± (48.37 - 48.42) MB48.41 ± (48.39 - 48.43) MB+0.0%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)-0.7%
.NET 6 - Baseline
process.internal_duration_ms20.64 ± (20.61 - 20.66) ms20.72 ± (20.69 - 20.75) ms+0.4%✅⬆️
process.time_to_main_ms68.12 ± (68.00 - 68.25) ms68.50 ± (68.38 - 68.62) ms+0.6%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.61 ± (10.60 - 10.61) MB10.62 ± (10.62 - 10.63) MB+0.2%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 6 - Bailout
process.internal_duration_ms20.62 ± (20.60 - 20.64) ms20.65 ± (20.62 - 20.67) ms+0.1%✅⬆️
process.time_to_main_ms69.11 ± (69.05 - 69.17) ms69.41 ± (69.35 - 69.47) ms+0.4%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.66 ± (10.65 - 10.66) MB10.72 ± (10.72 - 10.73) MB+0.6%✅⬆️
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms244.21 ± (241.80 - 246.61) ms241.65 ± (238.81 - 244.48) ms-1.0%
process.time_to_main_ms445.79 ± (445.34 - 446.25) ms449.43 ± (448.99 - 449.88) ms+0.8%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed49.08 ± (49.05 - 49.11) MB49.15 ± (49.12 - 49.18) MB+0.1%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)-0.0%
.NET 8 - Baseline
process.internal_duration_ms18.78 ± (18.75 - 18.81) ms18.74 ± (18.72 - 18.77) ms-0.2%
process.time_to_main_ms67.21 ± (67.09 - 67.33) ms67.11 ± (66.99 - 67.22) ms-0.2%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.65 ± (7.65 - 7.66) MB7.66 ± (7.65 - 7.67) MB+0.1%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 8 - Bailout
process.internal_duration_ms18.83 ± (18.80 - 18.85) ms18.78 ± (18.76 - 18.81) ms-0.2%
process.time_to_main_ms68.47 ± (68.40 - 68.55) ms68.32 ± (68.27 - 68.38) ms-0.2%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.71 ± (7.70 - 7.72) MB7.74 ± (7.73 - 7.76) MB+0.4%✅⬆️
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms177.28 ± (176.44 - 178.13) ms178.01 ± (177.06 - 178.96) ms+0.4%✅⬆️
process.time_to_main_ms428.74 ± (428.08 - 429.40) ms431.83 ± (431.21 - 432.46) ms+0.7%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed36.49 ± (36.46 - 36.53) MB36.46 ± (36.43 - 36.49) MB-0.1%
runtime.dotnet.threads.count27 ± (27 - 27)26 ± (26 - 26)-0.4%

HttpMessageHandler

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration192.14 ± (191.92 - 192.71) ms192.64 ± (192.26 - 193.21) ms+0.3%✅⬆️
.NET Framework 4.8 - Bailout
duration195.23 ± (194.93 - 195.43) ms195.73 ± (195.57 - 196.01) ms+0.3%✅⬆️
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1115.71 ± (1119.45 - 1129.34) ms1123.56 ± (1127.47 - 1136.49) ms+0.7%✅⬆️
.NET Core 3.1 - Baseline
process.internal_duration_ms186.16 ± (185.79 - 186.52) ms187.72 ± (187.31 - 188.14) ms+0.8%✅⬆️
process.time_to_main_ms80.45 ± (80.26 - 80.64) ms80.66 ± (80.42 - 80.91) ms+0.3%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.13 ± (16.10 - 16.16) MB16.10 ± (16.07 - 16.13) MB-0.2%
runtime.dotnet.threads.count20 ± (20 - 20)20 ± (19 - 20)-1.1%
.NET Core 3.1 - Bailout
process.internal_duration_ms186.36 ± (186.07 - 186.66) ms186.86 ± (186.46 - 187.26) ms+0.3%✅⬆️
process.time_to_main_ms81.59 ± (81.45 - 81.73) ms81.95 ± (81.75 - 82.15) ms+0.4%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.15 ± (16.12 - 16.17) MB16.19 ± (16.16 - 16.22) MB+0.3%✅⬆️
runtime.dotnet.threads.count21 ± (21 - 21)21 ± (21 - 21)-0.3%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms429.31 ± (426.42 - 432.21) ms428.63 ± (425.59 - 431.66) ms-0.2%
process.time_to_main_ms469.73 ± (469.21 - 470.25) ms472.62 ± (472.03 - 473.22) ms+0.6%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed58.82 ± (58.70 - 58.94) MB58.64 ± (58.52 - 58.76) MB-0.3%
runtime.dotnet.threads.count29 ± (29 - 29)29 ± (29 - 29)+0.1%✅⬆️
.NET 6 - Baseline
process.internal_duration_ms190.82 ± (190.38 - 191.26) ms192.54 ± (192.11 - 192.98) ms+0.9%✅⬆️
process.time_to_main_ms70.07 ± (69.86 - 70.27) ms70.60 ± (70.41 - 70.80) ms+0.8%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed15.85 ± (15.68 - 16.01) MB16.38 ± (16.35 - 16.42) MB+3.4%✅⬆️
runtime.dotnet.threads.count18 ± (18 - 18)19 ± (19 - 19)+4.7%✅⬆️
.NET 6 - Bailout
process.internal_duration_ms189.58 ± (189.34 - 189.82) ms190.09 ± (189.82 - 190.36) ms+0.3%✅⬆️
process.time_to_main_ms70.86 ± (70.74 - 70.98) ms71.13 ± (70.98 - 71.27) ms+0.4%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed15.87 ± (15.70 - 16.05) MB16.11 ± (15.96 - 16.26) MB+1.5%✅⬆️
runtime.dotnet.threads.count19 ± (18 - 19)19 ± (19 - 19)+3.2%✅⬆️
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms438.58 ± (434.75 - 442.41) ms451.05 ± (448.11 - 453.99) ms+2.8%✅⬆️
process.time_to_main_ms448.69 ± (448.17 - 449.20) ms456.29 ± (455.47 - 457.12) ms+1.7%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed58.77 ± (58.63 - 58.91) MB58.62 ± (58.49 - 58.75) MB-0.3%
runtime.dotnet.threads.count29 ± (29 - 29)29 ± (29 - 29)+0.0%✅⬆️
.NET 8 - Baseline
process.internal_duration_ms188.47 ± (188.17 - 188.77) ms192.13 ± (191.72 - 192.55) ms+1.9%✅⬆️
process.time_to_main_ms69.13 ± (69.00 - 69.26) ms70.30 ± (70.11 - 70.50) ms+1.7%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.78 ± (11.74 - 11.83) MB11.74 ± (11.72 - 11.76) MB-0.4%
runtime.dotnet.threads.count18 ± (18 - 18)18 ± (18 - 18)+0.2%✅⬆️
.NET 8 - Bailout
process.internal_duration_ms187.96 ± (187.62 - 188.31) ms189.02 ± (188.72 - 189.32) ms+0.6%✅⬆️
process.time_to_main_ms70.42 ± (70.30 - 70.54) ms70.67 ± (70.56 - 70.79) ms+0.4%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.82 ± (11.79 - 11.85) MB11.88 ± (11.85 - 11.91) MB+0.5%✅⬆️
runtime.dotnet.threads.count19 ± (19 - 19)19 ± (19 - 19)-0.2%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms364.26 ± (362.82 - 365.69) ms365.61 ± (364.39 - 366.83) ms+0.4%✅⬆️
process.time_to_main_ms433.43 ± (432.77 - 434.09) ms436.79 ± (436.06 - 437.51) ms+0.8%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed48.10 ± (48.05 - 48.14) MB48.19 ± (48.15 - 48.24) MB+0.2%✅⬆️
runtime.dotnet.threads.count29 ± (29 - 29)29 ± (29 - 29)+0.5%✅⬆️
Comparison explanation

Execution-time benchmarks measure the whole time it takes to execute a program, and are intended to measure the one-off costs. Cases where the execution time results for the PR are worse than latest master results are highlighted in **red**. The following thresholds were used for comparing the execution times:

  • Welch test with statistical test for significance of 5%
  • Only results indicating a difference greater than 5% and 5 ms are considered.

Note that these results are based on a single point-in-time result for each branch. For full results, see the dashboard.

Graphs show the p99 interval based on the mean and StdDev of the test run, as well as the mean value of the run (shown as a diamond below the graph).

Duration charts
FakeDbCommand (.NET Framework 4.8)
gantt
    title Execution time (ms) FakeDbCommand (.NET Framework 4.8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8070) - mean (68ms)  : 67, 70
    master - mean (68ms)  : 67, 70

    section Bailout
    This PR (8070) - mean (72ms)  : 71, 73
    master - mean (72ms)  : 71, 73

    section CallTarget+Inlining+NGEN
    This PR (8070) - mean (1,012ms)  : 944, 1079
    master - mean (1,013ms)  : 943, 1082

Loading
FakeDbCommand (.NET Core 3.1)
gantt
    title Execution time (ms) FakeDbCommand (.NET Core 3.1)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8070) - mean (106ms)  : 104, 108
    master - mean (106ms)  : 103, 108

    section Bailout
    This PR (8070) - mean (107ms)  : 106, 108
    master - mean (107ms)  : 105, 108

    section CallTarget+Inlining+NGEN
    This PR (8070) - mean (743ms)  : 695, 792
    master - mean (736ms)  : 685, 786

Loading
FakeDbCommand (.NET 6)
gantt
    title Execution time (ms) FakeDbCommand (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8070) - mean (94ms)  : 92, 96
    master - mean (94ms)  : 92, 96

    section Bailout
    This PR (8070) - mean (95ms)  : 94, 96
    master - mean (94ms)  : 93, 95

    section CallTarget+Inlining+NGEN
    This PR (8070) - mean (715ms)  : 673, 757
    master - mean (715ms)  : 680, 750

Loading
FakeDbCommand (.NET 8)
gantt
    title Execution time (ms) FakeDbCommand (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8070) - mean (92ms)  : 89, 94
    master - mean (92ms)  : 90, 95

    section Bailout
    This PR (8070) - mean (93ms)  : 92, 94
    master - mean (93ms)  : 92, 95

    section CallTarget+Inlining+NGEN
    This PR (8070) - mean (637ms)  : 622, 653
    master - mean (636ms)  : 622, 651

Loading
HttpMessageHandler (.NET Framework 4.8)
gantt
    title Execution time (ms) HttpMessageHandler (.NET Framework 4.8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8070) - mean (193ms)  : 188, 198
    master - mean (192ms)  : 188, 196

    section Bailout
    This PR (8070) - mean (196ms)  : 194, 198
    master - mean (195ms)  : 193, 198

    section CallTarget+Inlining+NGEN
    This PR (8070) - mean (1,132ms)  : 1067, 1197
    master - mean (1,124ms)  : 1053, 1195

Loading
HttpMessageHandler (.NET Core 3.1)
gantt
    title Execution time (ms) HttpMessageHandler (.NET Core 3.1)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8070) - mean (277ms)  : 271, 283
    master - mean (275ms)  : 270, 280

    section Bailout
    This PR (8070) - mean (277ms)  : 271, 282
    master - mean (276ms)  : 271, 280

    section CallTarget+Inlining+NGEN
    This PR (8070) - mean (930ms)  : 885, 976
    master - mean (926ms)  : 880, 972

Loading
HttpMessageHandler (.NET 6)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8070) - mean (271ms)  : 265, 278
    master - mean (269ms)  : 264, 274

    section Bailout
    This PR (8070) - mean (269ms)  : 265, 274
    master - mean (268ms)  : 265, 272

    section CallTarget+Inlining+NGEN
    This PR (8070) - mean (935ms)  : 887, 982
    master - mean (916ms)  : 855, 976

Loading
HttpMessageHandler (.NET 8)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8070) - mean (273ms)  : 261, 284
    master - mean (268ms)  : 263, 272

    section Bailout
    This PR (8070) - mean (269ms)  : 265, 274
    master - mean (268ms)  : 263, 273

    section CallTarget+Inlining+NGEN
    This PR (8070) - mean (833ms)  : 813, 852
    master - mean (829ms)  : 811, 847

Loading

@datadog-datadog-prod-us1

This comment has been minimized.

{
// Tells us which types are loaded, when, and how often.
SharedLogger.Debug("Logger retrieved for: {AssemblyQualifiedName}", classType.AssemblyQualifiedName);
if (SharedLogger.IsEnabled(LogEventLevel.Debug))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RuntimeType.AssemblyQualifiedName always allocates a new string and do some internals QCalls, is fast but we can avoid it.

Comment on lines +219 to +220
using var array = FixedSizeArrayPool<object?>.OneItemPool.Get();
array.Array[0] = property;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously we allocate an array for every single log line, here the idea is to have a small pool for it and reuse it.

{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _utcStart.Add(Elapsed);
get => _utcStart.AddTicks(StopwatchHelpers.GetElapsedTicks(Stopwatch.GetTimestamp() - _timestamp));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we really don't need to create a TimeSpan struct for this by calling Elapsed, we just count the ticks and add them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this measurably faster?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not in recent frameworks, in olders frameworks the codegen is just a little better.


namespace Datadog.Trace.Util;

internal struct CodeDuration : IDisposable
Copy link
Member Author

@tonyredondo tonyredondo Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the version for use in an async/await method because we cannot use ref struct there. So creating a CodeDuration in that case will allocate in the heap (struct boxing).

Below there's the ref struct version to avoid any heap allocation in a normal sync method.

}
}

internal ref struct CodeDurationRef
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref struct version to avoid an allocation in the heap.

#endif

#if NETCOREAPP3_0_OR_GREATER
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we clear the array if we know that T is a reference type (so we don't hold any reference from the GC)


namespace Datadog.Trace.Util;

internal ref struct RefStopwatch
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normal Stopwatch is a class, and we use multiple in the Initialization and in some of the CI visibility hotpath.
This version doesn't allocate (ref struct) but it's only useful in sync method, for async method is better to use the existing Stopwatch.

Copy link
Member

@lucaspimentel lucaspimentel Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we include this as a code comment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 4a649b1

@tonyredondo tonyredondo force-pushed the tony/testoptimization-performance-work-pr1 branch from 7a2fadb to c58aa40 Compare January 16, 2026 12:39
@tonyredondo tonyredondo marked this pull request as ready for review January 16, 2026 12:40
@tonyredondo tonyredondo requested review from a team as code owners January 16, 2026 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants