-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix ToString and ToWString on large strings (#5930 -> v2) #5940
Conversation
## Summary of changes Fix ToString and ToWString on large strings ## Reason for change Those two methods have a fast-path, where we try to fit the result in a pre-allocated buffer. But when the string is too long, we incorrectly assumed that `WideCharToMultiByte`/`MultiByteToWideChar` would return the needed size. In truth, [it only does so when `cchWideChar` is 0](https://learn.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar#return-value). Because of that mistake, we were returning an empty string. ## Implementation details Added a few minor changes while I was at it: - For arrays, directly use the array instead of `&array[0]` - For strings, use `str.data()` instead of `&str[0]` - Don't zero the buffer since we're going to only use what is filled ## Test coverage Added a unit test for long strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Datadog ReportBranch report: ❌ 1 Failed (1 Known Flaky), 347368 Passed, 2080 Skipped, 15h 37m 20.14s Total Time ❌ Failed Tests (1)
|
Execution-Time Benchmarks Report ⏱️Execution-time results for samples comparing the following branches/commits: 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 shown 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). gantt
title Execution time (ms) FakeDbCommand (.NET Framework 4.6.2)
dateFormat X
axisFormat %s
todayMarker off
section Baseline
This PR (5940) - mean (75ms) : 65, 84
. : milestone, 75,
section CallTarget+Inlining+NGEN
This PR (5940) - mean (1,028ms) : 1007, 1049
. : milestone, 1028,
gantt
title Execution time (ms) FakeDbCommand (.NET Core 3.1)
dateFormat X
axisFormat %s
todayMarker off
section Baseline
This PR (5940) - mean (110ms) : 106, 113
. : milestone, 110,
section CallTarget+Inlining+NGEN
This PR (5940) - mean (710ms) : 690, 729
. : milestone, 710,
gantt
title Execution time (ms) FakeDbCommand (.NET 6)
dateFormat X
axisFormat %s
todayMarker off
section Baseline
This PR (5940) - mean (93ms) : 91, 95
. : milestone, 93,
section CallTarget+Inlining+NGEN
This PR (5940) - mean (667ms) : 645, 689
. : milestone, 667,
gantt
title Execution time (ms) HttpMessageHandler (.NET Framework 4.6.2)
dateFormat X
axisFormat %s
todayMarker off
section Baseline
This PR (5940) - mean (190ms) : 187, 193
. : milestone, 190,
section CallTarget+Inlining+NGEN
This PR (5940) - mean (1,115ms) : 1095, 1135
. : milestone, 1115,
gantt
title Execution time (ms) HttpMessageHandler (.NET Core 3.1)
dateFormat X
axisFormat %s
todayMarker off
section Baseline
This PR (5940) - mean (279ms) : 270, 287
. : milestone, 279,
section CallTarget+Inlining+NGEN
This PR (5940) - mean (880ms) : 858, 901
. : milestone, 880,
gantt
title Execution time (ms) HttpMessageHandler (.NET 6)
dateFormat X
axisFormat %s
todayMarker off
section Baseline
This PR (5940) - mean (264ms) : 260, 268
. : milestone, 264,
section CallTarget+Inlining+NGEN
This PR (5940) - mean (866ms) : 840, 892
. : milestone, 866,
|
Benchmarks Report for tracer 🐌Benchmarks for #5940 compared to master:
The following thresholds were used for comparing the benchmark speeds:
Allocation changes below 0.5% are ignored. Benchmark detailsBenchmarks.Trace.ActivityBenchmark - Same speed ✔️ Same allocations ✔️Raw results
Benchmarks.Trace.AgentWriterBenchmark - Same speed ✔️ Same allocations ✔️Raw results
Benchmarks.Trace.AspNetCoreBenchmark - Same speed ✔️ Same allocations ✔️Raw results
Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark - Same speed ✔️ Same allocations ✔️Raw results
Benchmarks.Trace.DbCommandBenchmark - Same speed ✔️ Same allocations ✔️Raw results
Benchmarks.Trace.ElasticsearchBenchmark - Same speed ✔️ Same allocations ✔️Raw results
Benchmarks.Trace.GraphQLBenchmark - Slower
|
Benchmark | diff/base | Base Median (ns) | Diff Median (ns) | Modality |
---|---|---|---|---|
Benchmarks.Trace.GraphQLBenchmark.ExecuteAsync‑netcoreapp3.1 | 1.114 | 1,519.07 | 1,692.65 |
Benchmark | base/diff | Base Median (ns) | Diff Median (ns) | Modality |
---|---|---|---|---|
Benchmarks.Trace.GraphQLBenchmark.ExecuteAsync‑net6.0 | 1.143 | 1,293.25 | 1,131.48 |
Raw results
Branch | Method | Toolchain | Mean | StdError | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
master | ExecuteAsync |
net6.0 | 1.29μs | 1.03ns | 4.01ns | 0.0136 | 0 | 0 | 952 B |
master | ExecuteAsync |
netcoreapp3.1 | 1.52μs | 1.27ns | 4.76ns | 0.0128 | 0 | 0 | 952 B |
master | ExecuteAsync |
net472 | 1.79μs | 0.964ns | 3.61ns | 0.145 | 0 | 0 | 915 B |
#5940 | ExecuteAsync |
net6.0 | 1.13μs | 0.522ns | 2.02ns | 0.0136 | 0 | 0 | 952 B |
#5940 | ExecuteAsync |
netcoreapp3.1 | 1.69μs | 0.601ns | 2.25ns | 0.0127 | 0 | 0 | 952 B |
#5940 | ExecuteAsync |
net472 | 1.75μs | 0.583ns | 2.26ns | 0.145 | 0 | 0 | 915 B |
Benchmarks.Trace.HttpClientBenchmark - Same speed ✔️ Same allocations ✔️
Raw results
Branch | Method | Toolchain | Mean | StdError | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
master | SendAsync |
net6.0 | 4.13μs | 1.69ns | 6.31ns | 0.0311 | 0 | 0 | 2.22 KB |
master | SendAsync |
netcoreapp3.1 | 5.08μs | 1.83ns | 7.09ns | 0.0356 | 0 | 0 | 2.76 KB |
master | SendAsync |
net472 | 7.92μs | 8.94ns | 34.6ns | 0.498 | 0 | 0 | 3.15 KB |
#5940 | SendAsync |
net6.0 | 4.15μs | 2.06ns | 7.97ns | 0.031 | 0 | 0 | 2.22 KB |
#5940 | SendAsync |
netcoreapp3.1 | 5.25μs | 4.75ns | 18.4ns | 0.0367 | 0 | 0 | 2.76 KB |
#5940 | SendAsync |
net472 | 7.81μs | 1.24ns | 4.8ns | 0.497 | 0 | 0 | 3.15 KB |
Benchmarks.Trace.ILoggerBenchmark - Same speed ✔️ Same allocations ✔️
Raw results
Branch | Method | Toolchain | Mean | StdError | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
master | EnrichedLog |
net6.0 | 1.48μs | 0.987ns | 3.82ns | 0.0231 | 0 | 0 | 1.64 KB |
master | EnrichedLog |
netcoreapp3.1 | 2.37μs | 1.04ns | 3.88ns | 0.0225 | 0 | 0 | 1.64 KB |
master | EnrichedLog |
net472 | 2.77μs | 1.52ns | 5.69ns | 0.249 | 0 | 0 | 1.57 KB |
#5940 | EnrichedLog |
net6.0 | 1.53μs | 0.537ns | 2.08ns | 0.0231 | 0 | 0 | 1.64 KB |
#5940 | EnrichedLog |
netcoreapp3.1 | 2.17μs | 1.57ns | 5.87ns | 0.0228 | 0 | 0 | 1.64 KB |
#5940 | EnrichedLog |
net472 | 2.82μs | 1.61ns | 6.25ns | 0.249 | 0 | 0 | 1.57 KB |
Benchmarks.Trace.Log4netBenchmark - Same speed ✔️ Same allocations ✔️
Raw results
Branch | Method | Toolchain | Mean | StdError | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
master | EnrichedLog |
net6.0 | 116μs | 148ns | 575ns | 0.0584 | 0 | 0 | 4.28 KB |
master | EnrichedLog |
netcoreapp3.1 | 121μs | 261ns | 1.01μs | 0 | 0 | 0 | 4.28 KB |
master | EnrichedLog |
net472 | 150μs | 260ns | 1.01μs | 0.67 | 0.223 | 0 | 4.46 KB |
#5940 | EnrichedLog |
net6.0 | 116μs | 189ns | 731ns | 0.0581 | 0 | 0 | 4.28 KB |
#5940 | EnrichedLog |
netcoreapp3.1 | 117μs | 199ns | 772ns | 0.0583 | 0 | 0 | 4.28 KB |
#5940 | EnrichedLog |
net472 | 150μs | 347ns | 1.34μs | 0.675 | 0.225 | 0 | 4.46 KB |
Benchmarks.Trace.NLogBenchmark - Same speed ✔️ Same allocations ✔️
Raw results
Branch | Method | Toolchain | Mean | StdError | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
master | EnrichedLog |
net6.0 | 2.98μs | 0.767ns | 2.87ns | 0.0311 | 0 | 0 | 2.2 KB |
master | EnrichedLog |
netcoreapp3.1 | 4.19μs | 1.95ns | 7.29ns | 0.0294 | 0 | 0 | 2.2 KB |
master | EnrichedLog |
net472 | 4.84μs | 0.795ns | 2.87ns | 0.32 | 0 | 0 | 2.02 KB |
#5940 | EnrichedLog |
net6.0 | 3.03μs | 0.854ns | 3.31ns | 0.0303 | 0 | 0 | 2.2 KB |
#5940 | EnrichedLog |
netcoreapp3.1 | 4.26μs | 1.59ns | 5.96ns | 0.0299 | 0 | 0 | 2.2 KB |
#5940 | EnrichedLog |
net472 | 4.99μs | 1.57ns | 5.44ns | 0.319 | 0 | 0 | 2.02 KB |
Benchmarks.Trace.RedisBenchmark - Same speed ✔️ Same allocations ✔️
Raw results
Branch | Method | Toolchain | Mean | StdError | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
master | SendReceive |
net6.0 | 1.33μs | 1.3ns | 5.04ns | 0.016 | 0 | 0 | 1.14 KB |
master | SendReceive |
netcoreapp3.1 | 1.74μs | 0.728ns | 2.62ns | 0.0156 | 0 | 0 | 1.14 KB |
master | SendReceive |
net472 | 2.21μs | 1.04ns | 4.04ns | 0.183 | 0.0011 | 0 | 1.16 KB |
#5940 | SendReceive |
net6.0 | 1.44μs | 0.651ns | 2.44ns | 0.0159 | 0 | 0 | 1.14 KB |
#5940 | SendReceive |
netcoreapp3.1 | 1.73μs | 2.79ns | 10.8ns | 0.015 | 0 | 0 | 1.14 KB |
#5940 | SendReceive |
net472 | 2.16μs | 2.56ns | 9.93ns | 0.183 | 0 | 0 | 1.16 KB |
Benchmarks.Trace.SerilogBenchmark - Same speed ✔️ Same allocations ✔️
Raw results
Branch | Method | Toolchain | Mean | StdError | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
master | EnrichedLog |
net6.0 | 2.74μs | 0.858ns | 3.21ns | 0.0219 | 0 | 0 | 1.6 KB |
master | EnrichedLog |
netcoreapp3.1 | 3.83μs | 2ns | 7.75ns | 0.021 | 0 | 0 | 1.65 KB |
master | EnrichedLog |
net472 | 4.44μs | 2.7ns | 10.5ns | 0.323 | 0 | 0 | 2.04 KB |
#5940 | EnrichedLog |
net6.0 | 2.79μs | 1.02ns | 3.95ns | 0.0224 | 0 | 0 | 1.6 KB |
#5940 | EnrichedLog |
netcoreapp3.1 | 3.86μs | 1.5ns | 5.6ns | 0.0213 | 0 | 0 | 1.65 KB |
#5940 | EnrichedLog |
net472 | 4.33μs | 1.15ns | 4.3ns | 0.323 | 0 | 0 | 2.04 KB |
Benchmarks.Trace.SpanBenchmark - Same speed ✔️ Same allocations ✔️
Raw results
Branch | Method | Toolchain | Mean | StdError | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
master | StartFinishSpan |
net6.0 | 408ns | 0.917ns | 3.55ns | 0.00808 | 0 | 0 | 576 B |
master | StartFinishSpan |
netcoreapp3.1 | 560ns | 0.289ns | 1.12ns | 0.00766 | 0 | 0 | 576 B |
master | StartFinishSpan |
net472 | 581ns | 0.615ns | 2.22ns | 0.0917 | 0 | 0 | 578 B |
master | StartFinishScope |
net6.0 | 487ns | 0.474ns | 1.77ns | 0.00969 | 0 | 0 | 696 B |
master | StartFinishScope |
netcoreapp3.1 | 732ns | 0.497ns | 1.92ns | 0.00924 | 0 | 0 | 696 B |
master | StartFinishScope |
net472 | 947ns | 2.16ns | 8.08ns | 0.104 | 0 | 0 | 658 B |
#5940 | StartFinishSpan |
net6.0 | 394ns | 0.14ns | 0.523ns | 0.0081 | 0 | 0 | 576 B |
#5940 | StartFinishSpan |
netcoreapp3.1 | 564ns | 0.211ns | 0.817ns | 0.00765 | 0 | 0 | 576 B |
#5940 | StartFinishSpan |
net472 | 644ns | 0.61ns | 2.36ns | 0.0916 | 0 | 0 | 578 B |
#5940 | StartFinishScope |
net6.0 | 513ns | 0.246ns | 0.922ns | 0.00967 | 0 | 0 | 696 B |
#5940 | StartFinishScope |
netcoreapp3.1 | 748ns | 0.259ns | 0.967ns | 0.00927 | 0 | 0 | 696 B |
#5940 | StartFinishScope |
net472 | 941ns | 0.575ns | 2.23ns | 0.105 | 0 | 0 | 658 B |
Benchmarks.Trace.TraceAnnotationsBenchmark - Faster 🎉 Same allocations ✔️
Faster 🎉 in #5940
Benchmark
base/diff
Base Median (ns)
Diff Median (ns)
Modality
Benchmarks.Trace.TraceAnnotationsBenchmark.RunOnMethodBegin‑net6.0
1.122
673.93
600.81
Benchmark | base/diff | Base Median (ns) | Diff Median (ns) | Modality |
---|---|---|---|---|
Benchmarks.Trace.TraceAnnotationsBenchmark.RunOnMethodBegin‑net6.0 | 1.122 | 673.93 | 600.81 |
Raw results
Branch | Method | Toolchain | Mean | StdError | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
master | RunOnMethodBegin |
net6.0 | 674ns | 0.341ns | 1.32ns | 0.00973 | 0 | 0 | 696 B |
master | RunOnMethodBegin |
netcoreapp3.1 | 958ns | 1.64ns | 6.13ns | 0.00948 | 0 | 0 | 696 B |
master | RunOnMethodBegin |
net472 | 1.14μs | 5.01ns | 19.4ns | 0.104 | 0 | 0 | 658 B |
#5940 | RunOnMethodBegin |
net6.0 | 601ns | 0.162ns | 0.627ns | 0.0096 | 0 | 0 | 696 B |
#5940 | RunOnMethodBegin |
netcoreapp3.1 | 941ns | 0.541ns | 2.03ns | 0.00926 | 0 | 0 | 696 B |
#5940 | RunOnMethodBegin |
net472 | 1.1μs | 0.392ns | 1.41ns | 0.104 | 0 | 0 | 658 B |
Summary of changes
Fix ToString and ToWString on large strings
Reason for change
Those two methods have a fast-path, where we try to fit the result in a pre-allocated buffer. But when the string is too long, we incorrectly assumed that
WideCharToMultiByte
/MultiByteToWideChar
would return the needed size. In truth, it only does so whencchWideChar
is 0. Because of that mistake, we were returning an empty string.Implementation details
Added a few minor changes while I was at it:
&array[0]
str.data()
instead of&str[0]
Test coverage
Added a unit test for long strings.
Other details
Backport of #5930