Orika vs JMapper - how it works and a speed difference - why?

女生的网名这么多〃 提交于 2020-04-09 16:46:53

问题


I have downloaded and testing these two mapping libraries. I wrote a program which has 100000 iterations and maps the beans of the same class:

public class IntBean {

    @JMap
    private int int1;
    @JMap
    private int int2;
    .
    .
    .
    @JMap 
    private int int10;
}

Mappers are created BEFORE iterations start:

private JMapper jmapper = new JMapper(IntBean.class, IntBean.class);
private MapperFactory orikaFactory = new DefaultMapperFactory.Builder().build();
private MapperFacade orikaFacade = null;
orikaFactory.registerClassMap(orikaFactory.classMap(IntBean.class,IntBean.class).byDefault().toClassMap());
orikaFacade = orikaFactory.getMapperFacade();

What is in each iteration:

this.orikaFacade.map(a1, a2);

or

a2 = (A) this.jmapper2.getDestination(a1);

Hand mapping: 1ms

Orika mapping: 32ms

Hand mapping: 6ms GREAT SPEED !!!

Dozer: 1140ms

I know, that Orika and Jmapper are great libraries from Google and they use reflection in a different way than for example Dozer, which is much slower, they se reflection to generete code somehow..

I have 3 questions:

1) How they work - when the code is generated, during maven build, in runtime - everytime when I create mapper in code? Are they change class code byte dynamically when I create mappers.?

2) Why there is this speed difference that I noticed? If the generate code somehow, then why there are different results

3) Which library would you choose and why? Both have the same capabilities? Why both come from Google? Why Google didnt develop Orika and created Jmapper instead?


回答1:


I'm not familiar with Jmapper, so i'll concentrate on Orika and Dozer

  1. How do they work? They both work reasonably differently. Dozer using reflection and Orika using bytecode generation. During maven build? Nothing happens, its all done at runtime. Dozer access fields via their get methods and sets the value in a target object via setter methods. Orkia generates bytecode to do the work as if you'd done a hand mapping yourself. Its slow on the first conversion and should be faster on each one after that.

  2. Dozer, should always be roughly the same speed, relies on reflection. Orika, bytecode generation, 1st run should be alot slower while its generating the mapping code.

  3. Short answer, it depends. What are you trying to map? Dozer is very good at mapping from one type to another if the classes are roughly similar. It doesnt deal with Maps at all. Be prepared to write custom converter code if you have a Map in your Object

Orika is fantastic at mapping data between two objects of the same type. Some of its List handling is a little odd where it'll treat a list like a single object instead of a collection of individual objects. Again, be prepared to write some code for this too.

Neither handles a large Object graph particularly well, be prepared to write a lot of configuration for this one.

Unless you're going to be doing a lot of mapping in an application, or mapping that needs to change frequently. Write your own




回答2:


JMapper is based on Javassist framework, the power of this framework is the ability to apply enrichment, dynamic mappings, multi relational mappings, inherited mapping and other features without lose performance.
All code is written in constructor phase. The objective of jmapper is: the ease to use with performance of hand coding.




回答3:


JMapper:

JMapper Framework is a java bean to java bean mapper, allow you to perform the passage of data dynamically with annotations and / or XML. jmapper is not limited to generating code at runtime, it applies a number of optimizations that the developer does not do normally. With JMapper we have all the advantages of dynamic mapping with the performance of static code, with 0 memory consumption https://code.google.com/p/jmapper-framework/

Pros: 1. XML and annotation based mappings are available. 2. 1 to N and N to 1 relationships 3. Explicit conversions 4. Inherited configurations. 5. Performance is good compared to dozer.

Cons: 1. Aggregation is achieved through Explicit conversions 2. Basic documentation. 3. JMapper still in development.

MapStruct: MapStruct is a compile-time code generator for bean mappings, resulting in fast (no usage of reflection or similar), dependency-less and type-safe mapping code at runtime. http://mapstruct.org/

Pros: 1. Mapping through interface. 2. Compile time code generation, resulting in fast (no usage of reflection or similar). 3. Nested mappings for source object. 4. Implicit type conversions, for example between all Java primitive types (including their wrappers) and String, e.g. between int and String or Boolean and String. 5. Custom mappers. 6. Mapping collections easy. 7. Exception handling. 8. Reverse mappings. 9. Good documentation and forums. 10. Performance is good compared to Jmapper and dozer.

Cons: 1. Target properties should not be nested. 2. For Reverse mapping, it will need extra mapping or needs to create new mapper. 3. We can use hand written code instead of MapStruct and its give better performance.



来源:https://stackoverflow.com/questions/22078156/orika-vs-jmapper-how-it-works-and-a-speed-difference-why

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!