Add Base64DecodingStream#8226
Conversation
e202a95 to
77915fa
Compare
There was a problem hiding this comment.
💡 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".
BenchmarksBenchmark execution time: 2026-02-23 13:22:21 Comparing candidate commit 3d24011 in PR branch 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
scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorSimpleBody net6.0
scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeLegacyArgs netcoreapp3.1
scenario:Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces netcoreapp3.1
scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSlice netcoreapp3.1
scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSliceWithPool net6.0
scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatAspectBenchmark netcoreapp3.1
scenario:Benchmarks.Trace.Log4netBenchmark.EnrichedLog netcoreapp3.1
scenario:Benchmarks.Trace.NLogBenchmark.EnrichedLog netcoreapp3.1
scenario:Benchmarks.Trace.SpanBenchmark.StartFinishScope net6.0
|
77915fa to
aa05f67
Compare
There was a problem hiding this comment.
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
Base64DecodingStreamclass 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.
Execution-Time Benchmarks Report ⏱️Execution-time results for samples comparing This PR (8226) and master. ✅ No regressions detected - check the details below Full Metrics ComparisonFakeDbCommand
HttpMessageHandler
Comparison explanationExecution-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:
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 chartsFakeDbCommand (.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
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
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
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
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
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
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
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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
c331178 to
33c73ce
Compare
33c73ce to
3d24011
Compare
…#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
## 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
## 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
…#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
## 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
Summary of changes
Adds
Base64DecodingStreamhelper classReason for change
In a few places (particularly Remote Config and Newtonsoft.JSON) we have code like this:
This takes an existing
string, decodes it from base64 to abyte[], then feeds thatbyte[]into aMemoryStream. For big strings, that's a potentially large extra array allocation we can avoid. Instead,Base64DecodingStreamacts effectively as aMemoryStreamover thestring, 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:
Base64.DecodeFromUtf8InPlacebyte[]Span<T>It didn't seem worth trying to unify these two paths, given the lack of
Convert.TryFromBase64Charsin .NET Framework. We could useBase64.DecodeFromUtf8to 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