EFCacheContains 1.0.40

dotnet add package EFCacheContains --version 1.0.40
                    
NuGet\Install-Package EFCacheContains -Version 1.0.40
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EFCacheContains" Version="1.0.40" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EFCacheContains" Version="1.0.40" />
                    
Directory.Packages.props
<PackageReference Include="EFCacheContains" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EFCacheContains --version 1.0.40
                    
#r "nuget: EFCacheContains, 1.0.40"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package EFCacheContains@1.0.40
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EFCacheContains&version=1.0.40
                    
Install as a Cake Addin
#tool nuget:?package=EFCacheContains&version=1.0.40
                    
Install as a Cake Tool

Entity Framework translates queries written in Linq to SQL, and this is an expensive compilation. To help with performance, Entity Framework caches its "query plans" after compiling them (not to be confused with the SQL database's query plan). However, there are several cases where EF does not perform caching. One is the case where the IEnumerable.Contains() method is invoked. This library solves the problem by intercepting the expression trees before they get to EF and rewriting the contains method so that it is cached -- at least for small lists. By default it will rewrite xs.Contains(x) as xs[0] == x || xs[1] == x || ... || xs[n] == x. However, it only performs this for lists up to a configurable length, by default 5. In principle, EF should create one query plan for each different array length. When no rewriting is done (i.e. for long lists), no query plan is cached.

Installation

This is distributed as a NuGet package on nuget.org:

Install-Package EFCacheContains -Version 1.0.40

Usage

using berkeleychurchill.CacheContains;

...

var myQuery = from r in myContext.Records.CacheContains()
              where myList.Contains(r.Id)
              select r;

Typically, myQuery will reference an expression tree that has a call to Contains in it. But, so long as myList contains 5 elements or fewer, the call to CacheContains() will rewrite the expression tree as a cascade of boolean checks. To change the cutoff size from 5 to another value, pass it as a parameter to CacheContains:


var myQuery = from r in myContext.Records.CacheContains(10)
              where myList.Contains(r.Id)
              select r;

One can also change the default maximum list size to rewrite the expression tree:

CacheContains.QueryableExtensions.DefaultMaxSize = 20;

Troubleshooting

It's very important that before you use this library you perform profiling to ensure that EF query plan caching is a bottleneck. However, it's usually not enough to just rewrite your queries to invoke CacheContains. There are a number of other reasons that EF might not cache your query. The only way to be sure that things are working properly is to verify that you're obtaining the expected performance speedup. It's good to profile again and compare to make sure there aren't any lingering queries which aren't being cached. You may find it helpful to log the database queries and make sure that they're properly parameterized.

If you find a situation where this library is not rewriting a query that has a Contains method call, please file a bug report. A reproducible test case is very helpful (and it's even better if you can reproduce the problem with just Linq but no Entity Framework).

Release Notes

1.0.40

  • Fixing CI so that packages aren't improperly marked as prereleases.

1.0.36

  • Fix for environments that do not allow optional parameters in expression trees.
  • New way to set default maximum list size.

1.0.31

  • Important bug fix for lists containing more than 2 items.
  • Performance improvements
  • Additional testing.

1.0.26

  • Initial release.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.40 1,571 4/9/2019
1.0.36-Release 502 3/13/2019
1.0.31-Release 503 3/13/2019
1.0.26 670 3/12/2019