Skip to content

Add fast-path optimizations for Char.IsDigit in performance-critical paths #12030

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

Merged
merged 7 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Build.UnitTests/Evaluation/Expander_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4741,6 +4741,31 @@ public void PropertyFunctionStringArrayGetValue()
TestPropertyFunction("$(X.Split($([System.Convert]::ToString(`.`).ToCharArray())).GetValue($([System.Convert]::ToInt32(0))))", "X", "ab.cd", "ab");
}

/// <summary>
/// Test that Char.IsDigit fast-path works correctly
/// </summary>
[Theory]
// Test with digit characters - single char version
[InlineData("$([System.Char]::IsDigit('0'))", "True")]
[InlineData("$([System.Char]::IsDigit('5'))", "True")]
[InlineData("$([System.Char]::IsDigit('9'))", "True")]
// Test with non-digit characters - single char version
[InlineData("$([System.Char]::IsDigit('a'))", "False")]
[InlineData("$([System.Char]::IsDigit(' '))", "False")]
[InlineData("$([System.Char]::IsDigit('/'))", "False")]
[InlineData("$([System.Char]::IsDigit(':'))", "False")]
// Test with string and index version
[InlineData("$([System.Char]::IsDigit('abc123def', 3))", "True")]
[InlineData("$([System.Char]::IsDigit('abc123def', 4))", "True")]
[InlineData("$([System.Char]::IsDigit('abc123def', 5))", "True")]
[InlineData("$([System.Char]::IsDigit('abc123def', 0))", "False")]
[InlineData("$([System.Char]::IsDigit('abc123def', 2))", "False")]
[InlineData("$([System.Char]::IsDigit('hello789', 5))", "True")]
public void PropertyFunctionCharIsDigit(string expression, string expected)
{
TestPropertyFunction(expression, "dummy", "", expected);
}

private void TestPropertyFunction(string expression, string propertyName, string propertyValue, string expected)
{
var properties = new PropertyDictionary<ProjectPropertyInstance>();
Expand Down
23 changes: 23 additions & 0 deletions src/Build/Evaluation/Expander/WellKnownFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,29 @@ internal static bool TryExecuteWellKnownFunction(string methodName, Type receive
}
}
}
else if (receiverType == typeof(char))
{
if (string.Equals(methodName, nameof(char.IsDigit), StringComparison.OrdinalIgnoreCase))
{
bool? result = null;

if (ParseArgs.TryGetArg(args, out string? arg0) && arg0?.Length == 1)
{
char c = arg0[0];
result = char.IsDigit(c);
}
else if (ParseArgs.TryGetArgs(args, out string? str, out int index) && str != null)
{
result = char.IsDigit(str, index);
}

if (result.HasValue)
{
returnVal = result.Value;
return true;
}
}
}
else if (string.Equals(methodName, nameof(Regex.Replace), StringComparison.OrdinalIgnoreCase) && args.Length == 3)
{
if (ParseArgs.TryGetArgs(args, out string? arg1, out string? arg2, out string? arg3) && arg1 != null && arg2 != null && arg3 != null)
Expand Down