Skip to content

Add Base64DecodingStream#8226

Merged
andrewlock merged 5 commits into
masterfrom
andrew/add-streaming-base64-decoder
Mar 6, 2026
Merged

Add Base64DecodingStream#8226
andrewlock merged 5 commits into
masterfrom
andrew/add-streaming-base64-decoder

Conversation

@andrewlock

Copy link
Copy Markdown
Member

Summary of changes

Adds Base64DecodingStream helper class

Reason for change

In a few places (particularly Remote Config and Newtonsoft.JSON) we have code like this:

string someValue = // 
var contentDecode = Convert.FromBase64String(someValue);
using var stream = new MemoryStream(contentDecode);

This takes an existing string, decodes it from base64 to a byte[], then feeds that byte[] into a MemoryStream. For big strings, that's a potentially large extra array allocation we can avoid. Instead, Base64DecodingStream acts effectively as a MemoryStream over the string, doing the decode either on the fly (.NET Core), or by using a pooled buffer to decode the input string in chunks.

Implementation details

Basically asked 🤖 Claude to do this, then reviewed and tidied up and iterated. The general idea is:

  • Keep track of where in the string we are
  • .NET Framework/.NET Standard only
    • Decode the next section of the string into an array-pool rented buffer, using the vendored Base64.DecodeFromUtf8InPlace
    • Copy the buffer into the destination byte[]
  • .NET Core only
    • Decode the next section of the string directly into the destination Span<T>

It didn't seem worth trying to unify these two paths, given the lack of Convert.TryFromBase64Chars in .NET Framework. We could use Base64.DecodeFromUtf8 to write directly to the destination span instead, but we would still need to do the narrowing, and doing that in the destination span is a little risky, as it would mean we write a bunch of bytes which are then leftover junk. It's probably still fine, but I don't know that it's worth the complexity/risk.

Warning

Rather than handle the case where you're passed a destination buffer that's <3 bytes, this implementation currently just throws. Otherwise we have to decode into a stackallocated buffer, and hang onto the "overflow" bytes to avoid returning 0 when we're not EOF, which is a bit of a pain. For the places we currently use it, I don't think this will be a problem, but if others disagree, we can handle the edge case too.

Test coverage

Added a variety of unit tests for the implementation

Other details

https://datadoghq.atlassian.net/browse/LANGPLAT-940

This will be part of a Remote config stack, but for now kept it agnostic

@andrewlock
andrewlock requested a review from a team as a code owner February 20, 2026 11:38
@andrewlock andrewlock added area:tracer The core tracer library (Datadog.Trace, does not include OpenTracing, native code, or integrations) type:performance Performance, speed, latency, resource usage (CPU, memory) labels Feb 20, 2026
@andrewlock
andrewlock force-pushed the andrew/add-streaming-base64-decoder branch from e202a95 to 77915fa Compare February 20, 2026 11:38

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e202a958b6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tracer/src/Datadog.Trace/Util/Streams/Base64DecodingStream.cs Outdated
@pr-commenter

pr-commenter Bot commented Feb 20, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-02-23 13:22:21

Comparing candidate commit 3d24011 in PR branch andrew/add-streaming-base64-decoder with baseline commit caa8d05 in branch master.

Found 8 performance improvements and 3 performance regressions! Performance is the same for 161 metrics, 20 unstable metrics.

scenario:Benchmarks.Trace.AgentWriterBenchmark.WriteAndFlushEnrichedTraces net6.0

  • 🟥 execution_time [+95.496ms; +95.642ms] or [+90.513%; +90.651%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorSimpleBody net6.0

  • 🟩 execution_time [-24.195ms; -17.968ms] or [-10.980%; -8.154%]

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

  • 🟩 execution_time [-17.639ms; -16.861ms] or [-8.706%; -8.322%]

scenario:Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces netcoreapp3.1

  • 🟥 execution_time [+57.700ms; +63.080ms] or [+37.085%; +40.544%]

scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSlice netcoreapp3.1

  • 🟥 throughput [-149.677op/s; -108.136op/s] or [-30.274%; -21.872%]

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

  • 🟩 execution_time [-110.274µs; -101.432µs] or [-9.756%; -8.974%]
  • 🟩 throughput [+87.364op/s; +95.545op/s] or [+9.875%; +10.800%]

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

  • 🟩 throughput [+243.555op/s; +419.277op/s] or [+13.312%; +22.917%]

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

  • 🟩 execution_time [-35.068ms; -30.341ms] or [-17.261%; -14.935%]

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

  • 🟩 throughput [+10656.229op/s; +13455.333op/s] or [+7.633%; +9.638%]

scenario:Benchmarks.Trace.SpanBenchmark.StartFinishScope net6.0

  • 🟩 execution_time [-23.435ms; -17.708ms] or [-10.835%; -8.187%]

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a new Base64DecodingStream helper class that provides memory-efficient base64 decoding by processing the input string on-the-fly without allocating the full decoded byte array. The implementation uses different strategies for different target frameworks: on .NET Core it decodes directly into the caller's buffer using Convert.TryFromBase64Chars, while on .NET Framework it uses a chunked approach with ArrayPool buffers and the vendored Base64.DecodeFromUtf8InPlace method.

Changes:

  • Added Base64DecodingStream class for streaming base64 decoding without full array allocation
  • Implemented framework-specific optimizations (direct decode on NETCOREAPP, chunked decode with ArrayPool on .NET Framework)
  • Added comprehensive test suite covering various input sizes, padding scenarios, buffer sizes, and error cases

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
tracer/src/Datadog.Trace/Util/Streams/Base64DecodingStream.cs Core implementation of the streaming base64 decoder with conditional compilation for different target frameworks
tracer/test/Datadog.Trace.Tests/Util/Base64DecodingStreamTests.cs Comprehensive test suite with 20+ test cases covering correctness, edge cases, padding, invalid input, and async operations

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tracer/src/Datadog.Trace/Util/Streams/Base64DecodingStream.cs
Comment thread tracer/test/Datadog.Trace.Tests/Util/Base64DecodingStreamTests.cs
@dd-trace-dotnet-ci-bot

dd-trace-dotnet-ci-bot Bot commented Feb 20, 2026

Copy link
Copy Markdown

Execution-Time Benchmarks Report ⏱️

Execution-time results for samples comparing This PR (8226) 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
duration74.24 ± (74.53 - 75.20) ms74.44 ± (74.52 - 74.84) ms+0.3%✅⬆️
.NET Framework 4.8 - Bailout
duration79.19 ± (79.09 - 79.50) ms79.38 ± (79.22 - 79.57) ms+0.2%✅⬆️
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1073.55 ± (1075.37 - 1080.96) ms1073.20 ± (1075.41 - 1081.33) ms-0.0%
.NET Core 3.1 - Baseline
process.internal_duration_ms22.79 ± (22.73 - 22.84) ms22.83 ± (22.78 - 22.87) ms+0.2%✅⬆️
process.time_to_main_ms85.77 ± (85.57 - 85.96) ms86.40 ± (86.20 - 86.61) ms+0.7%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.91 ± (10.90 - 10.91) MB10.91 ± (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_ms22.69 ± (22.64 - 22.73) ms22.75 ± (22.71 - 22.78) ms+0.3%✅⬆️
process.time_to_main_ms87.05 ± (86.82 - 87.29) ms87.86 ± (87.68 - 88.05) ms+0.9%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.95 ± (10.95 - 10.96) MB10.94 ± (10.94 - 10.95) MB-0.1%
runtime.dotnet.threads.count13 ± (13 - 13)13 ± (13 - 13)+0.0%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms246.05 ± (242.13 - 249.96) ms243.26 ± (239.27 - 247.26) ms-1.1%
process.time_to_main_ms488.72 ± (488.10 - 489.34) ms494.08 ± (493.39 - 494.77) ms+1.1%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed47.65 ± (47.63 - 47.67) MB47.65 ± (47.63 - 47.67) MB+0.0%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)-0.2%
.NET 6 - Baseline
process.internal_duration_ms21.54 ± (21.49 - 21.58) ms21.60 ± (21.56 - 21.64) ms+0.3%✅⬆️
process.time_to_main_ms74.73 ± (74.57 - 74.89) ms75.43 ± (75.27 - 75.58) ms+0.9%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.62 ± (10.62 - 10.63) MB10.64 ± (10.64 - 10.65) MB+0.2%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 6 - Bailout
process.internal_duration_ms21.56 ± (21.51 - 21.61) ms21.52 ± (21.46 - 21.57) ms-0.2%
process.time_to_main_ms76.37 ± (76.18 - 76.55) ms76.29 ± (76.12 - 76.47) ms-0.1%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.68 ± (10.68 - 10.69) MB10.76 ± (10.76 - 10.76) MB+0.7%✅⬆️
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms254.87 ± (251.70 - 258.05) ms257.51 ± (254.26 - 260.76) ms+1.0%✅⬆️
process.time_to_main_ms470.00 ± (469.40 - 470.60) ms475.96 ± (475.00 - 476.91) ms+1.3%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed48.38 ± (48.36 - 48.41) MB48.32 ± (48.30 - 48.34) MB-0.1%
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)-0.1%
.NET 8 - Baseline
process.internal_duration_ms19.68 ± (19.63 - 19.72) ms19.98 ± (19.93 - 20.02) ms+1.5%✅⬆️
process.time_to_main_ms74.22 ± (74.05 - 74.39) ms75.36 ± (75.19 - 75.54) ms+1.5%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.67 ± (7.66 - 7.67) MB7.67 ± (7.67 - 7.68) MB+0.1%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 8 - Bailout
process.internal_duration_ms19.57 ± (19.53 - 19.62) ms19.86 ± (19.81 - 19.91) ms+1.5%✅⬆️
process.time_to_main_ms75.23 ± (75.05 - 75.42) ms75.57 ± (75.42 - 75.73) ms+0.5%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.74 ± (7.73 - 7.75) MB7.73 ± (7.72 - 7.74) MB-0.1%
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms187.71 ± (186.75 - 188.67) ms191.81 ± (191.05 - 192.56) ms+2.2%✅⬆️
process.time_to_main_ms449.88 ± (449.16 - 450.60) ms456.45 ± (455.80 - 457.09) ms+1.5%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed36.06 ± (36.01 - 36.11) MB36.12 ± (36.09 - 36.16) MB+0.2%✅⬆️
runtime.dotnet.threads.count27 ± (27 - 27)27 ± (27 - 27)-0.5%

HttpMessageHandler

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration191.52 ± (191.14 - 192.06) ms191.82 ± (191.56 - 192.32) ms+0.2%✅⬆️
.NET Framework 4.8 - Bailout
duration195.74 ± (195.28 - 195.97) ms194.87 ± (194.73 - 195.28) ms-0.4%
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1132.12 ± (1135.03 - 1143.27) ms1134.11 ± (1136.28 - 1143.92) ms+0.2%✅⬆️
.NET Core 3.1 - Baseline
process.internal_duration_ms186.50 ± (186.12 - 186.87) ms186.50 ± (186.17 - 186.84) ms+0.0%✅⬆️
process.time_to_main_ms80.88 ± (80.63 - 81.12) ms80.83 ± (80.59 - 81.07) ms-0.1%
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.17 ± (16.14 - 16.20) MB16.18 ± (16.15 - 16.20) MB+0.0%✅⬆️
runtime.dotnet.threads.count19 ± (19 - 20)20 ± (19 - 20)+0.5%✅⬆️
.NET Core 3.1 - Bailout
process.internal_duration_ms186.25 ± (185.94 - 186.56) ms185.86 ± (185.48 - 186.25) ms-0.2%
process.time_to_main_ms81.98 ± (81.83 - 82.14) ms81.92 ± (81.74 - 82.09) ms-0.1%
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.20 ± (16.17 - 16.23) MB16.14 ± (16.11 - 16.17) MB-0.4%
runtime.dotnet.threads.count21 ± (21 - 21)21 ± (20 - 21)-0.4%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms435.81 ± (433.79 - 437.84) ms438.77 ± (436.76 - 440.79) ms+0.7%✅⬆️
process.time_to_main_ms468.60 ± (468.14 - 469.06) ms471.23 ± (470.48 - 471.99) ms+0.6%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed58.14 ± (58.02 - 58.26) MB57.82 ± (57.71 - 57.94) MB-0.5%
runtime.dotnet.threads.count29 ± (29 - 30)29 ± (29 - 30)-0.0%
.NET 6 - Baseline
process.internal_duration_ms189.40 ± (189.07 - 189.74) ms190.25 ± (189.93 - 190.58) ms+0.4%✅⬆️
process.time_to_main_ms69.57 ± (69.39 - 69.74) ms69.77 ± (69.62 - 69.92) ms+0.3%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.01 ± (15.84 - 16.17) MB16.25 ± (16.13 - 16.37) MB+1.5%✅⬆️
runtime.dotnet.threads.count18 ± (18 - 18)19 ± (19 - 19)+4.3%✅⬆️
.NET 6 - Bailout
process.internal_duration_ms188.99 ± (188.71 - 189.27) ms189.14 ± (188.87 - 189.42) ms+0.1%✅⬆️
process.time_to_main_ms70.37 ± (70.28 - 70.46) ms70.69 ± (70.59 - 70.78) ms+0.4%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.07 ± (15.92 - 16.23) MB16.02 ± (15.86 - 16.18) MB-0.3%
runtime.dotnet.threads.count19 ± (19 - 19)19 ± (19 - 19)-0.0%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms446.43 ± (444.73 - 448.12) ms449.45 ± (447.83 - 451.07) ms+0.7%✅⬆️
process.time_to_main_ms445.04 ± (444.50 - 445.58) ms447.09 ± (446.67 - 447.50) ms+0.5%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed58.24 ± (58.12 - 58.35) MB58.28 ± (58.17 - 58.38) MB+0.1%✅⬆️
runtime.dotnet.threads.count29 ± (29 - 29)29 ± (29 - 29)+0.1%✅⬆️
.NET 8 - Baseline
process.internal_duration_ms187.36 ± (186.91 - 187.80) ms188.36 ± (187.97 - 188.76) ms+0.5%✅⬆️
process.time_to_main_ms68.81 ± (68.59 - 69.02) ms69.14 ± (68.93 - 69.35) ms+0.5%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.59 ± (11.49 - 11.68) MB11.77 ± (11.72 - 11.82) MB+1.6%✅⬆️
runtime.dotnet.threads.count17 ± (17 - 18)18 ± (18 - 18)+3.8%✅⬆️
.NET 8 - Bailout
process.internal_duration_ms187.61 ± (187.32 - 187.90) ms187.35 ± (187.06 - 187.64) ms-0.1%
process.time_to_main_ms69.93 ± (69.80 - 70.07) ms70.11 ± (70.01 - 70.22) ms+0.3%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.82 ± (11.75 - 11.88) MB11.73 ± (11.66 - 11.81) MB-0.7%
runtime.dotnet.threads.count19 ± (19 - 19)19 ± (18 - 19)-2.0%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms360.27 ± (359.05 - 361.49) ms362.88 ± (361.37 - 364.39) ms+0.7%✅⬆️
process.time_to_main_ms427.74 ± (427.09 - 428.39) ms432.04 ± (431.28 - 432.81) ms+1.0%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed47.81 ± (47.78 - 47.84) MB47.71 ± (47.67 - 47.76) MB-0.2%
runtime.dotnet.threads.count29 ± (29 - 29)29 ± (29 - 29)+0.0%✅⬆️
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 (8226) - mean (75ms)  : 72, 77
    master - mean (75ms)  : 70, 80

    section Bailout
    This PR (8226) - mean (79ms)  : 78, 81
    master - mean (79ms)  : 76, 83

    section CallTarget+Inlining+NGEN
    This PR (8226) - mean (1,078ms)  : 1035, 1121
    master - mean (1,078ms)  : 1039, 1118

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 (8226) - mean (116ms)  : 113, 119
    master - mean (116ms)  : 113, 119

    section Bailout
    This PR (8226) - mean (118ms)  : 115, 120
    master - mean (117ms)  : 114, 120

    section CallTarget+Inlining+NGEN
    This PR (8226) - mean (773ms)  : 704, 841
    master - mean (763ms)  : 698, 827

Loading
FakeDbCommand (.NET 6)
gantt
    title Execution time (ms) FakeDbCommand (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8226) - mean (104ms)  : 101, 106
    master - mean (103ms)  : 100, 105

    section Bailout
    This PR (8226) - mean (104ms)  : 102, 106
    master - mean (105ms)  : 102, 107

    section CallTarget+Inlining+NGEN
    This PR (8226) - mean (758ms)  : 679, 836
    master - mean (757ms)  : 701, 813

Loading
FakeDbCommand (.NET 8)
gantt
    title Execution time (ms) FakeDbCommand (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8226) - mean (103ms)  : 101, 106
    master - mean (102ms)  : 99, 105

    section Bailout
    This PR (8226) - mean (103ms)  : 101, 106
    master - mean (103ms)  : 100, 105

    section CallTarget+Inlining+NGEN
    This PR (8226) - mean (688ms)  : 661, 714
    master - mean (667ms)  : 653, 680

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 (8226) - mean (192ms)  : 188, 196
    master - mean (192ms)  : 187, 196

    section Bailout
    This PR (8226) - mean (195ms)  : 192, 198
    master - mean (196ms)  : 192, 199

    section CallTarget+Inlining+NGEN
    This PR (8226) - mean (1,140ms)  : 1086, 1195
    master - mean (1,139ms)  : 1080, 1198

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 (8226) - mean (276ms)  : 272, 279
    master - mean (276ms)  : 270, 282

    section Bailout
    This PR (8226) - mean (276ms)  : 270, 282
    master - mean (276ms)  : 272, 280

    section CallTarget+Inlining+NGEN
    This PR (8226) - mean (936ms)  : 884, 988
    master - mean (932ms)  : 888, 976

Loading
HttpMessageHandler (.NET 6)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8226) - mean (268ms)  : 263, 274
    master - mean (267ms)  : 262, 273

    section Bailout
    This PR (8226) - mean (268ms)  : 264, 272
    master - mean (267ms)  : 263, 271

    section CallTarget+Inlining+NGEN
    This PR (8226) - mean (925ms)  : 897, 953
    master - mean (920ms)  : 896, 944

Loading
HttpMessageHandler (.NET 8)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8226) - mean (267ms)  : 261, 273
    master - mean (266ms)  : 260, 272

    section Bailout
    This PR (8226) - mean (267ms)  : 263, 270
    master - mean (267ms)  : 263, 271

    section CallTarget+Inlining+NGEN
    This PR (8226) - mean (827ms)  : 811, 843
    master - mean (821ms)  : 803, 838

Loading

@lucaspimentel lucaspimentel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice

@dromanol dromanol left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👌

@andrewlock
andrewlock merged commit e7ebff0 into master Mar 6, 2026
142 checks passed
@andrewlock
andrewlock deleted the andrew/add-streaming-base64-decoder branch March 6, 2026 14:10
@github-actions github-actions Bot added this to the vNext-v3 milestone Mar 6, 2026
andrewlock added a commit that referenced this pull request Mar 9, 2026
…#8228)

## Summary of changes

Adds a simple `IArrayPool<char>` for use by Newtonsoft.JSON, and uses it
everywhere we can

## Reason for change

Newtonsoft.JSON fundamentally works with .NET's `char` type (UTF-16),
(as opposed to System.Text.Json which works with UTF-8 where it can). To
do so, it needs to create a bunch of `char[]` instances to use as
buffers.

The `JsonTextReader` and `JsonTextWriter` abstractions allow plugging in
an `IArrayPool<char>` implementation, and luckily this matches (pretty
much exactly) the API exposed by the `ArrayPool` in corelib (+
vendored), so it's easy to implement.

This should help alleviate some GC pressure, as we currently do a fair
amount of serializing and deserializing.

## Implementation details

Pretty simple:
- Implement `IArrayPool<char>` using `ArrayPool<char>.Shared`
- Get 🤖 to find everywhere that we could use it (`JsonTextReader` and
`JsonTextWriter`) and initialize
- Fix some cases where these weren't being disposed.

> [!WARNING]
> It's important that we _do_ dispose these, so that the arrays are
correctly returned to the pool, so that we don't leak memory

There are actually other places we can update too, as this PR doesn't
cover the common `JsonConvert.Serialize()` etc, but I'll follow up with
those in a separate PR.

## Test coverage

All the existing tests should pass. I worked on this as part of general
perf work on remote config, so the results are a bit fuzzy (as I can't
remember if it includes the savings from #8226 as well), but the results
are pretty conclusive, especially for big payloads 😅


| Method | size | Mean | Error | Allocated |
| ---------------------------- | ----- | ----------: | ------------: |
---------: |
| DeserializeResponse_Original | Small | 22.71 ns | 2,076.771 ns | 23.83
KB |
| DeserializeResponse_Updated | Small | 13.70 us | 0.186 us | 17.23 KB |
| | | | | |
| DeserializeResponse_Original | Big | 1,953.04 us | 58,665.219 ns
|2,343.26 KB |
| DeserializeResponse_Updated | Big | 614.46 us | 11.988 us | 252.37 KB
|


## Other details


https://datadoghq.atlassian.net/browse/LANGPLAT-940

Discovered this while exploring remote config optimizations
andrewlock added a commit that referenced this pull request Mar 10, 2026
## Summary of changes

Stop sending the fixed `RootVersion = 1` with every remote config
request

## Reason for change

Currently we're sending a fixed value of `RootVersion = 1` for all our
remote config requests, but doing so causes the agent to repeatedly send
us all the root certificates, significantly increasing the payload size,
because it thinks we haven't seen them. Sending the "final" version,
acknowledges it, and stops all the extra data, saving ~35kB per call.

## Implementation details

- Deserialize the roots in `GetRcmResponse`, leaving them as base64
encoded `string`s (which is how they are sent in the payload)
- When processing the response, de-encode the _last_ root object, and
deserialize it
- Using the `Base64DecodingStream` introduced in #8226 to avoid extra
allocations
  - Using the `IArrayPool` from #8228
- Introduces a `MinimalTufRoot` (in contrast to `TufRoot`) so that we
only materialize what we need (the `roots.signed.version` key)

This implementation avoids ~35kB per call for subsequent remote config
requests.

## Test coverage

Added unit test, and did manual test with the real agent, to confirm the
expected behaviour (reduction in data sent)

## Other details

https://datadoghq.atlassian.net/browse/LANGPLAT-940

All part of the Remote Config perf stack
andrewlock added a commit that referenced this pull request Mar 11, 2026
## Summary of changes

Adds `Base64DecodingStream` helper class

## Reason for change

In a few places (particularly Remote Config and Newtonsoft.JSON) we have
code like this:

```csharp
string someValue = // 
var contentDecode = Convert.FromBase64String(someValue);
using var stream = new MemoryStream(contentDecode);
```

This takes an existing `string`, decodes it from base64 to a `byte[]`,
then feeds that `byte[]` into a `MemoryStream`. For big strings, that's
a potentially large extra array allocation we can avoid. Instead,
`Base64DecodingStream` acts effectively as a `MemoryStream` over the
`string`, doing the decode either on the fly (.NET Core), or by using a
pooled buffer to decode the input string in chunks.

## Implementation details

Basically asked 🤖 Claude to do this, then reviewed and tidied up and
iterated. The general idea is:

- Keep track of where in the string we are
- **.NET Framework/.NET Standard only**
- Decode the next section of the string into an array-pool rented
buffer, using the vendored `Base64.DecodeFromUtf8InPlace`
  - Copy the buffer into the destination `byte[]`
- **.NET Core only**
- Decode the next section of the string directly into the destination
`Span<T>`

It didn't seem worth trying to unify these two paths, given the lack of
`Convert.TryFromBase64Chars` in .NET Framework. We _could_ use
`Base64.DecodeFromUtf8` to write directly to the destination span
instead, but we would still need to do the narrowing, and doing that in
the destination span is a little risky, as it would mean we write a
bunch of bytes which are then leftover junk. It's _probably_ still fine,
but I don't know that it's worth the complexity/risk.

> [!WARNING]
> Rather than handle the case where you're passed a destination buffer
that's <3 bytes, this implementation currently just throws. Otherwise we
have to decode into a stackallocated buffer, and hang onto the
"overflow" bytes to avoid returning 0 when we're not EOF, which is a bit
of a pain. For the places we currently use it, I don't think this will
be a problem, but if others disagree, we can handle the edge case too.

## Test coverage

Added a variety of unit tests for the implementation

## Other details

https://datadoghq.atlassian.net/browse/LANGPLAT-940

This will be part of a Remote config stack, but for now kept it agnostic
andrewlock added a commit that referenced this pull request Mar 11, 2026
…#8228)

## Summary of changes

Adds a simple `IArrayPool<char>` for use by Newtonsoft.JSON, and uses it
everywhere we can

## Reason for change

Newtonsoft.JSON fundamentally works with .NET's `char` type (UTF-16),
(as opposed to System.Text.Json which works with UTF-8 where it can). To
do so, it needs to create a bunch of `char[]` instances to use as
buffers.

The `JsonTextReader` and `JsonTextWriter` abstractions allow plugging in
an `IArrayPool<char>` implementation, and luckily this matches (pretty
much exactly) the API exposed by the `ArrayPool` in corelib (+
vendored), so it's easy to implement.

This should help alleviate some GC pressure, as we currently do a fair
amount of serializing and deserializing.

## Implementation details

Pretty simple:
- Implement `IArrayPool<char>` using `ArrayPool<char>.Shared`
- Get 🤖 to find everywhere that we could use it (`JsonTextReader` and
`JsonTextWriter`) and initialize
- Fix some cases where these weren't being disposed.

> [!WARNING]
> It's important that we _do_ dispose these, so that the arrays are
correctly returned to the pool, so that we don't leak memory

There are actually other places we can update too, as this PR doesn't
cover the common `JsonConvert.Serialize()` etc, but I'll follow up with
those in a separate PR.

## Test coverage

All the existing tests should pass. I worked on this as part of general
perf work on remote config, so the results are a bit fuzzy (as I can't
remember if it includes the savings from #8226 as well), but the results
are pretty conclusive, especially for big payloads 😅


| Method | size | Mean | Error | Allocated |
| ---------------------------- | ----- | ----------: | ------------: |
---------: |
| DeserializeResponse_Original | Small | 22.71 ns | 2,076.771 ns | 23.83
KB |
| DeserializeResponse_Updated | Small | 13.70 us | 0.186 us | 17.23 KB |
| | | | | |
| DeserializeResponse_Original | Big | 1,953.04 us | 58,665.219 ns
|2,343.26 KB |
| DeserializeResponse_Updated | Big | 614.46 us | 11.988 us | 252.37 KB
|


## Other details


https://datadoghq.atlassian.net/browse/LANGPLAT-940

Discovered this while exploring remote config optimizations
andrewlock added a commit that referenced this pull request Mar 11, 2026
## Summary of changes

A variety of minor performance improvements to remote config

## Reason for change

I did some initial benchmarking of remote config, as well as running a
test app with ASM/Debugger enabled (which use RCM), and the results
weren't great. Given we make RCM requests every 5s, smallish changes
here should add up, though the real-world effect will be tricky to
gauge.

## Implementation details

Most of the individual changes are small. In summary:
- Making work lazy where possible (delaying creating collections)
  - Don't re-create dictionaries inside loops if not required
- Avoid creating empty dictionaries and collections for no-op RCM
responses
- Avoid creating empty collections in JSON objects when they're not in
the JSON
- Cache things that change rarely
- The `ExtraServicesProvider` will rarely see new service names, so
cache the array (as the collection is append-only)
- Don't create a new request each time, just update any values that may
have changed
  - Cache the capabilities array which will rarely change
- There's actually a potential threading bug around the use of
`BigInteger`. We probably could/should use `ulong` instead but that I'll
look at that in a separate PR
- Use abstractions introduced earlier in the stack (e.g. #8226, #8228)

Additionally, I did a little bit of cleanup:
- Remove unused members from the `IRcmSubscriptionManager` interface
- Add `#nullable enable` to the RCM types (and fix nullability where
required)

## Test coverage

Mostly covered by existing unit tests, also did some manual testing.
Finally, ran a few benchmarks, but it's a bit tricky to check reliably.
This benchmark is benchmarking `_manager.SendRequest(_rcmTracer, _ =>
_steadyStateResponseTask)` and passing the same response every time. In
practice, the response changes every time, so this isn't strictly
representative, but with the changes to request caching, this should
actually mean our improvements are _better_ than the original would be.
The regression in the .NET duration is curious, but I'm not massively
concerned, and the allocations are obviously down a lot at least


| Method | Runtime | Mean | Error | Allocated |
| ------------------------ | ------------------ | ----------: |
-----------: | --------: |
| PollSteadyState_Original | .NET 10.0 | 14,235.8 ns | 275.09 ns | 12.04
KB |
| PollSteadyState_Updated | .NET 10.0 | 20,472.0 ns | 1,352.567 ns |
3.90 KB |
| | | | | |
| PollSteadyState_Original | .NET 6.0 | 33,915.7 ns | 3,339.42 ns |
12.27 KB |
| PollSteadyState_Updated | .NET 6.0 | 14,979.7 ns | 447.784 ns | 3.99
KB |
| | | | | |
| PollSteadyState_Original | .NET Core 2.1 | 21,488.2 ns | 365.39 ns |
12.85 KB |
| PollSteadyState_Updated | .NET Core 2.1 | 17,847.2 ns | 693.510 ns |
4.04 KB |
| | | | | |
| PollSteadyState_Original | .NET Core 3.1 | 18,925.2 ns | 304.16 ns |
12.23 KB |
| PollSteadyState_Updated | .NET Core 3.1 | 15,407.7 ns | 313.26 ns |
4.01 KB |
| | | | | |
| PollSteadyState_Original | .NET Framework 4.8 | 20,946.8 ns | 320.06
ns | 14.21 KB |
| PollSteadyState_Updated | .NET Framework 4.8 | 16,635.2 ns | 266.526
ns | 4.40 KB |


## Other details
https://datadoghq.atlassian.net/browse/LANGPLAT-940

All part of the Remote Config perf stack. I think this is probably about
the end of it for now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:tracer The core tracer library (Datadog.Trace, does not include OpenTracing, native code, or integrations) type:performance Performance, speed, latency, resource usage (CPU, memory)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants