Skip to content

Commit b37748f

Browse files
committed
Improve string manipulation performance
Specifying StringComparison.Ordinal where possible will make operations faster, especially for large queries, especially when running on Linux. The culture sesitive string operations are significantly slower on Linux compared to Windows (dotnet/runtime#37919).
1 parent b2b82e8 commit b37748f

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/EntityFramework.DynamicFilters.Shared/DynamicFilterExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ private static void RemoveFilterDisabledConditionFromQuery(DbCommand command, Db
940940
(paramIdx = command.CommandText.IndexOf(param.ParameterName + " IS NOT NULL", curIdx, StringComparison.OrdinalIgnoreCase)) != -1)
941941
{
942942
int startIdx = command.CommandText.LastIndexOf("or", paramIdx, StringComparison.OrdinalIgnoreCase);
943-
int endIdx = command.CommandText.IndexOf(')', paramIdx);
943+
int endIdx = command.CommandText.IndexOf(")", paramIdx, StringComparison.Ordinal);
944944

945945
if (endIdx == -1)
946946
{
@@ -955,7 +955,7 @@ private static void RemoveFilterDisabledConditionFromQuery(DbCommand command, Db
955955
// (note the extra ()'s which are not present in PostgreSQL).
956956
// So while we found the ending ")", we may or may not want to actually remove it.
957957
// Determine that by checking for the presence of an opening "(" in between the "or" and the parameter name
958-
var openingParenIndex = command.CommandText.IndexOf('(', startIdx);
958+
var openingParenIndex = command.CommandText.IndexOf("(", startIdx, StringComparison.Ordinal);
959959
if ((openingParenIndex < startIdx) || (openingParenIndex > paramIdx))
960960
endIdx--; // Do not have opening paren so do not remove the trailing ")"!
961961
}
@@ -996,7 +996,7 @@ private static void SetParameterList(IEnumerable paramValueCollection, DbParamet
996996
int prevStartIndex = 0;
997997
int paramStartIdx;
998998
bool isNotCondition = false;
999-
while ((paramStartIdx = command.CommandText.IndexOf(param.ParameterName, prevStartIndex)) != -1)
999+
while ((paramStartIdx = command.CommandText.IndexOf(param.ParameterName, prevStartIndex, StringComparison.OrdinalIgnoreCase)) != -1)
10001000
{
10011001
int startIdx = FindLastComparisonOperator(command.CommandText, paramStartIdx, out isNotCondition);
10021002
if (startIdx == -1)
@@ -1058,11 +1058,11 @@ private static int FindLastComparisonOperator(string commandText, int fromIdx, o
10581058
// possible for us to match on a condition that is earlier in the sql statement.
10591059

10601060
// MySql uses "!=" syntax. Not possible to find both.
1061-
int notEqualIdx = commandText.LastIndexOf("!=", fromIdx, 10);
1061+
int notEqualIdx = commandText.LastIndexOf("!=", fromIdx, 10, StringComparison.Ordinal);
10621062
if (notEqualIdx == -1)
1063-
notEqualIdx = commandText.LastIndexOf("<>", fromIdx, 10);
1063+
notEqualIdx = commandText.LastIndexOf("<>", fromIdx, 10, StringComparison.Ordinal);
10641064

1065-
int equalIdx = commandText.LastIndexOf("=", fromIdx, 10);
1065+
int equalIdx = commandText.LastIndexOf("=", fromIdx, 10, StringComparison.Ordinal);
10661066
if (equalIdx == notEqualIdx + 1) // Don't want to match on the "=" in "!="
10671067
equalIdx = -1;
10681068

0 commit comments

Comments
 (0)