Skip to content

Fix ToString and ToWString on large strings#5930

Merged
kevingosse merged 2 commits into
masterfrom
kevin/string_conversion
Aug 23, 2024
Merged

Fix ToString and ToWString on large strings#5930
kevingosse merged 2 commits into
masterfrom
kevin/string_conversion

Conversation

@kevingosse

Copy link
Copy Markdown
Contributor

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. 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.

@kevingosse
kevingosse requested a review from a team as a code owner August 22, 2024 15:16
@andrewlock

andrewlock commented Aug 22, 2024

Copy link
Copy Markdown
Member

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:

  • 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).

@datadog-ddstaging

datadog-ddstaging Bot commented Aug 22, 2024

Copy link
Copy Markdown

Datadog Report

Branch report: kevin/string_conversion
Commit report: 4342ed5
Test service: dd-trace-dotnet

✅ 0 Failed, 300432 Passed, 1852 Skipped, 14h 0m 9.27s Total Time

@andrewlock andrewlock added the area:native-library Automatic instrumentation native C++ code (Datadog.Trace.ClrProfiler.Native) label Aug 22, 2024
@andrewlock

andrewlock commented Aug 22, 2024

Copy link
Copy Markdown
Member

Benchmarks Report for tracer 🐌

Benchmarks for #5930 compared to master:

  • 4 benchmarks are faster, with geometric mean 1.144
  • 1 benchmarks have more allocations

The following thresholds were used for comparing the benchmark speeds:

  • Mann–Whitney U test with statistical test for significance of 5%
  • Only results indicating a difference greater than 10% and 0.3 ns are considered.

Allocation changes below 0.5% are ignored.

Benchmark details

Benchmarks.Trace.ActivityBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master StartStopWithChild net6.0 7.73μs 40.4ns 274ns 0.0123 0.0041 0 5.43 KB
master StartStopWithChild netcoreapp3.1 10.3μs 56.8ns 321ns 0.0192 0.00961 0 5.62 KB
master StartStopWithChild net472 16μs 47.4ns 184ns 1.02 0.308 0.0915 6.06 KB
#5930 StartStopWithChild net6.0 7.75μs 42.6ns 241ns 0.0114 0.00379 0 5.43 KB
#5930 StartStopWithChild netcoreapp3.1 9.93μs 54.2ns 311ns 0.0244 0.00978 0 5.62 KB
#5930 StartStopWithChild net472 16μs 24.3ns 90.8ns 1.04 0.34 0.0871 6.07 KB
Benchmarks.Trace.AgentWriterBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master WriteAndFlushEnrichedTraces net6.0 455μs 118ns 441ns 0 0 0 2.7 KB
master WriteAndFlushEnrichedTraces netcoreapp3.1 631μs 218ns 846ns 0 0 0 2.7 KB
master WriteAndFlushEnrichedTraces net472 833μs 711ns 2.75μs 0.417 0 0 3.3 KB
#5930 WriteAndFlushEnrichedTraces net6.0 461μs 310ns 1.16μs 0 0 0 2.7 KB
#5930 WriteAndFlushEnrichedTraces netcoreapp3.1 630μs 455ns 1.7μs 0 0 0 2.7 KB
#5930 WriteAndFlushEnrichedTraces net472 836μs 562ns 2.1μs 0.417 0 0 3.3 KB
Benchmarks.Trace.AspNetCoreBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master SendRequest net6.0 191μs 1.03μs 6.12μs 0.197 0 0 18.45 KB
master SendRequest netcoreapp3.1 217μs 1.21μs 9.06μs 0.208 0 0 20.61 KB
master SendRequest net472 0.00202ns 0.000754ns 0.00292ns 0 0 0 0 b
#5930 SendRequest net6.0 188μs 1.02μs 5.49μs 0.202 0 0 18.45 KB
#5930 SendRequest netcoreapp3.1 209μs 1.19μs 9.17μs 0.146 0 0 20.61 KB
#5930 SendRequest net472 0.000576ns 0.000308ns 0.00119ns 0 0 0 0 b
Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark - Same speed ✔️ More allocations ⚠️

More allocations ⚠️ in #5930

Benchmark Base Allocated Diff Allocated Change Change %
Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces‑netcoreapp3.1 41.75 KB 42.07 KB 325 B 0.78%

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master WriteAndFlushEnrichedTraces net6.0 571μs 2.51μs 12μs 0.551 0 0 41.69 KB
master WriteAndFlushEnrichedTraces netcoreapp3.1 682μs 3.72μs 20.7μs 0.327 0 0 41.75 KB
master WriteAndFlushEnrichedTraces net472 864μs 3.92μs 15.2μs 8.19 2.59 0.431 53.26 KB
#5930 WriteAndFlushEnrichedTraces net6.0 569μs 3.01μs 14.7μs 0.584 0 0 41.71 KB
#5930 WriteAndFlushEnrichedTraces netcoreapp3.1 684μs 3.82μs 23.9μs 0.324 0 0 42.07 KB
#5930 WriteAndFlushEnrichedTraces net472 866μs 2.63μs 10.2μs 8.68 2.6 0.434 53.26 KB
Benchmarks.Trace.DbCommandBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master ExecuteNonQuery net6.0 1.24μs 0.957ns 3.71ns 0.0142 0 0 1.02 KB
master ExecuteNonQuery netcoreapp3.1 1.65μs 0.997ns 3.86ns 0.0138 0 0 1.02 KB
master ExecuteNonQuery net472 2μs 2.16ns 8.38ns 0.156 0 0 987 B
#5930 ExecuteNonQuery net6.0 1.22μs 0.9ns 3.49ns 0.014 0 0 1.02 KB
#5930 ExecuteNonQuery netcoreapp3.1 1.73μs 0.73ns 2.53ns 0.0131 0 0 1.02 KB
#5930 ExecuteNonQuery net472 1.92μs 1.34ns 5.01ns 0.156 0 0 987 B
Benchmarks.Trace.ElasticsearchBenchmark - Faster 🎉 Same allocations ✔️

Faster 🎉 in #5930

Benchmark base/diff Base Median (ns) Diff Median (ns) Modality
Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearchAsync‑net6.0 1.173 1,421.00 1,211.46

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master CallElasticsearch net6.0 1.21μs 0.824ns 3.19ns 0.0133 0 0 976 B
master CallElasticsearch netcoreapp3.1 1.5μs 0.716ns 2.68ns 0.0127 0 0 976 B
master CallElasticsearch net472 2.42μs 1.1ns 4.24ns 0.158 0 0 995 B
master CallElasticsearchAsync net6.0 1.42μs 0.934ns 3.62ns 0.0136 0 0 952 B
master CallElasticsearchAsync netcoreapp3.1 1.69μs 0.684ns 2.56ns 0.0134 0 0 1.02 KB
master CallElasticsearchAsync net472 2.57μs 1.83ns 6.84ns 0.167 0 0 1.05 KB
#5930 CallElasticsearch net6.0 1.15μs 0.83ns 3.1ns 0.0133 0 0 976 B
#5930 CallElasticsearch netcoreapp3.1 1.58μs 0.702ns 2.53ns 0.0128 0 0 976 B
#5930 CallElasticsearch net472 2.37μs 1.02ns 3.67ns 0.157 0.00119 0 995 B
#5930 CallElasticsearchAsync net6.0 1.21μs 0.497ns 1.72ns 0.0134 0 0 952 B
#5930 CallElasticsearchAsync netcoreapp3.1 1.66μs 1.13ns 4.08ns 0.0134 0 0 1.02 KB
#5930 CallElasticsearchAsync net472 2.55μs 1.53ns 5.91ns 0.166 0 0 1.05 KB
Benchmarks.Trace.GraphQLBenchmark - Faster 🎉 Same allocations ✔️

Faster 🎉 in #5930

Benchmark base/diff Base Median (ns) Diff Median (ns) Modality
Benchmarks.Trace.GraphQLBenchmark.ExecuteAsync‑netcoreapp3.1 1.122 1,710.79 1,525.42

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master ExecuteAsync net6.0 1.32μs 0.442ns 1.59ns 0.0133 0 0 952 B
master ExecuteAsync netcoreapp3.1 1.71μs 1.52ns 5.89ns 0.0128 0 0 952 B
master ExecuteAsync net472 1.8μs 0.931ns 3.48ns 0.145 0 0 915 B
#5930 ExecuteAsync net6.0 1.43μs 0.897ns 3.36ns 0.0136 0 0 952 B
#5930 ExecuteAsync netcoreapp3.1 1.53μs 0.801ns 3ns 0.0123 0 0 952 B
#5930 ExecuteAsync net472 1.8μs 0.914ns 3.54ns 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.2μs 2.11ns 8.15ns 0.0296 0 0 2.22 KB
master SendAsync netcoreapp3.1 5.1μs 2.12ns 7.93ns 0.0356 0 0 2.76 KB
master SendAsync net472 7.86μs 2.11ns 8.18ns 0.497 0 0 3.15 KB
#5930 SendAsync net6.0 4.32μs 1.42ns 5.3ns 0.0303 0 0 2.22 KB
#5930 SendAsync netcoreapp3.1 5.18μs 1.5ns 5.82ns 0.0363 0 0 2.76 KB
#5930 SendAsync net472 7.77μs 2.43ns 9.08ns 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.63μs 0.621ns 2.32ns 0.0228 0 0 1.64 KB
master EnrichedLog netcoreapp3.1 2.26μs 0.504ns 1.82ns 0.0226 0 0 1.64 KB
master EnrichedLog net472 2.58μs 1.88ns 7.03ns 0.249 0 0 1.57 KB
#5930 EnrichedLog net6.0 1.47μs 0.567ns 2.12ns 0.0228 0 0 1.64 KB
#5930 EnrichedLog netcoreapp3.1 2.42μs 1.34ns 5ns 0.0217 0 0 1.64 KB
#5930 EnrichedLog net472 2.66μs 1.31ns 5.09ns 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 113μs 113ns 424ns 0.0569 0 0 4.28 KB
master EnrichedLog netcoreapp3.1 120μs 295ns 1.14μs 0.0594 0 0 4.28 KB
master EnrichedLog net472 148μs 94.7ns 354ns 0.672 0.224 0 4.46 KB
#5930 EnrichedLog net6.0 117μs 194ns 752ns 0.0592 0 0 4.28 KB
#5930 EnrichedLog netcoreapp3.1 120μs 249ns 964ns 0 0 0 4.28 KB
#5930 EnrichedLog net472 147μs 289ns 1.12μs 0.658 0.219 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 3.11μs 0.914ns 3.3ns 0.0312 0 0 2.2 KB
master EnrichedLog netcoreapp3.1 4.25μs 0.892ns 3.34ns 0.0297 0 0 2.2 KB
master EnrichedLog net472 4.9μs 1.79ns 6.69ns 0.32 0 0 2.02 KB
#5930 EnrichedLog net6.0 3.13μs 1.08ns 4.2ns 0.0298 0 0 2.2 KB
#5930 EnrichedLog netcoreapp3.1 4.23μs 1.91ns 7.38ns 0.0295 0 0 2.2 KB
#5930 EnrichedLog net472 4.94μs 2.1ns 8.13ns 0.32 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.38μs 0.839ns 3.25ns 0.0159 0 0 1.14 KB
master SendReceive netcoreapp3.1 1.72μs 0.789ns 2.95ns 0.0155 0 0 1.14 KB
master SendReceive net472 2.08μs 1.85ns 6.92ns 0.183 0 0 1.16 KB
#5930 SendReceive net6.0 1.35μs 0.673ns 2.61ns 0.0162 0 0 1.14 KB
#5930 SendReceive netcoreapp3.1 1.73μs 0.619ns 2.23ns 0.0148 0 0 1.14 KB
#5930 SendReceive net472 2.27μs 2.02ns 7.84ns 0.183 0.00111 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.86μs 0.894ns 3.46ns 0.0215 0 0 1.6 KB
master EnrichedLog netcoreapp3.1 3.89μs 1.7ns 6.58ns 0.0214 0 0 1.65 KB
master EnrichedLog net472 4.4μs 1.83ns 7.1ns 0.323 0 0 2.04 KB
#5930 EnrichedLog net6.0 2.79μs 0.964ns 3.74ns 0.0223 0 0 1.6 KB
#5930 EnrichedLog netcoreapp3.1 3.92μs 4.14ns 16ns 0.0212 0 0 1.65 KB
#5930 EnrichedLog net472 4.33μs 1.42ns 5.31ns 0.322 0 0 2.04 KB
Benchmarks.Trace.SpanBenchmark - Faster 🎉 Same allocations ✔️

Faster 🎉 in #5930

Benchmark base/diff Base Median (ns) Diff Median (ns) Modality
Benchmarks.Trace.SpanBenchmark.StartFinishSpan‑net472 1.167 650.93 557.73
Benchmarks.Trace.SpanBenchmark.StartFinishScope‑netcoreapp3.1 1.115 820.55 736.11

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master StartFinishSpan net6.0 403ns 0.297ns 1.15ns 0.00808 0 0 576 B
master StartFinishSpan netcoreapp3.1 587ns 1.77ns 6.85ns 0.00761 0 0 576 B
master StartFinishSpan net472 650ns 0.664ns 2.57ns 0.0917 0 0 578 B
master StartFinishScope net6.0 544ns 0.352ns 1.36ns 0.00978 0 0 696 B
master StartFinishScope netcoreapp3.1 820ns 0.41ns 1.53ns 0.00942 0 0 696 B
master StartFinishScope net472 842ns 0.458ns 1.65ns 0.104 0 0 658 B
#5930 StartFinishSpan net6.0 393ns 0.123ns 0.475ns 0.00813 0 0 576 B
#5930 StartFinishSpan netcoreapp3.1 563ns 0.361ns 1.4ns 0.00782 0 0 576 B
#5930 StartFinishSpan net472 558ns 0.547ns 2.12ns 0.0916 0 0 578 B
#5930 StartFinishScope net6.0 571ns 0.407ns 1.58ns 0.00971 0 0 696 B
#5930 StartFinishScope netcoreapp3.1 737ns 0.571ns 2.21ns 0.0096 0 0 696 B
#5930 StartFinishScope net472 811ns 1.07ns 4.16ns 0.104 0 0 658 B
Benchmarks.Trace.TraceAnnotationsBenchmark - Same speed ✔️ Same allocations ✔️

Raw results

Branch Method Toolchain Mean StdError StdDev Gen 0 Gen 1 Gen 2 Allocated
master RunOnMethodBegin net6.0 662ns 0.521ns 2.02ns 0.00956 0 0 696 B
master RunOnMethodBegin netcoreapp3.1 948ns 0.482ns 1.87ns 0.00946 0 0 696 B
master RunOnMethodBegin net472 1.17μs 0.853ns 3.3ns 0.104 0 0 658 B
#5930 RunOnMethodBegin net6.0 689ns 0.383ns 1.48ns 0.00969 0 0 696 B
#5930 RunOnMethodBegin netcoreapp3.1 988ns 0.842ns 3.26ns 0.00941 0 0 696 B
#5930 RunOnMethodBegin net472 1.2μs 1.11ns 4.3ns 0.104 0 0 658 B

@gleocadie gleocadie left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

👍

@kevingosse
kevingosse merged commit e0d9add into master Aug 23, 2024
@kevingosse
kevingosse deleted the kevin/string_conversion branch August 23, 2024 15:15
@github-actions github-actions Bot added this to the vNext-v3 milestone Aug 23, 2024
andrewlock pushed a commit that referenced this pull request Aug 23, 2024
## 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.
andrewlock added a commit that referenced this pull request Aug 23, 2024
## 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.


## Other details
Backport of #5930

Co-authored-by: Kevin Gosse <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:native-library Automatic instrumentation native C++ code (Datadog.Trace.ClrProfiler.Native) area:shared-components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants