Skip to content

Conversation

@lucaspimentel
Copy link
Member

@lucaspimentel lucaspimentel commented Jan 23, 2026

Summary of changes

Simplified ProcessTags.GetLastPathSegment() to use DirectoryInfo.Name and fixed the edge case where root paths (e.g., /, C:\) would return an empty string.

Reason for change

  • Bug fix: The previous implementation returned an empty string for root paths like / or C:\ because Path.GetFileName() returns empty for paths ending with a separator, and root paths always logically end with a separator.

Implementation details

ProcessTags.cs:

  • Replaced manual TrimEnd + Path.GetFileName logic with new DirectoryInfo(path).Name, which correctly handles:
    • Trailing separators
    • Root paths (returns the root itself)
    • Both Windows and Unix path formats
  • Changed TagsList and SerializedTags to use field ??= pattern (Lazy<T> semantics) to delay initialization until first access
  • Inlined GetSerializedTagsFromList() since it was a one-liner

Test coverage

Added unit tests for GetLastPathSegment:

  • GetLastPathSegment_ReturnsLastDirectory_Windows - Tests Windows paths (rooted, unrooted, with/without trailing separators, root paths, empty, null)
  • GetLastPathSegment_ReturnsLastDirectory_NonWindows - Tests Unix paths (same scenarios)

Other details

n/a

@lucaspimentel lucaspimentel changed the title avoid some allocations in process tags Reduce allocations in process tags handling Jan 23, 2026
@lucaspimentel lucaspimentel force-pushed the lpimentel/reduce-allocations-in-process-tags branch from 97d3939 to 67c69a8 Compare January 23, 2026 21:02
@lucaspimentel lucaspimentel changed the title Reduce allocations in process tags handling Reduce allocations in ProcessTags.GetLastPathSegment() Jan 23, 2026
@lucaspimentel lucaspimentel changed the title Reduce allocations in ProcessTags.GetLastPathSegment() process tags: fix edge case and avoid some allocations Jan 23, 2026
@lucaspimentel lucaspimentel marked this pull request as ready for review January 23, 2026 21:09
@lucaspimentel lucaspimentel requested a review from a team as a code owner January 23, 2026 21:09
@lucaspimentel lucaspimentel force-pushed the lpimentel/reduce-allocations-in-process-tags branch from 67c69a8 to e520464 Compare January 23, 2026 21:10
@dd-trace-dotnet-ci-bot
Copy link

dd-trace-dotnet-ci-bot bot commented Jan 23, 2026

Execution-Time Benchmarks Report ⏱️

Execution-time results for samples comparing This PR (8103) 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
duration77.10 ± (77.04 - 77.43) ms68.28 ± (68.33 - 68.56) ms-11.4%
.NET Framework 4.8 - Bailout
duration82.39 ± (82.29 - 82.75) ms72.07 ± (72.03 - 72.25) ms-12.5%
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1109.07 ± (1109.12 - 1119.43) ms1020.29 ± (1022.87 - 1031.36) ms-8.0%
.NET Core 3.1 - Baseline
process.internal_duration_ms23.86 ± (23.81 - 23.91) ms21.85 ± (21.83 - 21.88) ms-8.4%
process.time_to_main_ms92.22 ± (92.01 - 92.43) ms78.75 ± (78.60 - 78.90) ms-14.6%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.88 ± (10.88 - 10.89) MB10.91 ± (10.90 - 10.91) MB+0.2%✅⬆️
runtime.dotnet.threads.count12 ± (12 - 12)12 ± (12 - 12)+0.0%
.NET Core 3.1 - Bailout
process.internal_duration_ms23.57 ± (23.50 - 23.64) ms21.74 ± (21.72 - 21.76) ms-7.8%
process.time_to_main_ms92.65 ± (92.43 - 92.87) ms80.00 ± (79.91 - 80.09) ms-13.7%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.91 ± (10.91 - 10.92) MB10.94 ± (10.94 - 10.95) MB+0.3%✅⬆️
runtime.dotnet.threads.count13 ± (13 - 13)13 ± (13 - 13)+0.0%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms251.69 ± (247.38 - 256.00) ms246.62 ± (243.35 - 249.89) ms-2.0%
process.time_to_main_ms523.67 ± (522.71 - 524.62) ms476.15 ± (475.67 - 476.63) ms-9.1%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed48.51 ± (48.48 - 48.53) MB48.61 ± (48.58 - 48.63) MB+0.2%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)+1.0%✅⬆️
.NET 6 - Baseline
process.internal_duration_ms22.51 ± (22.46 - 22.56) ms20.78 ± (20.75 - 20.81) ms-7.7%
process.time_to_main_ms80.37 ± (80.18 - 80.56) ms68.62 ± (68.51 - 68.73) ms-14.6%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.61 ± (10.61 - 10.61) MB10.63 ± (10.63 - 10.64) MB+0.2%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 6 - Bailout
process.internal_duration_ms22.34 ± (22.28 - 22.39) ms20.62 ± (20.59 - 20.64) ms-7.7%
process.time_to_main_ms80.88 ± (80.66 - 81.10) ms69.29 ± (69.22 - 69.36) ms-14.3%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.66 ± (10.66 - 10.66) MB10.77 ± (10.75 - 10.79) MB+1.0%✅⬆️
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms234.47 ± (230.39 - 238.55) ms250.67 ± (249.75 - 251.59) ms+6.9%✅⬆️
process.time_to_main_ms496.44 ± (495.71 - 497.16) ms452.74 ± (452.29 - 453.20) ms-8.8%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed49.18 ± (49.16 - 49.20) MB49.39 ± (49.36 - 49.42) MB+0.4%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)+0.0%
.NET 8 - Baseline
process.internal_duration_ms20.71 ± (20.66 - 20.76) ms18.81 ± (18.78 - 18.84) ms-9.2%
process.time_to_main_ms78.39 ± (78.21 - 78.56) ms67.33 ± (67.23 - 67.44) ms-14.1%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.67 ± (7.66 - 7.67) MB7.68 ± (7.67 - 7.68) MB+0.1%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 8 - Bailout
process.internal_duration_ms20.59 ± (20.54 - 20.64) ms18.79 ± (18.77 - 18.82) ms-8.7%
process.time_to_main_ms79.61 ± (79.44 - 79.79) ms68.59 ± (68.52 - 68.66) ms-13.8%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.72 ± (7.71 - 7.73) MB7.74 ± (7.73 - 7.75) MB+0.3%✅⬆️
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms199.68 ± (199.03 - 200.32) ms178.93 ± (177.84 - 180.03) ms-10.4%
process.time_to_main_ms479.07 ± (478.07 - 480.08) ms439.79 ± (438.88 - 440.71) ms-8.2%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed37.04 ± (37.00 - 37.07) MB36.68 ± (36.65 - 36.72) MB-1.0%
runtime.dotnet.threads.count27 ± (27 - 27)27 ± (27 - 27)-0.4%

HttpMessageHandler

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration196.01 ± (196.19 - 197.25) ms193.64 ± (193.62 - 194.42) ms-1.2%
.NET Framework 4.8 - Bailout
duration198.67 ± (198.72 - 199.71) ms197.69 ± (197.59 - 198.09) ms-0.5%
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1143.32 ± (1145.00 - 1153.39) ms1142.17 ± (1146.52 - 1154.77) ms-0.1%
.NET Core 3.1 - Baseline
process.internal_duration_ms189.17 ± (188.75 - 189.59) ms188.15 ± (187.82 - 188.48) ms-0.5%
process.time_to_main_ms81.80 ± (81.58 - 82.02) ms81.38 ± (81.21 - 81.56) ms-0.5%
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.04 ± (16.02 - 16.06) MB16.09 ± (16.06 - 16.12) MB+0.3%✅⬆️
runtime.dotnet.threads.count20 ± (19 - 20)20 ± (19 - 20)-0.1%
.NET Core 3.1 - Bailout
process.internal_duration_ms188.91 ± (188.44 - 189.39) ms188.47 ± (188.08 - 188.86) ms-0.2%
process.time_to_main_ms82.98 ± (82.75 - 83.21) ms83.28 ± (83.07 - 83.48) ms+0.4%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.10 ± (16.07 - 16.13) MB16.11 ± (16.08 - 16.14) MB+0.1%✅⬆️
runtime.dotnet.threads.count21 ± (21 - 21)21 ± (20 - 21)-0.3%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms429.26 ± (425.86 - 432.65) ms423.97 ± (420.49 - 427.46) ms-1.2%
process.time_to_main_ms485.08 ± (484.18 - 485.97) ms482.51 ± (481.84 - 483.18) ms-0.5%
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed59.13 ± (59.02 - 59.24) MB58.94 ± (58.81 - 59.07) MB-0.3%
runtime.dotnet.threads.count29 ± (29 - 30)29 ± (29 - 30)+0.0%✅⬆️
.NET 6 - Baseline
process.internal_duration_ms193.84 ± (193.43 - 194.25) ms192.95 ± (192.51 - 193.39) ms-0.5%
process.time_to_main_ms71.00 ± (70.81 - 71.20) ms70.54 ± (70.33 - 70.75) ms-0.7%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.14 ± (16.05 - 16.23) MB16.19 ± (16.07 - 16.31) MB+0.3%✅⬆️
runtime.dotnet.threads.count19 ± (18 - 19)19 ± (19 - 19)+1.1%✅⬆️
.NET 6 - Bailout
process.internal_duration_ms192.74 ± (192.37 - 193.11) ms191.39 ± (191.11 - 191.66) ms-0.7%
process.time_to_main_ms71.85 ± (71.68 - 72.03) ms71.47 ± (71.33 - 71.61) ms-0.5%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.29 ± (16.20 - 16.38) MB16.24 ± (16.12 - 16.36) MB-0.3%
runtime.dotnet.threads.count20 ± (20 - 20)19 ± (19 - 20)-2.7%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms444.80 ± (441.62 - 447.98) ms445.35 ± (442.14 - 448.55) ms+0.1%✅⬆️
process.time_to_main_ms459.90 ± (459.23 - 460.57) ms458.48 ± (457.88 - 459.09) ms-0.3%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed58.95 ± (58.83 - 59.08) MB59.03 ± (58.90 - 59.15) MB+0.1%✅⬆️
runtime.dotnet.threads.count29 ± (29 - 29)29 ± (29 - 29)-0.0%
.NET 8 - Baseline
process.internal_duration_ms191.16 ± (190.85 - 191.47) ms190.88 ± (190.48 - 191.28) ms-0.1%
process.time_to_main_ms70.83 ± (70.62 - 71.03) ms70.49 ± (70.33 - 70.66) ms-0.5%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.73 ± (11.70 - 11.76) MB11.74 ± (11.71 - 11.77) MB+0.1%✅⬆️
runtime.dotnet.threads.count18 ± (18 - 18)18 ± (18 - 18)-0.2%
.NET 8 - Bailout
process.internal_duration_ms191.32 ± (190.89 - 191.75) ms190.37 ± (189.99 - 190.75) ms-0.5%
process.time_to_main_ms71.67 ± (71.52 - 71.82) ms71.57 ± (71.41 - 71.73) ms-0.1%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.75 ± (11.73 - 11.77) MB11.79 ± (11.76 - 11.82) MB+0.3%✅⬆️
runtime.dotnet.threads.count19 ± (19 - 19)19 ± (19 - 19)+0.2%✅⬆️
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms369.54 ± (368.10 - 370.98) ms367.63 ± (366.26 - 369.00) ms-0.5%
process.time_to_main_ms443.60 ± (442.84 - 444.36) ms444.12 ± (443.40 - 444.84) ms+0.1%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed48.38 ± (48.34 - 48.41) MB48.37 ± (48.34 - 48.41) MB-0.0%
runtime.dotnet.threads.count28 ± (28 - 29)28 ± (28 - 29)-0.1%
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 (8103) - mean (68ms)  : 67, 70
    master - mean (77ms)  : 74, 80

    section Bailout
    This PR (8103) - mean (72ms)  : 71, 73
    master - mean (83ms)  : 80, 85

    section CallTarget+Inlining+NGEN
    This PR (8103) - mean (1,027ms)  : 966, 1088
    master - mean (1,114ms)  : 1035, 1193

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 (8103) - mean (106ms)  : 103, 108
    master - mean (123ms)  : 119, 128

    section Bailout
    This PR (8103) - mean (107ms)  : 106, 108
    master - mean (123ms)  : 121, 126

    section CallTarget+Inlining+NGEN
    This PR (8103) - mean (749ms)  : 703, 795
    master - mean (813ms)  : 743, 883

Loading
FakeDbCommand (.NET 6)
gantt
    title Execution time (ms) FakeDbCommand (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8103) - mean (94ms)  : 92, 96
    master - mean (110ms)  : 105, 114

    section Bailout
    This PR (8103) - mean (95ms)  : 94, 96
    master - mean (110ms)  : 107, 113

    section CallTarget+Inlining+NGEN
    This PR (8103) - mean (727ms)  : 706, 749
    master - mean (761ms)  : 692, 830

Loading
FakeDbCommand (.NET 8)
gantt
    title Execution time (ms) FakeDbCommand (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8103) - mean (92ms)  : 90, 94
    master - mean (107ms)  : 104, 110

    section Bailout
    This PR (8103) - mean (93ms)  : 92, 95
    master - mean (108ms)  : 105, 112

    section CallTarget+Inlining+NGEN
    This PR (8103) - mean (647ms)  : 630, 664
    master - mean (718ms)  : 689, 746

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 (8103) - mean (194ms)  : 190, 198
    master - mean (197ms)  : 189, 205

    section Bailout
    This PR (8103) - mean (198ms)  : 196, 200
    master - mean (199ms)  : 194, 204

    section CallTarget+Inlining+NGEN
    This PR (8103) - mean (1,151ms)  : 1089, 1212
    master - mean (1,149ms)  : 1090, 1209

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 (8103) - mean (278ms)  : 273, 283
    master - mean (279ms)  : 273, 285

    section Bailout
    This PR (8103) - mean (280ms)  : 275, 285
    master - mean (280ms)  : 274, 287

    section CallTarget+Inlining+NGEN
    This PR (8103) - mean (939ms)  : 885, 993
    master - mean (945ms)  : 896, 994

Loading
HttpMessageHandler (.NET 6)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8103) - mean (272ms)  : 266, 277
    master - mean (273ms)  : 265, 281

    section Bailout
    This PR (8103) - mean (271ms)  : 268, 274
    master - mean (273ms)  : 268, 278

    section CallTarget+Inlining+NGEN
    This PR (8103) - mean (928ms)  : 866, 990
    master - mean (932ms)  : 878, 987

Loading
HttpMessageHandler (.NET 8)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8103) - mean (271ms)  : 265, 277
    master - mean (272ms)  : 268, 276

    section Bailout
    This PR (8103) - mean (272ms)  : 267, 276
    master - mean (273ms)  : 266, 280

    section CallTarget+Inlining+NGEN
    This PR (8103) - mean (843ms)  : 822, 864
    master - mean (845ms)  : 823, 866

Loading

@lucaspimentel lucaspimentel force-pushed the lpimentel/reduce-allocations-in-process-tags branch from 9b7d9c9 to 70cdc46 Compare January 26, 2026 14:55
@pr-commenter
Copy link

pr-commenter bot commented Jan 26, 2026

Benchmarks

Benchmark execution time: 2026-01-30 00:23:32

Comparing candidate commit ce3b4d3 in PR branch lpimentel/reduce-allocations-in-process-tags with baseline commit 3d020db in branch master.

Found 10 performance improvements and 14 performance regressions! Performance is the same for 152 metrics, 16 unstable metrics.

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

  • 🟥 execution_time [+100.820ms; +100.995ms] or [+99.884%; +100.058%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorMoreComplexBody netcoreapp3.1

  • 🟥 execution_time [+10.588ms; +15.056ms] or [+5.343%; +7.598%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorSimpleBody netcoreapp3.1

  • 🟥 execution_time [+16.171ms; +21.880ms] or [+8.168%; +11.052%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeLegacyArgs net6.0

  • 🟩 execution_time [-21.910ms; -21.610ms] or [-10.750%; -10.603%]

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

  • 🟩 execution_time [-22.689ms; -21.869ms] or [-11.308%; -10.899%]

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

  • 🟥 execution_time [+13.070ms; +14.734ms] or [+8.444%; +9.519%]
  • 🟥 throughput [-136.109op/s; -119.768op/s] or [-8.777%; -7.723%]

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

  • 🟩 execution_time [-108.497µs; -103.223µs] or [-7.150%; -6.802%]
  • 🟩 throughput [+48.144op/s; +50.708op/s] or [+7.306%; +7.695%]

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

  • 🟩 execution_time [-911.372µs; -663.670µs] or [-33.069%; -24.082%]

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

  • 🟩 execution_time [-68.638µs; -54.922µs] or [-6.352%; -5.083%]
  • 🟩 throughput [+49.952op/s; +62.034op/s] or [+5.397%; +6.702%]

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

  • 🟥 execution_time [+94.850µs; +106.910µs] or [+5.073%; +5.718%]

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

  • 🟩 execution_time [-238.639µs; -156.521µs] or [-11.251%; -7.380%]
  • 🟩 throughput [+39.311op/s; +57.245op/s] or [+8.331%; +12.131%]

scenario:Benchmarks.Trace.DbCommandBenchmark.ExecuteNonQuery net472

  • 🟥 throughput [-31426.483op/s; -29930.062op/s] or [-8.342%; -7.944%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearchAsync net472

  • 🟥 throughput [-18220.324op/s; -15587.862op/s] or [-6.244%; -5.342%]

scenario:Benchmarks.Trace.GraphQLBenchmark.ExecuteAsync net6.0

  • 🟥 throughput [-47725.940op/s; -44761.559op/s] or [-8.499%; -7.971%]

scenario:Benchmarks.Trace.ILoggerBenchmark.EnrichedLog net6.0

  • 🟥 execution_time [+14.575ms; +18.083ms] or [+7.329%; +9.094%]

scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatBenchmark net6.0

  • 🟥 execution_time [+2.468µs; +6.615µs] or [+5.920%; +15.868%]
  • 🟥 throughput [-3025.599op/s; -1384.599op/s] or [-12.557%; -5.746%]

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

  • 🟥 throughput [-2555.630op/s; -1100.634op/s] or [-12.398%; -5.340%]

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

  • 🟩 execution_time [-27.051ms; -24.650ms] or [-13.509%; -12.310%]

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

  • 🟥 execution_time [+15.615ms; +16.631ms] or [+7.781%; +8.287%]

@lucaspimentel lucaspimentel force-pushed the lpimentel/reduce-allocations-in-process-tags branch from 70cdc46 to 7d10db6 Compare January 26, 2026 21:14
@lucaspimentel lucaspimentel force-pushed the lpimentel/reduce-allocations-in-process-tags branch from 7d10db6 to 6526688 Compare January 28, 2026 20:42
Copy link
Collaborator

@bouwkast bouwkast left a comment

Choose a reason for hiding this comment

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

Thanks 👍

return Path.GetFileName(directoryPath.TrimEnd('\\').TrimEnd('/'));
return StringUtil.IsNullOrEmpty(directoryPath) ?
string.Empty :
new DirectoryInfo(directoryPath).Name;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is creating a new DirectoryInfo the workaround to the TrimEnd bug?

Copy link
Member Author

@lucaspimentel lucaspimentel Jan 28, 2026

Choose a reason for hiding this comment

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

Not really. We could just use the TrimEnd() that uses string instead of ReadOnlySpan<char> and accept the extra allocations.

But I decided I was worrying to much about just a few allocations that only happen once. Handling paths is quite complicated and DirectoryInfo.Name handles things correctly:

  • / and \ separators
  • paths with or without trailing separators
  • root paths like / or C:\
  • other edge cases like device paths (\\?\.\ etc) and \\server\share UNC paths
  • probably other edge cases I haven't even thought of 😅

The drawback is that we have to allocate the DirectoryInfo object and its constructor can also allocate some strings internally. But since this code only runs once, I think it's not a terrible trade-off.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice sounds like a lot easier to handle with DirectoryInfo 👍

@lucaspimentel lucaspimentel changed the title process tags: fix edge case and avoid some allocations process tags: fix edge cases when getting directory name Jan 29, 2026
@lucaspimentel lucaspimentel changed the title process tags: fix edge cases when getting directory name Fix ProcessTags.GetLastPathSegment for root paths Jan 29, 2026
@lucaspimentel lucaspimentel changed the title Fix ProcessTags.GetLastPathSegment for root paths Fix ProcessTags.GetLastPathSegment for root paths and other edge cases Jan 29, 2026
@lucaspimentel lucaspimentel changed the title Fix ProcessTags.GetLastPathSegment for root paths and other edge cases Fix ProcessTags.GetLastPathSegment for some edge cases, add tests Jan 29, 2026
@lucaspimentel
Copy link
Member Author

I wanted to give some context on this PR since I did 180 from the original goal 😅

It all started when I saw the char[] heap allocations from TrimEnd(params char[]) and the potential intermediate trimmed strings. I thought these could be easily avoided. As I was refactoring that code, I noticed there were no unit tests, so I started adding some tests. That's when I found about some edge cases we were not handling well, so the code got more and more complicated to handle those cases. Then the tests started hanging and I found the bug in our vendored/decompiled version of the TrimEnd(ReadOnlySpan<char>).

After going down that rabbit whole for a while, I realized that handling paths is complex, and I was too focused on reducing just a few small allocations that only happen once. (Yes, every bit counts, but sometimes you have to make trade-offs.) I pivoted the PR to focus less on the allocations (the original goal) and focused instead on a more correct handling of paths by using DirectoryInfo 😛

Copy link
Collaborator

@bouwkast bouwkast left a comment

Choose a reason for hiding this comment

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

Thanks!

return Path.GetFileName(directoryPath.TrimEnd('\\').TrimEnd('/'));
return StringUtil.IsNullOrEmpty(directoryPath) ?
string.Empty :
new DirectoryInfo(directoryPath).Name;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice sounds like a lot easier to handle with DirectoryInfo 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants