-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Reduce allocations in Escape() and Unescape() #10041
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6cc75c8
4d7c316
ee3a005
68c349a
b409065
b4917fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -53,6 +53,10 @@ public sealed class WildcardPattern | |||
| // char that escapes special chars | ||||
|
iSazonov marked this conversation as resolved.
Outdated
|
||||
| private const char escapeChar = '`'; | ||||
|
|
||||
| // Threshold for stack allocation. | ||||
| // The size is less than MaxShortPath = 260. | ||||
| private const int StackAllocThreshold = 256; | ||||
|
daxian-dbw marked this conversation as resolved.
Outdated
|
||||
|
|
||||
| // we convert a wildcard pattern to a predicate | ||||
| private Predicate<string> _isMatch; | ||||
|
|
||||
|
|
@@ -203,15 +207,20 @@ internal static string Escape(string pattern, char[] charsNotToEscape) | |||
| { | ||||
| if (pattern == null) | ||||
| { | ||||
| throw PSTraceSource.NewArgumentNullException("pattern"); | ||||
| throw PSTraceSource.NewArgumentNullException(nameof(pattern)); | ||||
| } | ||||
|
|
||||
| if (charsNotToEscape == null) | ||||
| { | ||||
| throw PSTraceSource.NewArgumentNullException("charsNotToEscape"); | ||||
| throw PSTraceSource.NewArgumentNullException(nameof(charsNotToEscape)); | ||||
| } | ||||
|
|
||||
| if (pattern == string.Empty) | ||||
| { | ||||
| return pattern; | ||||
| } | ||||
|
|
||||
| char[] temp = new char[pattern.Length * 2 + 1]; | ||||
| Span<char> temp = pattern.Length < StackAllocThreshold ? stackalloc char[pattern.Length * 2 + 1] : new char[pattern.Length * 2 + 1]; | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit misleading.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps, but other options look even worse (I mean renaming and using 2 constants). On the other hand, in both cases we can consider this condition just as a boundary based on the length of the pattern. |
||||
| int tempIndex = 0; | ||||
|
|
||||
| for (int i = 0; i < pattern.Length; i++) | ||||
|
|
@@ -231,13 +240,13 @@ internal static string Escape(string pattern, char[] charsNotToEscape) | |||
|
|
||||
| string s = null; | ||||
|
|
||||
| if (tempIndex > 0) | ||||
| if (tempIndex == pattern.Length) | ||||
|
daxian-dbw marked this conversation as resolved.
Outdated
|
||||
| { | ||||
| s = new string(temp, 0, tempIndex); | ||||
| s = pattern; | ||||
| } | ||||
| else | ||||
| { | ||||
| s = string.Empty; | ||||
| s = new string(temp.Slice(0, tempIndex)); | ||||
| } | ||||
|
|
||||
| return s; | ||||
|
|
@@ -312,10 +321,16 @@ public static string Unescape(string pattern) | |||
| { | ||||
| if (pattern == null) | ||||
| { | ||||
| throw PSTraceSource.NewArgumentNullException("pattern"); | ||||
| throw PSTraceSource.NewArgumentNullException(nameof(pattern)); | ||||
| } | ||||
|
|
||||
| char[] temp = new char[pattern.Length]; | ||||
| if (pattern == string.Empty) | ||||
| { | ||||
| return pattern; | ||||
| } | ||||
|
|
||||
| Span<char> temp = pattern.Length < StackAllocThreshold ? stackalloc char[pattern.Length] : new char[pattern.Length]; | ||||
|
|
||||
| int tempIndex = 0; | ||||
| bool prevCharWasEscapeChar = false; | ||||
|
|
||||
|
|
@@ -361,13 +376,13 @@ public static string Unescape(string pattern) | |||
|
|
||||
| string s = null; | ||||
|
|
||||
| if (tempIndex > 0) | ||||
| if (tempIndex == pattern.Length) | ||||
|
daxian-dbw marked this conversation as resolved.
Outdated
|
||||
| { | ||||
| s = new string(temp, 0, tempIndex); | ||||
| s = pattern; | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have tests cases for regex escape?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only indirectly - the API is used in engine and involved in paths, completions and other tests. Update:
Perhaps MSFT team can port the Unit tests. |
||||
| } | ||||
| else | ||||
| { | ||||
| s = string.Empty; | ||||
| s = new string(temp.Slice(0, tempIndex)); | ||||
| } | ||||
|
|
||||
| return s; | ||||
|
|
||||
Uh oh!
There was an error while loading. Please reload this page.