[perf] Optimise {ActivityTraceId|ActivitySpanId}.CopyTo() - #134135
Conversation
Optimise `ActivityTraceId.CopyTo()` and `ActivitySpanId.CopyTo()` by using `Convert.FromHexString()` in `ActivityTraceId.SetSpanFromHexChars()` to benefit from SIMD acceleration, where available, for a ~9x throughput improvement.
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @steveisok, @dotnet/area-system-diagnostics-tracing |
There was a problem hiding this comment.
🟡 Changes recommended
The new exception path can surface an argument name (“outBytes”) that doesn’t match the public CopyTo(... destination ...) parameter, making the resulting error message confusing.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Optimizes ActivityTraceId.CopyTo() / ActivitySpanId.CopyTo() by switching the internal hex-to-bytes conversion helper to use Convert.FromHexString(...) (on #if NET builds), aiming to leverage SIMD-accelerated hex decoding where available.
Changes:
- Updated
ActivityTraceId.SetSpanFromHexChars(...)to decode hex viaConvert.FromHexStringonNETbuilds. - Added a stricter success check (
OperationStatus.Doneand expectedbytesWritten) with an exception on mismatch. - Clarified the helper’s length precondition in the XML comment.
File summaries
| File | Description |
|---|---|
| src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs | Uses span-based Convert.FromHexString in SetSpanFromHexChars to speed up CopyTo() decoding on NET builds. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Lite
Rename parameter to match the public callers' parameter name.
There was a problem hiding this comment.
🟡 Changes recommended
The #else path still relies on Debug.Assert for a public-facing invariant, which can result in partial/truncated writes in Release builds and inconsistent behavior vs the new #if NET throwing behavior.
Get a fresh assessment by requesting another Copilot review.
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Lite
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
The #if NET path can partially write to destination before throwing on length mismatch, which is inconsistent with the non-NET path and should be guarded up-front (and ideally covered by tests).
Get a fresh assessment by requesting another Copilot review.
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 2
- Review effort level: Lite
- Validate destination length up-front. - Add test coverage for `ArgumentOutOfRangeException` being thrown when the destination is the wrong size.
There was a problem hiding this comment.
🟢 Approval recommended
The change is localized, preserves correct behavior for valid inputs, improves performance via the NET fast path, and adds targeted regression tests for the updated error handling.
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0 new
- Review effort level: Lite
Optimise
ActivityTraceId.CopyTo()andActivitySpanId.CopyTo()by usingConvert.FromHexString()inActivityTraceId.SetSpanFromHexChars()to benefit from SIMD acceleration, where available, for a ~9x throughput improvement when the IDs realistically vary per invocation.Adapted from open-telemetry/opentelemetry-dotnet#7775.
Benchmarks
ActivityTraceId.CopyToActivitySpanId.CopyToActivityTraceId.CopyToActivitySpanId.CopyToCode