Entity Framework Core: Default Comparer For Byte Arrays May Waste Lots Of Memory And CPU

The default implementation of Entity Framework Core prefers to play it safe (for good reasons) when working with byte arrays. This 'safety' is - in some use cases - unnecessary and costs us a lot of memory and CPU. In this article, we will see that doing less is sufficient for the given property thanks to one of the most overlooked features of Entity Framework.

In diesem Artikel:

pg
Pawel Gerr ist Architekt und Consultant bei Thinktecture. Er hat sich auf .NET Core Backends spezialisiert und kennt Entity Framework von vorne bis hinten.

Please note: this article is not about whether a byte array should or should not be used with relational databases but rather about “if you do, then be aware of …”

EF's default behavior with byte arrays

When working with byte arrays and change tracking is active, then on SaveChanges Entity Framework Core (EF) is not just comparing the object references of the arrays, but the content as well. If the corresponding property represents some kind of bit-mask, i.e., every byte in the array is changed independently, then comparing every byte is necessary. But, most of the time, I see in projects that the properties are used for persisting small binary data, like thumbnails, which are considered immutable. In such cases, it is unlikely that someone will change single bytes inside the array. If the thumbnail has to be changed, then the byte array is replaced by another byte array, i.e., the new one is a completely new object reference.

How much does it cost?

Having some binary data, the comparison of the content is not wrong in general but unnecessary. I’ve benchmarked a few use cases in terms of memory and CPU usage. One entity was using the default behavior, the other a custom ValueComparer.

The benchmarks update 10k entities with 1kB array each. Before calling SaveChanges, the property is assigned one of two new arrays. One new array has 1 different byte at the beginning and is considered the best-case, and the other has a different byte at the end of the array.

				
					// array read from database = [0,0,0,...,0];

var newArray_bestCase = [1,0,0,...,0];
var newArray_worstCase = [0,0,0,...,1];
				
			

All benchmarks do two things: update the property bytes and call SaveChanges.

				
					entitiesLoadedFromDb.ForEach(e => e.Bytes = newArray_bestCase); // or newArray_worstCase

await myDbContext.SaveChangesAsync();
				
			

For benchmarking, I use the library BenchmarkDotNet with the MemoryDiagnoser.

The source code can be found on GitHub

				
					|            Method |       Mean |    Error |   StdDev | Gen 0 |Gen 1 | Allocated |
|------------------ |-----------:|---------:|---------:|------:|-----:|----------:|
|  Default_BestCase |   337.0 ms |  4.01 ms |  3.75 ms |  7000 | 2000 |     63 MB |
| Default_WorstCase | 1,220.7 ms | 11.84 ms | 11.07 ms | 66000 | 2000 |    531 MB |
|   Custom_BestCase |   325.6 ms |  6.16 ms |  5.46 ms |  8000 | 2000 |     65 MB |
|  Custom_WorstCase |   330.5 ms |  5.02 ms |  4.70 ms |  8000 | 2000 |     65 MB |
				
			

Worst case, the memory usage rises from 63 MB to 531 MB (ca. 850%) and the duration from 337 ms to 1220 ms (over 350%), when using the default behavior. With the custom ValueComparer, the values always staylow.

Use reference equality for opaque binary data

The ValueComparer can be changed in OnModelCreating or in IEntityTypeConfiguration<T> via the method SetValueComparer. The method expects an instance of ValueComparer, which can be implemented from scratch or by using the generic class ValueComparer<T>. The constructor of ValueComparer<T> expects three expressions:

  • equalsExpression: compares two instances using reference equality
  • hashCodeExpression: computes the hash code
  • snapshotExpression: passes the reference of the array as is because it is enough for reference equality
				
					builder.Property(e => e.Bytes)
       .Metadata
       .SetValueComparer(new ValueComparer<byte[]>(
            (obj, otherObj) => ReferenceEquals(obj, otherObj),
            obj => obj.GetHashCode(),
            obj => obj));
				
			

Summary

In this article, we looked at the ValueComparer and how it affects memory and CPU usage when using byte arrays with EF. Although we were talking about byte arrays only, the same performance issues could arise with all custom objects with a ValueConverter (please note: Converter, not Comparer).

Kostenloser
Newsletter

Aktuelle Artikel, Screencasts, Webinare und Interviews unserer Experten für Sie

Verpassen Sie keine Inhalte zu Angular, .NET Core, Blazor, Azure und Kubernetes und melden Sie sich zu unserem kostenlosen monatlichen Dev-Newsletter an.

Newsletter Anmeldung
Diese Artikel könnten Sie interessieren
.NET
pg

Pattern Matching with Discriminated Unions in .NET

Traditional C# pattern matching with switch statements and if/else chains is error-prone and doesn't guarantee exhaustive handling of all cases. When you add new types or states, it's easy to miss updating conditional logic, leading to runtime bugs. The library Thinktecture.Runtime.Extensions solves this with built-in Switch and Map methods for discriminated unions that enforce compile-time exhaustiveness checking.
26.08.2025
.NET
pg

Value Objects in .NET: Integration with Frameworks and Libraries

Value Objects in .NET provide a structured way to improve consistency and maintainability in domain modeling. This article examines their integration with popular frameworks and libraries, highlighting best practices for seamless implementation. From working with Entity Framework to leveraging their advantages in ASP.NET, we explore how Value Objects can be effectively incorporated into various architectures. By understanding their role in framework integration, developers can optimize data handling and enhance code clarity without unnecessary complexity.
12.08.2025
.NET
pg

Smart Enums: Adding Domain Logic to Enumerations in .NET

This article builds upon the introduction of Smart Enums by exploring their powerful capability to encapsulate behavior, a significant limitation of traditional C# enums. We delve into how Thinktecture.Runtime.Extensions enables embedding domain-specific logic directly within Smart Enum definitions. This co-location of data and behavior promotes more cohesive, object-oriented, and maintainable code, moving beyond scattered switch statements and extension methods. Discover techniques to make your enumerations truly "smart" by integrating behavior directly where it belongs.
29.07.2025
.NET
pg

Discriminated Unions: Representation of Alternative Types in .NET

Representing values that may take on multiple distinct types or states is a common challenge in C#. Traditional approaches—like tuples, generics, or exceptions—often lead to clumsy and error-prone code. Discriminated unions address these issues by enabling clear, type-safe modeling of “one-of” alternatives. This article examines pitfalls of conventional patterns and introduces discriminated unions with the Thinktecture.Runtime.Extensions library, demonstrating how they enhance code safety, prevent invalid states, and improve maintainability—unlocking powerful domain modeling in .NET with minimal boilerplate.
15.07.2025
.NET
pg

Handling Complexity: Introducing Complex Value Objects in .NET

While simple value objects wrap single primitives, many domain concepts involve multiple related properties (e.g., a date range's start and end). This article introduces Complex Value Objects in .NET, which group these properties into a cohesive unit. This ensures internal consistency, centralizes validation, and encapsulates behavior. Discover how to implement these for clearer, safer code using the library Thinktecture.Runtime.Extensions, which minimizes boilerplate when handling such related data.
01.07.2025
.NET
pg

Smart Enums: Beyond Traditional Enumerations in .NET

Traditional C# enums often fall short when needing to associate data or behavior with constants, or ensure strong type safety. This article explores the "Smart Enum" pattern as a superior alternative. Leveraging the library Thinktecture.Runtime.Extensions and Roslyn Source Generators, developers can easily implement Smart Enums. These provide a robust, flexible, and type-safe way to represent fixed sets of related options, encapsulating both data and behavior directly within the Smart Enum. This results in more maintainable, expressive, and resilient C# code, overcoming the limitations of basic enums.
17.06.2025