Compiled Models Quicken App Startups in Entity Framework Core 6.0 Preview 5
Some six years in the making, an effort to quicken app startup times in Entity Framework Core via compiled models has been realized in the new v6.0 Preview 5.
As detailed in the Plan for Entity Framework Core 6.0, "Compiled models will allow the generation of a compiled form of the EF model. This will provide both better startup performance, as well as generally better performance when accessing the model."
The effort was proposed way back in March 2015 in a GitHub issue titled "Reduce EF Core application startup time via compiled models." That moved from In Progress to Completed last month. It must have been proposed in the very earliest stages of Microsoft's "Core" initiative (originally called Entity Framework 7) because EF Core 1.0 didn't debut until June 2016.
This month, Microsoft's Jeremy Likness announced initial support in EF Core 6.0 Preview 5. "This release includes the first iteration of compiled models. If startup time for your application is important and your EF Core model contains hundreds or thousands of entities, properties, and relationships, this is one release you don’t want to ignore," said the senior program manager - .NET Data, .NET.
Broadly speaking, the model generation is done by optimization via command-line interface (CLI) tooling to generate source code files that can be used in a project to build a compiled model.
For example, the 2015 issue mentioned above included this CLI command:
dotnet ef dbcontext optimize -c MyContext -o MyFolder -n My.Namespace
while Likness this month reported using this command:
dotnet ef dbcontext optimize -output-dir MyCompiledModels --namespace MyCompiledModels
That command (along with other steps) optimizes the "Build model" step of executing a first query, as depicted here (with cache boxes shown disabled for benchmarking):
Likness reported a 10x performance boost in one example with 449 entity types, 6,390 properties and 720 relationships. He pointed to this graphic -- comparing startup times for compiles and non-compiled models -- as an example of how startup times remain fast even if a model grows larger:
While that's an obvious "pro" for the scheme, Likness noted that the model must be manually synchronized by regenerating it when its definition or configuration change, which he deemed a "con." Also on the regenerating front, when asked by a developer if compiled models generated in the preview would be usable in the full EF Core 6.0 RTM release, Likness replied: "Like other generated code, the expectation is that you would have to regenerate the model at a minimum each minor release. There is an extensibility point being made so that you can customize the model without having to rewrite the code every time the compiled model changes. The extensibility API is expected to be backwards compatible through future releases."
Other "cons" he listed include lack of support for:
Developers are invited to find and upvote associated issues for the above if they are deemed critical to success. The new functionality can be tested by downloading the latest EF Core 6.0 preview and the latest EF Core CLI.
About the Author
David Ramel is an editor and writer for Converge360.