Optimize GraphDiff performance on first update (subsequent updates are quick)

entity-framework-6.1 graphdiff


I am working on a project with a very complex entity. The object graph contains approximately 30 objects in a tree with two levels (there are children of children).

The entity is managed in a browser, and sent as Json to a Web API endpoint. The endpoint serializes the JSON into the complex entity, then the repository uses GraphDiff to save the entity.

On first save, the UpdateGraph call takes around 12 seconds, on subsequent calls on the same entity, takes milliseconds.

I assume that the graph is cached per entity i the EF DbContext.

Is there any way to optimize the first call? We have only a few scenarios when we are using GraphDiff, maybe is there a way to prepare the graphs used in teh applicaton.

Thank you for your help.

11/21/2014 2:31:15 PM

Accepted Answer

I solved it by cloning the GraphDiff source code from GitHub and modifying it.

GraphDiff detects dynamically the keys of the entities, and because the way how the dynamic query expressions are generated to retrieve the already persisted objects, the expression are recompiled by EF every time when a new entity (another primary key) is used for the query. So the compiled queries are not cached only if the entity keys were used before. These query compilations took a lot of time in my case (10s).

The source code was modified to allow the injection of the already persisted entity. This means that instead of letting GrahpDiff to retrieve the entity from the DB, it was retrieved in an optimal way and was injected for merging into GraphDiff.

12/18/2014 6:44:11 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow