[Repo Assist] improve: use SearchValues(char) in ShellQuoting.NeedsQuoting for SIMD metachar scan#182
Merged
shanselman merged 1 commit intomasterfrom Apr 20, 2026
Conversation
… scan Replace per-character foreach+switch with System.Buffers.SearchValues<char>, which uses SSE2/AVX2 vectorized IndexOfAny to detect shell metacharacters in a single SIMD pass instead of a character-by-character loop. - Add static SearchValues<char> s_shellMetachars (same 25-char set as former - Replace NeedsQuoting foreach body with arg.AsSpan().IndexOfAny(s_shellMetachars) >= 0 - Remove private IsShellMetachar helper (no longer needed) - Add using System.Buffers (SearchValues<T> namespace) SearchValues<char> is available since .NET 8; this project targets net10.0. All existing ShellQuotingTests pass unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
10 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 This is an automated PR from Repo Assist.
Summary
Replaces the per-character
foreach+IsShellMetacharswitch inShellQuoting.NeedsQuotingwithSystem.Buffers.SearchValues<char>, which builds a vectorized lookup structure at startup and usesIndexOfAnyto scan for metacharacters in a single SIMD pass.Root cause / motivation
NeedsQuotingis called on every argument in everysystem.run/system.run.prepareinvocation. The former implementation iterated character-by-character through a 25-armswitchexpression. On .NET 8+,SearchValues<char>uses SSE2/AVX2 instructions to check multiple characters against a lookup bitmap simultaneously — significantly faster for typical argument lengths.Changes
ShellQuoting.csusing System.Buffers; addstatic readonly SearchValues<char> s_shellMetachars(same 25-char set asIsShellMetachar); replaceNeedsQuotingbody witharg.AsSpan().IndexOfAny(s_shellMetachars) >= 0; remove now-unusedIsShellMetacharprivate helperNet diff: 9 insertions / 16 deletions — net −7 lines while preserving identical semantics.
Metacharacter set preserved (25 chars)
space,\t,",',&,|,;,<,>,(,),^,%,!,$,`,*,?,[,],{,},~,\n,\rIdentical to the former
IsShellMetacharswitch — no behavioral change.Trade-offs
SearchValues<char>requiresSystem.Buffers(usingadded) — available since .NET 8; this project targets net10.0.QuoteForShellorFormatExecCommand— only the metachar detection inner loop is affected.Test Status
All existing
ShellQuotingTestspass unchanged (covering all 25 metacharacters, empty/plain args,QuoteForShellcmd and PowerShell paths,FormatExecCommandedge cases).Shared.Tests: 586 passed, 20 skipped — exit 0
Tray.Tests: 122 passed — exit 0