From e38bd38ccc27ebb231d8bd9b6111ec481c76857a Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Tue, 5 Oct 2021 00:12:34 +0200 Subject: [PATCH] Improvements to context pooling docs (#3460) Closes #3445 Closes #3369 --- .../advanced-performance-topics.md | 38 +++++++--- .../core/providers/sql-server/columns.md | 3 + .../ef-core-6.0/breaking-changes.md | 2 +- samples/core/Benchmarks/Benchmarks.csproj | 6 +- samples/core/Benchmarks/ContextPooling.cs | 69 +++++++++++++++++++ samples/core/Performance/Performance.csproj | 8 +-- .../core/Performance/PooledBloggingContext.cs | 12 ++++ samples/core/Performance/Program.cs | 14 ++++ 8 files changed, 134 insertions(+), 18 deletions(-) create mode 100644 samples/core/Benchmarks/ContextPooling.cs create mode 100644 samples/core/Performance/PooledBloggingContext.cs diff --git a/entity-framework/core/performance/advanced-performance-topics.md b/entity-framework/core/performance/advanced-performance-topics.md index 5eeac61968..2e25bf94e6 100644 --- a/entity-framework/core/performance/advanced-performance-topics.md +++ b/entity-framework/core/performance/advanced-performance-topics.md @@ -10,35 +10,53 @@ uid: core/performance/advanced-performance-topics ## DbContext pooling -`AddDbContextPool` enables pooling of `DbContext` instances. Context pooling can increase throughput in high-scale scenarios such as web servers by reusing context instances, rather than creating new instances for each request. +A `DbContext` is generally a light object: creating and disposing one doesn't involve a database operation, and most applications can do so without any noticeable impact on performance. However, each `DbContext` does set up a various internal services and objects necessary for performing its duties, and the overhead of continuously doing so may be significant in high-performance scenarios. For these cases, EF Core can *pool* your `DbContext` instances: when you dispose your `DbContext`, EF Core resets its state and stores it in an internal pool; when a new instance is next requested, that pooled instance is returned instead of setting up a new one. `DbContext` pooling allows you to pay `DbContext` setup costs only once at program startup, rather than continuously. -The typical pattern in an ASP.NET Core app using EF Core involves registering a custom type into the [dependency injection](/aspnet/core/fundamentals/dependency-injection) container and obtaining instances of that type through constructor parameters in controllers or Razor Pages. Using constructor injection, a new context instance is created for each request. +Following are the benchmark results for fetching a single row from a SQL Server database running locally on the same machine, with and without `DbContext` pooling. As always, results will change with the number of rows, the latency to your database server and other factors. Importantly, this benchmarks single-threaded pooling performance, while a real-world contended scenario may have different results; benchmark on your platform before making any decisions. [The source code is available here](https://github.com/dotnet/EntityFramework.Docs/tree/main/samples/core/Benchmarks/ContextPooling.cs), feel free to use it as a basis for your own measurements. - enables a pool of reusable context instances. To use context pooling, use the `AddDbContextPool` method instead of `AddDbContext` during service registration: +| Method | NumBlogs | Mean | Error | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated | +|---------------------- |--------- |---------:|---------:|---------:|--------:|------:|------:|----------:| +| WithoutContextPooling | 1 | 701.6 us | 26.62 us | 78.48 us | 11.7188 | - | - | 50.38 KB | +| WithContextPooling | 1 | 350.1 us | 6.80 us | 14.64 us | 0.9766 | - | - | 4.63 KB | + +Note that `DbContext` pooling is orthogonal to database connection pooling, which is managed at a lower level in the database driver. + +### [With dependency injection](#tab/with-di) + +The typical pattern in an ASP.NET Core app using EF Core involves registering a custom type into the [dependency injection](/aspnet/core/fundamentals/dependency-injection) container via . Then, instances of that type are obtained through constructor parameters in controllers or Razor Pages. + +To enable `DbContext` pooling, simply replace `AddDbContext` with : ```csharp services.AddDbContextPool( options => options.UseSqlServer(connectionString)); ``` -When `AddDbContextPool` is used, at the time a context instance is requested, EF first checks if there is an instance available in the pool. Once the request processing finalizes, any state on the instance is reset and the instance is itself returned to the pool. +The `poolSize` parameter of sets the maximum number of instances retained by the pool (defaults to 1024 in EF Core 6.0, and to 128 in previous versions). Once `poolSize` is exceeded, new context instances are not cached and EF falls back to the non-pooling behavior of creating instances on demand. -This is conceptually similar to how connection pooling operates in ADO.NET providers and has the advantage of saving some of the cost of initialization of the context instance. +### [Without dependency injection](#tab/without-di) -The `poolSize` parameter of sets the maximum number of instances retained by the pool (defaults to 1024 in EF Core 6.0, and to 128 in previous versions). Once `poolSize` is exceeded, new context instances are not cached and EF falls back to the non-pooling behavior of creating instances on demand. +> [!NOTE] +> Pooling without dependency injection was introduced in EF Core 6.0. -### Limitations +To use `DbContext` pooling without dependency injection, initialize a `PooledDbContextFactory` and request context instances from it: + +[!code-csharp[Main](../../../samples/core/Performance/Program.cs#DbContextPoolingWithoutDI)] + +The `poolSize` parameter of the `PooledDbContextFactory` constructor sets the maximum number of instances retained by the pool (defaults to 1024 in EF Core 6.0, and to 128 in previous versions). Once `poolSize` is exceeded, new context instances are not cached and EF falls back to the non-pooling behavior of creating instances on demand. -Apps should be profiled and tested to show that context initialization is a significant cost. +*** + +### Limitations -`AddDbContextPool` has a few limitations on what can be done in the `OnConfiguring` method of the context. +`DbContext` pooling has a few limitations on what can be done in the `OnConfiguring` method of the context. > [!WARNING] > Avoid using context pooling in apps that maintain state. For example, private fields in the context that shouldn't be shared across requests. EF Core only resets the state that it is aware of before adding a context instance to the pool. Context pooling works by reusing the same context instance across requests. This means that it's effectively registered as a [Singleton](/aspnet/core/fundamentals/dependency-injection#service-lifetimes) in terms of the instance itself so that it's able to persist. -Context pooling is intended for scenarios where the context configuration, which includes services resolved, is fixed between requests. For cases where [Scoped](/aspnet/core/fundamentals/dependency-injection#service-lifetimes) services are required, or configuration needs to be changed, don't use pooling. The performance gain from pooling is usually negligible except in highly optimized scenarios. +Context pooling is intended for scenarios where the context configuration, which includes services resolved, is fixed between requests. For cases where [Scoped](/aspnet/core/fundamentals/dependency-injection#service-lifetimes) services are required, or configuration needs to be changed, don't use pooling. ## Query caching and parameterization diff --git a/entity-framework/core/providers/sql-server/columns.md b/entity-framework/core/providers/sql-server/columns.md index e6267332d1..c76ba0cebf 100644 --- a/entity-framework/core/providers/sql-server/columns.md +++ b/entity-framework/core/providers/sql-server/columns.md @@ -11,6 +11,9 @@ This page details column configuration options that are specific to the SQL Serv ## Sparse columns +> [!NOTE] +> Sparse column support was introduced in EF Core 6.0. + Sparse columns are ordinary columns that have an optimized storage for null values, reducing the space requirements for null values at the cost of more overhead to retrieve non-null values. As an example, consider a type hierarchy mapped via [the table-per-hierarchy (TPH) strategy](xref:core/modeling/inheritance#table-per-hierarchy-and-discriminator-configuration). In TPH, a single database table is used to hold all types in a hierarchy; this means that the table must contain columns for each and every property across the entire hierarchy, and for columns belonging to rare types, most rows will contain a null value for that column. In these cases, it may make sense to configure the column as *sparse*, in order to reduce the space requirements. The decision whether to make a column sparse must be made by the user, and depends on expectations for actual data in the table. diff --git a/entity-framework/core/what-is-new/ef-core-6.0/breaking-changes.md b/entity-framework/core/what-is-new/ef-core-6.0/breaking-changes.md index c71d37e71b..4b3bf78896 100644 --- a/entity-framework/core/what-is-new/ef-core-6.0/breaking-changes.md +++ b/entity-framework/core/what-is-new/ef-core-6.0/breaking-changes.md @@ -123,7 +123,7 @@ If your application expects joined entities to be returned in a particular order was originally made to implement mainly in order to allow direct enumeration on it via the `foreach` construct. Unfortunately, when a project also references [System.Linq.Async](https://www.nuget.org/packages/System.Linq.Async) in order to compose async LINQ operators client-side, this resulted in an ambiguous invocation error between the operators defined over `IQueryable` and those defined over `IAsyncEnumerable`. C# 9 added [extension `GetEnumerator` support for `foreach` loops](/dotnet/csharp/language-reference/proposals/csharp-9.0/extension-getenumerator), removing the original main reason to reference `IAsyncEnumerable`. -The vast majority of `DbSet` usages will continue to work as-is, since they either compose LINQ operators over `DbSet`, enumerate it directly, etc. The only usages broken are those which attempt to cast `DbSet` directly to `IAsyncEnumerable`. +The vast majority of `DbSet` usages will continue to work as-is, since they compose LINQ operators over `DbSet`, enumerate it, etc. The only usages broken are those which attempt to cast `DbSet` directly to `IAsyncEnumerable`. #### Mitigations diff --git a/samples/core/Benchmarks/Benchmarks.csproj b/samples/core/Benchmarks/Benchmarks.csproj index 9bb8a15b75..5eb3ccf91f 100644 --- a/samples/core/Benchmarks/Benchmarks.csproj +++ b/samples/core/Benchmarks/Benchmarks.csproj @@ -1,13 +1,13 @@  Exe - net5.0 + net6.0 true - - + + diff --git a/samples/core/Benchmarks/ContextPooling.cs b/samples/core/Benchmarks/ContextPooling.cs new file mode 100644 index 0000000000..6161ab8114 --- /dev/null +++ b/samples/core/Benchmarks/ContextPooling.cs @@ -0,0 +1,69 @@ +using System.Collections.Generic; +using System.Linq; +using BenchmarkDotNet.Attributes; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; + +namespace Benchmarks +{ + [MemoryDiagnoser] + public class ContextPooling + { + private DbContextOptions _options; + private PooledDbContextFactory _poolingFactory; + + [Params(1)] + public int NumBlogs { get; set; } + + [GlobalSetup] + public void Setup() + { + _options = new DbContextOptionsBuilder() + .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True") + .Options; + + using var context = new BloggingContext(_options); + context.Database.EnsureDeleted(); + context.Database.EnsureCreated(); + context.SeedData(NumBlogs); + + _poolingFactory = new PooledDbContextFactory(_options); + } + + [Benchmark] + public List WithoutContextPooling() + { + using var context = new BloggingContext(_options); + + return context.Blogs.ToList(); + } + + [Benchmark] + public List WithContextPooling() + { + using var context = _poolingFactory.CreateDbContext(); + + return context.Blogs.ToList(); + } + + public class BloggingContext : DbContext + { + public DbSet Blogs { get; set; } + + public BloggingContext(DbContextOptions options) : base(options) {} + + public void SeedData(int numBlogs) + { + Blogs.AddRange(Enumerable.Range(0, numBlogs).Select(i => new Blog { Url = $"http://www.someblog{i}.com"})); + SaveChanges(); + } + } + + public class Blog + { + public int BlogId { get; set; } + public string Url { get; set; } + public int Rating { get; set; } + } + } +} diff --git a/samples/core/Performance/Performance.csproj b/samples/core/Performance/Performance.csproj index 625f6d2b33..d0f0df4377 100644 --- a/samples/core/Performance/Performance.csproj +++ b/samples/core/Performance/Performance.csproj @@ -2,13 +2,13 @@ Exe - net5.0 + net6.0 - - - + + + diff --git a/samples/core/Performance/PooledBloggingContext.cs b/samples/core/Performance/PooledBloggingContext.cs new file mode 100644 index 0000000000..9959f04a6b --- /dev/null +++ b/samples/core/Performance/PooledBloggingContext.cs @@ -0,0 +1,12 @@ +using Microsoft.EntityFrameworkCore; + +namespace Performance +{ + public class PooledBloggingContext : DbContext + { + public PooledBloggingContext(DbContextOptions options) : base(options) {} + + public DbSet Blogs { get; set; } + public DbSet Posts { get; set; } + } +} diff --git a/samples/core/Performance/Program.cs b/samples/core/Performance/Program.cs index d81550419f..cc525583ea 100644 --- a/samples/core/Performance/Program.cs +++ b/samples/core/Performance/Program.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; using Performance.LazyLoading; namespace Performance @@ -203,6 +204,19 @@ private static void Main(string[] args) context.Database.ExecuteSqlRaw("UPDATE [Employees] SET [Salary] = [Salary] + 1000"); #endregion } + + #region DbContextPoolingWithoutDI + var options = new DbContextOptionsBuilder() + .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True") + .Options; + + var factory = new PooledDbContextFactory(options); + + using (var context = factory.CreateDbContext()) + { + var allPosts = context.Posts.ToList(); + } + #endregion } } }