none
System.Reflection.Emit.DynamicMethod.CreateDelegate slow on Windows 7 RRS feed

  • Question

  • Hi

    We have an application running on 3.5 and so far we have run it successfully on windows XP. Now we have done an installation on a quadcore, x64, Win 7 machine and we are having some performance issues. We use Linq to Sql towards a SQL Express 2008 R2 (both environments) and after a lot of debugging I can see that our queries spend around 4-5 seconds in ObjectReaderCompiler.Compile on row 198 (var fnMaterialize = (Delegate)dm.CreateDelegate(fnMatType);) it seems to be System.Reflection.Emit.DynamicMethod.CreateDelegate  that is called.

    Can anybody explain why this is 10 times slower on Windows 7 than on Win XP?

    The Linq query is just a simple;

    var x=persons.Where(_ => _.Id == new Guid("CC52F9E1-C756-413E-9C10-10180E2426F9")).ToList();
    


    Thursday, September 22, 2011 12:19 PM

Answers

  • When setting the target platform to x86 instead of AnyCPU the application behaves as expected. So Linq2Sql and x64 doesn't seem to play along very well.

     

    The "bug" is reported to the production team so now we only have to wait for fix...

    • Edited by Johan_b Friday, November 4, 2011 7:52 AM
    • Marked as answer by Johan_b Friday, November 4, 2011 7:53 AM
    Thursday, November 3, 2011 11:41 AM

All replies

  • Hmmn, are the timings consistent?  Did you make more than one run?

    Thursday, September 22, 2011 1:30 PM
  • I made several runs, if I start 10 threads on XP, the first call takes about 3 seconds then it is fast. On the win 7 it takes 17 seconds before the first call is made...

    I have been able to break out pieces of the code so I can reproduce the problem. We are doing something that Linq to Sql doesn't like...


    I haven't been able to time CreateDelegate method, but in the production code the timings are fairly equal (ranging from 5-7 seconds) on win 7
    • Edited by Johan_b Thursday, September 22, 2011 1:38 PM
    Thursday, September 22, 2011 1:36 PM
  • The hardware on both environments is the same?  Can you provide detailed timmings for both environments?

    Thursday, September 22, 2011 2:32 PM
  • No it is not the same. The XP is x86, dual core. Win 7 is x64 quadcore.

    The timings I have is on a higer level:

    Two simultaneous threads on win XP Thread number within ()

    Time taken (1): 0
    Time taken (0): 0
    Time taken (0): 3196
    Time taken (1): 3249
    Time taken (0): 3707
    Time taken (1): 3729
    Time taken (0): 3778
    Time taken (1): 3797
    Time taken (0): 3810
    Time taken (1): 3829

     

    The same program run on Win 7 gives:

    Time taken (0): 0
    Time taken (1): 0
    Time taken (1): 6931
    Time taken (0): 6931
    Time taken (0): 8010
    Time taken (1): 8010
    Time taken (0): 8046
    Time taken (1): 8055
    Time taken (1): 8098
    Time taken (0): 8098

    Thursday, September 22, 2011 2:51 PM
  • Has the code been recompiled to target x64?

    Thursday, September 22, 2011 3:06 PM
  • No, the target is AnyCPU (deployed from vs2008 on XP). I'll try to pin the test-program to just one cpu later on. But it has not helped on the production code... I have also disabled Hyperthreading without any changes in performance
    Thursday, September 22, 2011 3:13 PM
  • Hmmm, see if you can create a couple of VM, one running XP and one running Win7 to rule out hardware as the issue.

    You can set the affinity to a specific core to and see if that helps.

    Sometimes more cpus are worse because the program doesn't take a advantage of the additional cores, either because it doesn't require it or because it wasn't written that way.

    btw are the sql generated the same?

     

    Thursday, September 22, 2011 3:24 PM
  • I would agree with Serguey123 that it can for sure be hardware.

    I have had stranger issues when changing CPU vendor, so my first advice is to see if they run they run on the same speed on AMD and on Intel.

    The second is that some times a special calculation thrashes the L3 cache on CPUs from one of the vendors if you run on a quad core, but if you run single core the same task it runs faster.

    I have access to some Windows 7 x64 / 2008 R2 servers if you want me to test it on different dual/quadcore hardware. Ask Fabian to contact me in that case.

    Thursday, September 22, 2011 3:35 PM
  • I might have come up with something. We have a class with 17 InheritanceMappings and it seems that this is what causes the problems. If I reduce it to about 7 the performance goes from 7 seconds to about 2 seconds in my test code. I have to do some more testing on this...
    Thursday, September 22, 2011 6:41 PM
  • Hello,

    Like two community members' said, it looks this line may not cause the performance of your question. In addition, I suggest you can debug/monior the performance and statistics with the help of debugger tool such as WinDbg, Performance tool, etc.

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, September 26, 2011 3:49 AM
  • I ran a test program with my domainmodel and I started with two inheritancemappings and I ran the same testprogram on both Win XP and on Win 7. For each run I added one inheritancemapping of our domain. The results are really crazy:

    I have run other tests which simulates part of our domain that does not show this behaviour so there is something in the domain that makes Win 7 go nuts when it compiles the query.

    The x-axis is number of inheritancemappings and y-axis is average time to first retrieveal in milliseconds.
    • Edited by Johan_b Monday, September 26, 2011 7:08 AM
    Monday, September 26, 2011 7:02 AM
  • It seems that if you have an baseclass where there is a lot of classes inhereting that baseclass (direct or indirect) soemthing goes bad. If you add a property on the baseclass this is not very well treated on the Win 7 machine. You can see time-differences when I have around 10 subclasses (deepest tree is 3 subclasses) and 8 properties on the baseclass. It is probably when win 7 tries to merge all the classes that something goes wrong.
    • Edited by Johan_b Monday, September 26, 2011 10:57 AM
    Monday, September 26, 2011 10:55 AM
  • here, data and modeling customer advisory team has already delivered potential performance issues with compiled LINQ query recompiles.

    Thanks,

    Werewolf,


    Just a newbie for everything.
    Tuesday, September 27, 2011 9:31 AM
  • From my point of view the problem is not that it recompiles, the problem is that the compilation takes much longer time on Win 7 than on Win XP...  I'll read the blog to see how it can fit my problem. Thanks
    Tuesday, September 27, 2011 10:26 AM
  • Hello,

    Honestly speaking, it's hard to say about performance issue just according to description. You ran your application under the same framework version, but different OS version. So far it looks that your question may not be related to framework itself, but I think we should need more detailed information to check it. Could you please provide your demo code snippet to us to help us analyze your question more detail? You can directly send it to me. v-xugong@microsoft.com

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, September 28, 2011 5:53 AM
  • Did you get my email?

    Best Regards,

     

    Johan

     

    Thursday, September 29, 2011 12:23 PM
  • Hello Johan,

    I double checked my mailbox, but unfortunately I didn't receive anything from you.

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 29, 2011 12:52 PM
  • Ok, I resent it now.

     

    Thanks,

    Thursday, September 29, 2011 1:13 PM
  • Just an update. We realized that is probably some kind of memory issue. If we run our test program with 8 threads we have a virtual memory usage of around 3 GB on Win 7 but only 80 MB on XP...

    The issue is handled by Microsoft Support at the moment...

    Thursday, November 3, 2011 9:34 AM
  • When setting the target platform to x86 instead of AnyCPU the application behaves as expected. So Linq2Sql and x64 doesn't seem to play along very well.

     

    The "bug" is reported to the production team so now we only have to wait for fix...

    • Edited by Johan_b Friday, November 4, 2011 7:52 AM
    • Marked as answer by Johan_b Friday, November 4, 2011 7:53 AM
    Thursday, November 3, 2011 11:41 AM