Skip to content

Is EF9 slower than EF8? #35053

@wallyrion

Description

@wallyrion

I observed that EF 9 is running noticeably slower and is allocating twice as much memory compared to its performance with EF 8. I'm trying to determine whether this performance degradation is an isolated exception or if there might be an issue with my benchmarking setup. Any insights would be appreciated

There are some benchmarks with BenchmarkDotnet

Method Job Runtime UseSplitQuery Mean Error StdDev Ratio RatioSD Allocated Alloc Ratio
GetCustomerById .NET 8.0 .NET 8.0 False 1.688 ms 0.0368 ms 0.1038 ms 1.00 0.09 53.82 KB 1.00
GetCustomerById .NET 9.0 .NET 9.0 False 2.516 ms 0.0474 ms 0.1059 ms 1.50 0.11 196.56 KB 3.65
GetCustomerById .NET 8.0 .NET 8.0 True 2.867 ms 0.0598 ms 0.1705 ms 1.00 0.08 79.58 KB 1.00
GetCustomerById .NET 9.0 .NET 9.0 True 3.114 ms 0.0732 ms 0.2136 ms 1.09 0.10 161.05 KB 2.02

Comparing the load testing metrics with k6. Performance degradation is also noticeable.

Metric .NET 8 .NET 9
Total Requests 1,690,574 1,287,101
Requests per second 1,741.02 1,325.61
Data Received 13 GB 9.5 GB
Data Sent 220 MB 164 MB
HTTP Request Duration avg=2.29s, p(90)=4.79s, p(95)=5.51s avg=3.32s, p(90)=6.73s, p(95)=8.06s
Request Blocking avg=6.02µs, p(90)=5.01µs, p(95)=5.81µs avg=6.65µs, p(90)=5.15µs, p(95)=6.02µs
Request Receiving avg=44.14µs, p(90)=61.38µs, p(95)=75.48µs avg=42.62µs, p(90)=62.5µs, p(95)=76.01µs
Request Sending avg=14.77µs, p(90)=16.12µs, p(95)=32.88µs avg=13.8µs, p(90)=15.95µs, p(95)=29.79µs
Iteration Duration avg=3.29s, p(90)=5.79s, p(95)=6.52s avg=4.32s, p(90)=7.73s, p(95)=9.06s
Virtual Users (VUs) max=10,000 max=10,000

Query for benchmarks.

 var result =
            await customerQuery.Select(customer => new CustomerDto
                {
                    Id = customer.Id,
                    Name = customer.Name,
                    Orders = customer.Orders.Select(order => new OrderDto
                    {
                        OrderId = order.Id,
                        OrderDate = order.OrderDate,
                        OrderItems = order.OrderItems.Select(oi => new OrderItemDto
                        {
                            OrderItemId = oi.Id,
                            ProductName = oi.ProductName,
                            Quantity = oi.Quantity,
                            Price = oi.Price
                        }).ToList()
                    }).ToList()
                })
                .FirstOrDefaultAsync(c => c.Id == customerId);

Benchmarking ToListAsync() (retrieving 10000 items into memory)

We can observe 7x performance degradation for both speed and memory allocation between EF8 and EF9

Method Job Runtime Mean Error StdDev Ratio RatioSD Allocated Alloc Ratio
GetALlCustomers .NET 8.0 .NET 8.0 1.788 s 0.0194 s 0.0172 s 1.00 0.01 272.78 MB 1.00
GetALlCustomers .NET 9.0 .NET 9.0 7.926 s 0.0411 s 0.0384 s 4.43 0.05 1784.25 MB 6.54

Complete source code for the benchmarks

My repo

Include provider and version information

EF Core version:
Database provider Npgsql.EntityFrameworkCore.PostgreSQL (I tried to use SqlServer provider instead of Postgres and is shows the same results)
Version="8.0.10"
Version="9.0.0-rc.2.24474.1"

Target framework: (e.g. .NET 8.0 and .NET 9)
Operating system:
Windows 11 (for benchmarks) / Docker container (for load test)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions