Emit mapper vs valueinjecter or automapper performance
Asked Answered
S

1

22

I have spent some time comparing this three mappers and it is interesting why so big performance diffrenece between emitmapper and any of valueinjecter or automapper(last two comparable by performance). From benchmark test in emitmapper solution (1000000 iterations):

    Auto Mapper (simple):        38483 milliseconds
    Emit Mapper (simple):        118 milliseconds
    Handwritten Mapper (simple): 37 milliseconds

    Auto Mapper (Nested):        53800 milliseconds
    Emit Mapper (Nested):        130 milliseconds
    Handwritten Mapper (Nested): 128 milliseconds

    Auto Mapper (Custom):        49587 milliseconds
    Emit Mapper (Custom):        231 milliseconds

Also some benchmarks from valueinjecter runned with added emitmapper(for 10000 iterations):

    Convention: 00:00:00.5016074
    Automapper: 00:00:00.1992945 
    Smart convention: 00:00:00.2132185
    Emit mapper(each time new mapper): 00:00:00.1168676
    Emit mapper(one mapper): 00:00:00.0012337

There in first emit mapper test - it was created each time, in second - one mapper for all conversions.

Taking this into account, have result as valueinjecter(also as automapper) slower than in 100 times than emit mapper. What is a reason of so huge performance difference? As for me object to object mapper cannot took so much time comparing to handwritten mapper as it be a bottleneck of project(if we need to map collection of objects for example).

At this moment I'm thinking about using emit mapper, but only one reason why I'm not ready to decide: emit mapper not supported at all by first developers, but I'm not sure that this is very important(very low possibility to requirement of some additional functionality).

Shelburne answered 24/10, 2012 at 16:34 Comment(3)
It is really difficult to draw conclusions from data when you don't know what was tested. Automapper uses reflection to do the initial auto mapping and then caches the results. If you redid that part every iteration it could perform badly. You will likely get better answers if you show how you ran your tests.Assize
I've used for second part as base code from this page:valueinjecter.codeplex.com/… for first part I've used benchmark project from emitmapper project from emitmapper's homepage(emitmapper.codeplex.com)Shelburne
Thanks for providing some benchmarks, made me decide against using Automapper. It slowed down everything drastically.Threw
T
12

The reason is explained int the EmitMapper documentation:

It effectively uses the Emit library to generate mappers at run-time direct in IL as though these mappers are written by hand. Most other mappers use the Reflection library for mapping (or source code generation). Also EmitMapper minimizes boxing-unboxing operations and additional calls during mapping. For example it performs type conversion for value-types without boxing-unboxing and converts nested members without recursion (one-pass algorithm) when it is possible.

Reflection is extremely slow compared to handwritten code. EmitMapper instead, compared to handwritten mapping, has only the startup overhead when it emits.

Trulatrull answered 8/5, 2013 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.