Skip to content

Conversation

@TobiasMarklund
Copy link
Contributor

Profiling our db code, we saw a hotspot in the '''RemoveFilterDisabledConditionFromQuery''' method. There are a few optimizations able to be made here:

  • It should be safe to use the much more efficient OrdinalIgnoreCase string comparison since parameters are of well-known format with no unusual characters.
  • Doing IndexOf with type char is more efficient than using a string with one character.
  • Keep track of how far we have searched in the command so we don't have to seek the whole string on each iteration.
  • Use StringBuilder for greatly reduced memory allocations on larger command texts where many removes are performed. If using NetStandard it would be possible to work with Span instead for minimal memory usage, but since this is dual-targeting to .Net Framework I did not investigate it.

Benchmarks on EF-generated command strings from our product yields somewhere between 10-100x time improvement for this method over original code. Memory allocations may cause few KB more allocations on very small queries, but saves several megabytes on large queries with many replacements.

Can use OrdinalIgnoreCase since parameters are of well-known format with no unusual characters
CommandText was originally updated on each iteration, but optimization caused it only to update on complete success.
Now, if parse error occur after some successful replaces we bail out and set the CommandText as earlier.
@TobiasMarklund TobiasMarklund changed the title Optimizeremovefilter Optimize removal of filter disabled conditions Oct 24, 2019
@JonathanMagnan
Copy link
Member

Thank you @TobiasMarklund for your pull,

we will try to review it during the weekend and release it if accepted.

Best Regards,

Jonathan


Performance Libraries
context.BulkInsert(list, options => options.BatchSize = 1000);
Entity Framework ExtensionsEntity Framework ClassicBulk OperationsDapper Plus

Runtime Evaluation
Eval.Execute("x + y", new {x = 1, y = 2}); // return 3
C# Eval FunctionSQL Eval Function

@JonathanMagnan JonathanMagnan self-assigned this Oct 24, 2019
@JonathanMagnan JonathanMagnan merged commit 1d0103a into zzzprojects:master Nov 3, 2019
@JonathanMagnan
Copy link
Member

Hello @TobiasMarklund ,

Thank you for your contribution.

The v3.1.1 has been released. It includes your both pull requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants