Java Orika vs JMapper - 它是如何工作的以及速度差异 - 为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22078156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Orika vs JMapper - how it works and a speed difference - why?
提问by
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:
我已经下载并测试了这两个映射库。我写了一个程序,它有 100000 次迭代并映射同一个类的 bean:
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
手绘图:1ms
Orika mapping: 32ms
Orika 映射:32ms
Hand mapping: 6msGREAT SPEED !!!
手绘图:6ms极速!!!
Dozer: 1140ms
推土机: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..
我知道,Orika 和 Jmapper 是来自 Google 的很棒的库,它们使用反射的方式与 Dozer 不同,例如 Dozer 慢得多,它们以某种方式使用反射来生成代码。
I have 3 questions:
我有3个问题:
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.?
1)它们是如何工作的——在生成代码时、在 Maven 构建期间、在运行时——每次我在代码中创建映射器时?当我创建映射器时,它们是否会动态更改类代码字节。?
2) Why there is this speed difference that I noticed? If the generate code somehow, then why there are different results
2)为什么我注意到这种速度差异?如果以某种方式生成代码,那么为什么会有不同的结果
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?
3)你会选择哪个图书馆,为什么?两者具有相同的功能?为什么两者都来自谷歌?为什么 Google 没有开发 Orika 而是创建了 Jmapper?
回答by harmingcola
I'm not familiar with Jmapper, so i'll concentrate on Orika and Dozer
我不熟悉 Jmapper,所以我将专注于 Orika 和 Dozer
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.
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.
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
它们是如何工作的?它们的工作原理不同。Dozer 使用反射,Orika 使用字节码生成。在 maven 构建期间?什么都没有发生,一切都在运行时完成。Dozer 通过 get 方法访问字段并通过 setter 方法设置目标对象中的值。Orkia 生成字节码来完成这项工作,就像您自己完成了手绘地图一样。第一次转换很慢,之后的每个转换都应该更快。
推土机,应该始终保持大致相同的速度,依靠反射。Orika,字节码生成,第一次运行在生成映射代码时应该慢很多。
简短的回答,这取决于。你想映射什么?如果类大致相似,Dozer 非常擅长从一种类型映射到另一种类型。它根本不处理地图。如果您的对象中有 Map,请准备好编写自定义转换器代码
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.
Orika 擅长在相同类型的两个对象之间映射数据。它的某些 List 处理有点奇怪,它将列表视为单个对象而不是单个对象的集合。同样,也准备为此编写一些代码。
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
除非您要在应用程序中进行大量映射,或者需要频繁更改的映射。自己写
回答by Alessandro
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.
JMapper 基于 Javassist 框架,该框架的强大之处在于能够在不损失性能的情况下应用丰富、动态映射、多关系映射、继承映射等特性。
所有代码都是在构造函数阶段编写的。jmapper 的目标是:易于使用和手工编码的性能。
回答by Sudheer OSI Technologies
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/
JMapper Framework 是一个 java bean 到 java bean 的映射器,允许您使用注释和/或 XML 动态执行数据的传递。jmapper 不限于在运行时生成代码,它应用了许多开发人员通常不会做的优化。使用 JMapper,我们拥有动态映射的所有优点和静态代码的性能,0 内存消耗 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.
优点: 1. XML 和基于注解的映射可用。2. 1 到 N 和 N 到 1 的关系 3. 显式转换 4. 继承的配置。5、性能优于推土机。
Cons: 1. Aggregation is achieved through Explicit conversions 2. Basic documentation. 3. JMapper still in development.
缺点: 1. 聚合是通过显式转换实现的 2. 基本文档。3. JMapper 仍在开发中。
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/
MapStruct:MapStruct 是用于 bean 映射的编译时代码生成器,可在运行时生成快速(不使用反射或类似)、无依赖和类型安全的映射代码。 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.
优点: 1. 通过接口映射。2. 编译时间代码生成,导致速度快(没有使用反射或类似)。3. 源对象的嵌套映射。4. 隐式类型转换,例如所有 Java 基本类型(包括它们的包装器)和 String 之间的转换,例如 int 和 String 或 Boolean 和 String 之间的转换。5. 自定义映射器。6. 映射集合容易。7.异常处理。8. 反向映射。9. 良好的文档和论坛。10、性能比Jmapper和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.
缺点: 1. 不应嵌套目标属性。2.对于反向映射,需要额外的映射或需要创建新的映射器。3. 我们可以使用手写代码代替 MapStruct 并提供更好的性能。