Uh oh!
There was an error while loading. Please reload this page.
Fast-path in String.Trim - #84300
Conversation
| public unsafe string Trim(char trimChar) => TrimHelper(&trimChar, 1, TrimType.Both); | ||
| public unsafe string Trim(char trimChar) | ||
| { | ||
| if (Length == 0 || (_firstChar != trimChar && this[^1] != trimChar)) |
| { | ||
| return this; | ||
| } | ||
| return TrimHelper(&trimChar, 1, TrimType.Both); |
There was a problem hiding this comment.
Not for this PR, but now that we'll be invoking these helpers only when we know there is something to be trimmed, I wonder whether employing an IndexOfAnyExceptWhitespace would be more useful, i.e. whether there being any whitespace makes it more likely there's enough whitespace for vectorization to be a win.
ghost
commented
Apr 4, 2023
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsSimilar to #84210 but for String. I tried to use Span APIs here but there was a small overhead. Bonus point - "string literal".Trim() is a no-op now, e.g.: stringTrimLiteral()=>"Hello".Trim();; Method Program:TrimLiteral():System.String:this
48B9809520802B020000 mov rcx, 0x22B80209580 ;; 'string literal'
- BA03000000 mov edx, 3- FF251BFE0400 tail.jmp [System.String:TrimWhiteSpaceHelper(int):System.String:this]-; Total bytes of code: 21+; Total bytes of code: 11Since JIT is able to fold Benchmarks: [Benchmark][Arguments("Hello world")][Arguments("Hello world ")][MethodImpl(MethodImplOptions.NoInlining)]publicstringTrim(stringstr)=>str.Trim();[Benchmark]publicstringTrimLiteral()=>"Hello".Trim();
|
Similar to #84210 but for String. I tried to use Span APIs here but there was a small overhead.
Bonus point - "string literal".Trim() is a no-op now, e.g.:
Since JIT is able to fold
if (Length == 0 || (!char.IsWhiteSpace(_firstChar) && !char.IsWhiteSpace(this[^1])))for a constant string 🙂 e.g. constant folding for RVA[cns]Benchmarks: