<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="RSS_xslt_style.asp" version="1.0" ?>
<rss version="2.0" xmlns:WebWizForums="http://syndication.webwiz.co.uk/rss_namespace/">
 <channel>
  <title>DevForce Community Forum : Performance issues with ImportEntities</title>
  <link>http://www.ideablade.com/forum/</link>
  <description>This is an XML content feed of; DevForce Community Forum : DevForce 2010 : Performance issues with ImportEntities</description>
  <pubDate>Tue, 12 May 2026 23:24:37 -700</pubDate>
  <lastBuildDate>Wed, 06 Mar 2013 07:50:10 -700</lastBuildDate>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Web Wiz Forums 9.69</generator>
  <ttl>360</ttl>
  <WebWizForums:feedURL>www.ideablade.com/forum/RSS_post_feed.asp?TID=3978</WebWizForums:feedURL>
  <image>
   <title>DevForce Community Forum</title>
   <url>http://www.ideablade.com/forum/forum_images/IdeaBlade_logo_tm.png</url>
   <link>http://www.ideablade.com/forum/</link>
  </image>
  <item>
   <title>Performance issues with ImportEntities :   Thanks, Stephen. Thanks too...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3978&amp;PID=15945#15945</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=11" rel="nofollow">kimj</a><br /><strong>Subject:</strong> 3978<br /><strong>Posted:</strong> 06-Mar-2013 at 7:50am<br /><br />Thanks, Stephen.&nbsp; Thanks too for adding this issue to User Voice.]]>
   </description>
   <pubDate>Wed, 06 Mar 2013 07:50:10 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3978&amp;PID=15945#15945</guid>
  </item> 
  <item>
   <title>Performance issues with ImportEntities : You can find the code I use here....</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3978&amp;PID=15942#15942</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=613" rel="nofollow">stephenmcd1</a><br /><strong>Subject:</strong> 3978<br /><strong>Posted:</strong> 05-Mar-2013 at 1:42pm<br /><br />You can find the code I use <a href="https://gist.github.com/stephenmcd1/5094537" target="_blank">here</a>. &nbsp;As I say on that page, it's certainly not the most robust form of serialization. &nbsp;But it's ended up working well for us.]]>
   </description>
   <pubDate>Tue, 05 Mar 2013 13:42:43 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3978&amp;PID=15942#15942</guid>
  </item> 
  <item>
   <title>Performance issues with ImportEntities :   That&amp;#039;s interesting about...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3978&amp;PID=15844#15844</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=11" rel="nofollow">kimj</a><br /><strong>Subject:</strong> 3978<br /><strong>Posted:</strong> 07-Feb-2013 at 11:27am<br /><br />That's interesting about the ECS deserialization issue with large graphs.&nbsp; Would you mind sharing more information on your custom serialization/deserialization logic, if not here in a PM or email?&nbsp; ]]>
   </description>
   <pubDate>Thu, 07 Feb 2013 11:27:03 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3978&amp;PID=15844#15844</guid>
  </item> 
  <item>
   <title>Performance issues with ImportEntities : Thanks for the thorough response....</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3978&amp;PID=15842#15842</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=613" rel="nofollow">stephenmcd1</a><br /><strong>Subject:</strong> 3978<br /><strong>Posted:</strong> 07-Feb-2013 at 10:39am<br /><br />Thanks for the thorough response. &nbsp;Well, it's probably clear but I vote for the deep cloning logic to be revisited :-). &nbsp;But I'm sure it's not exactly the hottest new feature that most people would care about.<div><br></div><div>I think we'll switch over some of our logic to use the fast way. &nbsp;Hopefully will find any problems that might come up. &nbsp;:-). &nbsp;Luckily, I don't think tempids, validation errors,&nbsp;navigation&nbsp;properties, etc. will come into play in the cases we'll be using this.</div><div><br></div><div>If we do run into problems with our method, we might go with your suggestion about saving a pre-serialized EntityCacheState. &nbsp;That could be a good middle ground.</div><div><br></div><div>On a related note, we've tried saving EntityCacheStates to&nbsp;isolated&nbsp;storage as a way of caching large amounts of data across restarts of the app. &nbsp;Unfortunately, we found the performance of deserializing the data was not that great and so ended up writing our own logic to serialize and deserialize entities to isolated storage. &nbsp;We were trying to save a very deep/complex graph of entities so perhaps that is a case where the standard EntityCacheState stuff suffers and some custom logic can help.</div>]]>
   </description>
   <pubDate>Thu, 07 Feb 2013 10:39:02 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3978&amp;PID=15842#15842</guid>
  </item> 
  <item>
   <title>Performance issues with ImportEntities :   Deep cloning using the DataContractSerializer...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3978&amp;PID=15830#15830</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=11" rel="nofollow">kimj</a><br /><strong>Subject:</strong> 3978<br /><strong>Posted:</strong> 06-Feb-2013 at 6:15pm<br /><br />Deep cloning using the DataContractSerializer has not done us any favors, as it's the cause of several other performance issues, notably a 2-tier save problem.<div>&nbsp;</div><div>Aside from the deep clone, ImportEntities is also responsible for temp ID fixup, merging with entities already in cache, fixing up navigation properties, re-validating entities with existing errors, and generally ensuring that the state after the import is correct and consistent.&nbsp; Whether these activities are needed for all entities all the time, particularly if their state is Unchanged, is hard to say.&nbsp; I'm hesitant to say that the fast cloning solution will always be safe, but if your&nbsp;testing shows it's reliable and you haven't introduced new and obscure errors, then it probably is safe for your needs.</div><div>&nbsp;</div><div>We need to look at when and why we do a deep clone to determine when it's really necessary and worth the cost.&nbsp; We also need to look at using something other than the DCS to perform the deep clone.&nbsp; These items our on our defect list, but we won't have an answer for them soon.</div><div>&nbsp;</div><div>One alternative you might consider, which would be more appropriate for unchanging data and may not suit your requirements, is to store an EntityCacheState in either a MemoryStream or isolated storage, and use the em.CacheStateManager.RestoreCacheState overload which accepts a stream to load a destination EntityManager as needed.&nbsp; This would about halve the DCS time of the deep clone, but would still take care of&nbsp;the other merge logic done by ImportEntities.</div>]]>
   </description>
   <pubDate>Wed, 06 Feb 2013 18:15:02 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3978&amp;PID=15830#15830</guid>
  </item> 
  <item>
   <title>Performance issues with ImportEntities : We cache a lot of data in our...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3978&amp;PID=15801#15801</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=613" rel="nofollow">stephenmcd1</a><br /><strong>Subject:</strong> 3978<br /><strong>Posted:</strong> 05-Feb-2013 at 2:13pm<br /><br />We cache a lot of data in our Silverlight client. &nbsp;One of the main ways we accomplish this is by having an Entity Manager that has a bunch of commonly used entities. &nbsp;Then when somebody asks for data, we check that Entity Manager and use those entities if possible. &nbsp;However, in our case it is not safe to just return the same copy of the cached entities to all the callers - we need to give them copies. &nbsp;We do this by importing the cached entities into the EntityManager of the code asking for the data (we have one Entity Manager for each window and sometimes even for sections of the window that need isolation). &nbsp;Also, most of our code assumes that entities will be owned by an entity manager so it's not really safe to give back free-floating entities.<div><br></div><div>We've been doing some performance testing of our app and noticed that the ImportEntities logic is actually starting to be a bottleneck. &nbsp;I did a bunch of timings and it seems like a simple ImportEntities from a nearly empty entity manager into an empty entity manager will always take a certain, non-trivial amount of time. &nbsp;On my machine, I can import from one to twenty entities at a time and it will always take about 60 milliseconds. &nbsp;It's not until I start importing like 40 entities at the same time that the time starts to increase (40 takes like 66ms). &nbsp;If you're doing big imports, that extra 60ms is probably not a big deal....but in our case, we are sometimes doing dozens of tiny imports of just one to four entities at a time. &nbsp;And those 60ms start to add up.</div><div><br></div><div>I looked at how ImportEntities works and it seems like it makes a copy of the EntityCacheState using the DataContractSerializer to basically do a deep clone. &nbsp;In all my profiling, that is the logic that seems to be taking the longest time in various operations in our app. &nbsp;I'm not sure if there is anything that can be done. &nbsp;Perhaps many small imports is not really the intended use of that method? &nbsp;But if there could be some way to speed that up, I would very much appreciate it....</div><div><br></div><div>In trying to find alternate ways for quickly copying entities from one Entity Manager to another, I've come across the following solution and wanted to get your opinion. &nbsp;Basically, I check whether any of the entities I need to copy have changes or exist in the destination Entity Manager. &nbsp;In that case, it seems complicated so I just fall back to Import Entities. &nbsp;Luckily, in our code that rarely happens. &nbsp;Most of the time, we are moving unchanged entities into an empty (or mostly empty) Entity Manager. &nbsp;In that case, I just clone the entities and attach them. &nbsp;My code looks like this:<table width="99%"><tr><td><pre class="BBcode">private static void QuickImport(IList&lt;Entity&gt; toImport, EntityManager destinationManager, MergeStrategy mergeStrategy)</div><div>{</div><div>&nbsp; &nbsp; //If any of the passed in entities have pending changes or exist in the destination entity manager, use the&nbsp;</div><div>&nbsp; &nbsp; // &nbsp; standard (slow) Import Entities logic. &nbsp;Otherwise, we can just clone each entity and attach it to the destination</div><div>&nbsp; &nbsp; // &nbsp; for better performance.</div><div><br></div><div>&nbsp; &nbsp; if (toImport.Any(e =&gt;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.EntityAspect.EntityState != EntityState.Unchanged ||</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; destinationManager.FindEntity(e.EntityAspect.EntityKey, includeDeleted: true) != null))</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; destinationManager.ImportEntities(toImport, mergeStrategy);</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp; else</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; destinationManager.AttachEntities(toImport.Select(e =&gt; ((ICloneable)e).Clone()));</div><div>&nbsp; &nbsp; }</div><div>}</pre></td></tr></table></div><div><br></div><div>Does that seem like a good and safe solution? &nbsp;I tried to restrict it to the cases that I assume don't require any complicated import/merging logic.</div><div><br></div><div>For reference, here are some numbers on my timings comparing how long imports take for various numbers of entities and also the average time per entity for doing the merge. &nbsp;You'll notice that the ImportEntities logic takes a long time even with just a handful of entities.</div><div><br></div><div><img src="uploads/613/ImportTimings.png" height="578" width="707" border="0" /><br></div><div><br></div><div>Thanks!</div><div><br></div>]]>
   </description>
   <pubDate>Tue, 05 Feb 2013 14:13:32 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3978&amp;PID=15801#15801</guid>
  </item> 
 </channel>
</rss>