📜 ⬆️ ⬇️

Benchmark: AutoMapper vs BLToolkit vs EmitMapper

What is it about?


Speech in this note will focus on libraries for automatically copying the fields of one object into the fields of another (object mapping). For what it is necessary to read, for example, here .

I propose to consider the following libraries, with the help of which one can solve the above described problem:

1) AutoMapper
2) BLToolkit
3) EmitMapper
')
All of these libraries are distinguished by the fact that they use Emit to generate code at runtime, and they can potentially work with efficiency close to manual coding. Is it, I suggest to check.

Automap

Website: automapper.codeplex.com

At the moment it is the most common library. The library contains a decent set of methods for expanding and customizing the mapping, and also supports working with complex nested objects. Although to work with the latter, you will have to additionally do squats, after registering each pair of matched types.

BLToolkit

Website: bltoolkit.net

This is not a pure Objects to Objects mapper (rather, it is a lightweight ORM), although it supports the mapping of some objects to others. But due to the fact that this is not a pure Objects to Objects mapper, he lacks many of the features inherent in full-fledged mappers such as AutoMapper or EmitMapper. The library does not support complex objects, and the mapping itself cannot be customized in any way.

Emitmapper

Website: emitmapper.codeplex.com

Fresh development. A very flexible library that allows meppit almost anything anywhere. For example, you can write the mapper's configuration for this library, which reads the data from the DbDataReader and writes it to some object. At the moment, it is the only library under consideration that is available under Silverlight (at least in official releases).

Performance comparison


The following tests were written for performance comparison.

A simple test.

Two simple classes are mapped, all fields of which are of primitive types and without any additional customization. This is the only test that can be performed on BLToolkit. Test code can be found here .

Results (time in milliseconds):
Auto mapper36809
BLToolkit34533
Emit mapper117
Handwritten mapper37

From the numbers it is clear that the difference is very impressive, but for a better idea of ​​the scale, I suggest looking at the diagram:

simple test

Nested classes.

Two classes having a complex structure are compared. Test code can be found here .

Results (time in milliseconds):
Auto mapper52238
Emit mapper102
Handwritten mapper97
BLToolkitnot supported

In this test, you can see that the performance of the Emit Mapper and the manual code is almost identical.
Diagram:
nested classes

Customization

This test verifies the speed of the work of complex custom matching. In particular, Custom constructors, Null substitution and Custom converters are actively used. Test code is available here.

Results (time in milliseconds):
Auto mapper50142
Emit mapper197
BLToolkitnot supported

Digram:
customized mapping

findings


The demonstrated difference in performance is very significant and changes the very approach to the use of automatic object mappers. If in the case of AutoMapper we must constantly keep in mind the requirements for this part of the code (how often it is used), then in the case of EmitMapper none of this is required. We just do the mapping where we need it and know that everything will be fine with performance.

PS: The comments reasonably asked about the reasons for such a difference in performance. To avoid misunderstandings, here they are:
1) In EmitMapper, it is possible to pre-generate a mapper and save it somewhere (for example, during program start in a static field). In AutoMapper and BLToolkit, we are forced to use one single global entry point of the type: "Map.ObjectToObject (foo)". And this is the cost of synchronization, dictionary search, etc.
2) AutoMapper was originally designed and written for Reflection and only then transferred to Emit. Because of this, there are a lot of unnecessary overhead.
3) EmitMapper almost never uses boxing / unboxing, unlike its competitors. Anyway, the code that it generates is very effective.

Source: https://habr.com/ru/post/80793/


All Articles