Skip to content

Improve some string Rune helpers#127939

Open
MihaZupan wants to merge 2 commits intodotnet:mainfrom
MihaZupan:string-rune
Open

Improve some string Rune helpers#127939
MihaZupan wants to merge 2 commits intodotnet:mainfrom
MihaZupan:string-rune

Conversation

@MihaZupan
Copy link
Copy Markdown
Member

@MihaZupan MihaZupan commented May 8, 2026

Method Toolchain Mean Error Ratio
ContainsBmp \main\corerun.exe 11.018 ns 0.0416 ns 1.00
ContainsBmp \pr\corerun.exe 5.335 ns 0.0090 ns 0.48
ContainsSupplementary \main\corerun.exe 14.186 ns 0.0586 ns 1.00
ContainsSupplementary \pr\corerun.exe 15.075 ns 0.0278 ns 1.06
StartsWithBmp \main\corerun.exe 5.637 ns 0.0063 ns 1.00
StartsWithBmp \pr\corerun.exe 2.193 ns 0.0021 ns 0.39
StartsWithSupplementary \main\corerun.exe 9.371 ns 0.0070 ns 1.00
StartsWithSupplementary \pr\corerun.exe 2.822 ns 0.0041 ns 0.30
EndsWithBmp \main\corerun.exe 5.641 ns 0.0076 ns 1.00
EndsWithBmp \pr\corerun.exe 2.505 ns 0.0022 ns 0.44
EndsWithSupplementary \main\corerun.exe 9.400 ns 0.0273 ns 1.00
EndsWithSupplementary \pr\corerun.exe 3.135 ns 0.0031 ns 0.33
ReplaceBothBmp \main\corerun.exe 59.146 ns 0.6353 ns 1.00
ReplaceBothBmp \pr\corerun.exe 29.462 ns 0.3781 ns 0.50
ReplaceSupplementary \main\corerun.exe 62.393 ns 0.5636 ns 1.00
ReplaceSupplementary \pr\corerun.exe 61.546 ns 0.5907 ns 0.99
SplitBmp \main\corerun.exe 51.507 ns 0.8753 ns 1.00
SplitBmp \pr\corerun.exe 51.466 ns 1.0191 ns 1.00
SplitSupplementary \main\corerun.exe 49.510 ns 0.9627 ns 1.00
SplitSupplementary \pr\corerun.exe 48.681 ns 0.4651 ns 0.98
TrimBmp \main\corerun.exe 35.152 ns 0.1661 ns 1.00
TrimBmp \pr\corerun.exe 14.858 ns 0.1258 ns 0.42
TrimSupplementary \main\corerun.exe 23.888 ns 0.1723 ns 1.00
TrimSupplementary \pr\corerun.exe 13.587 ns 0.1036 ns 0.57
TrimStartBmp \main\corerun.exe 17.435 ns 0.1468 ns 1.00
TrimStartBmp \pr\corerun.exe 12.658 ns 0.0918 ns 0.73
TrimStartSupplementary \main\corerun.exe 17.189 ns 0.1117 ns 1.00
TrimStartSupplementary \pr\corerun.exe 12.552 ns 0.0952 ns 0.73
TrimEndBmp \main\corerun.exe 24.816 ns 0.1445 ns 1.00
TrimEndBmp \pr\corerun.exe 11.842 ns 0.0948 ns 0.48
TrimEndSupplementary \main\corerun.exe 19.257 ns 0.1592 ns 1.00
TrimEndSupplementary \pr\corerun.exe 11.465 ns 0.1043 ns 0.60

@MihaZupan MihaZupan added this to the 11.0.0 milestone May 8, 2026
@MihaZupan MihaZupan self-assigned this May 8, 2026
Copilot AI review requested due to automatic review settings May 8, 2026 00:54
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

@MihaZupan
Copy link
Copy Markdown
Member Author

@EgorBot

using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
 
BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);

[MemoryDiagnoser]
public class Bench
{
   private string _testString = default!;
   private string _testStringSupplementary = default!;
   private Rune _bmpRune;
   private Rune _supplementaryRune;
   private Rune _bmpReplacementRune;
   private Rune _supplementaryReplacementRune;

   [GlobalSetup]
   public void Setup()
   {
       _bmpRune = new Rune('a');
       _supplementaryRune = new Rune(0x1F600); // 😀
       _bmpReplacementRune = new Rune('b');
       _supplementaryReplacementRune = new Rune(0x1F601); // 😁

       _testString = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxa";
       _testStringSupplementary = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\U0001F600";
   }

   [Benchmark]
   public bool ContainsBmp() => _testString.Contains(_bmpRune);

   [Benchmark]
   public bool ContainsSupplementary() => _testStringSupplementary.Contains(_supplementaryRune);

   [Benchmark]
   public bool StartsWithBmp() => _testString.StartsWith(_bmpRune);

   [Benchmark]
   public bool StartsWithSupplementary() => _testStringSupplementary.StartsWith(_supplementaryRune);

   [Benchmark]
   public bool EndsWithBmp() => _testString.EndsWith(_bmpRune);

   [Benchmark]
   public bool EndsWithSupplementary() => _testStringSupplementary.EndsWith(_supplementaryRune);

   [Benchmark]
   public string ReplaceBothBmp() => _testString.Replace(_bmpRune, _bmpReplacementRune);

   [Benchmark]
   public string ReplaceSupplementary() => _testStringSupplementary.Replace(_supplementaryRune, _supplementaryReplacementRune);

   [Benchmark]
   public string[] SplitBmp() => _testString.Split(_bmpRune);

   [Benchmark]
   public string[] SplitSupplementary() => _testStringSupplementary.Split(_supplementaryRune);

   [Benchmark]
   public string TrimBmp() => "aaahello worldaaa".Trim(_bmpRune);

   [Benchmark]
   public string TrimSupplementary() => "\U0001F600hello world\U0001F600".Trim(_supplementaryRune);

   [Benchmark]
   public string TrimStartBmp() => "aaahello world".TrimStart(_bmpRune);

   [Benchmark]
   public string TrimStartSupplementary() => "\U0001F600hello world".TrimStart(_supplementaryRune);

   [Benchmark]
   public string TrimEndBmp() => "hello worldaaa".TrimEnd(_bmpRune);

   [Benchmark]
   public string TrimEndSupplementary() => "hello world\U0001F600".TrimEnd(_supplementaryRune);
}

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates several string overloads that accept Rune to take faster paths for BMP runes and to simplify ordinal StartsWith / EndsWith checks.

Changes:

  • Special-cases BMP Rune inputs to delegate to existing char overloads for Contains, Replace, and Split.
  • Implements ordinal StartsWith(Rune) / EndsWith(Rune) via direct UTF-16 surrogate comparisons for non-BMP runes.
  • Refactors Trim(Rune) / TrimStart(Rune) / TrimEnd(Rune) to use TrimHelper with a pinned UTF-16 span.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/libraries/System.Private.CoreLib/src/System/String.Searching.cs Adds BMP fast-path for Contains(Rune) by forwarding to Contains(char).
src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs Adds BMP fast-paths for Replace / Split and refactors Trim* (Rune) implementations to call TrimHelper.
src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs Adds BMP fast-paths and direct surrogate-pair comparisons for ordinal StartsWith(Rune) / EndsWith(Rune).

Comment thread src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread src/libraries/System.Private.CoreLib/src/System/String.Searching.cs
Comment thread src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 8, 2026 16:12
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

return Contains((char)value.Value);
}

return Contains(value, StringComparison.Ordinal);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants