简体   繁体   English

在 Java 对象之间复制数据的最快方法是什么?

[英]What would be the fastest way of copying data between Java objects?

I have various sets of Java objects, some of them pojos generated from JAXB tools, some of them domain classes etc.. In a large application, I need to get data from one set of objects and put into another set of objects which have different capabilities to use the data.我有各种 Java 对象集,其中一些是从 JAXB 工具生成的 pojo,其中一些是域类等。在大型应用程序中,我需要从一组对象中获取数据并放入另一组具有不同的对象使用数据的能力。

There are various methods of doing this: object mapping frameworks being an obvious choice.有多种方法可以做到这一点:object 映射框架是一个明显的选择。 However, most of these frameworks with a solid code base and community, use reflection.(dozer for example)但是,大多数具有可靠代码库和社区的框架都使用反射。(例如推土机)

I've been using a combination of adapters which adopt pojos and more complex java classes to visitor pattern, so that a visitor walking on the adaptor walks over one set of objects, and in the process creates another set of objects (the objects usually have parent/child/tree type of references) Especially when pass by reference is used rather than creating new Strings etc, this should be the fastest method available.我一直在使用采用 pojos 和更复杂的 java 类来访问访问者模式的适配器组合,以便在适配器上行走的访问者走过一组对象,并在此过程中创建另一组对象(对象通常具有父/子/树类型的引用)特别是当使用引用传递而不是创建新的字符串等时,这应该是最快的方法。

Can you think of any other method?你能想到其他方法吗? Some sort of serialization to a byte array in memory, then deserialization maybe?对 memory 中的字节数组进行某种序列化,然后可能是反序列化? Could it beat visitor based copies in terms of performance?它能否在性能方面击败基于访问者的副本? Am I being unfair to reflection based approaches like Dozer?我对像 Dozer 这样的基于反射的方法不公平吗? This is a key operation in the application, so any improvements are likely to improve overall performance significantly.这是应用程序中的关键操作,因此任何改进都可能显着提高整体性能。

The visitor pattern should be pretty near optimal for performance.访问者模式应该非常接近最佳性能。 Anything involving serialization is going to be worse because of the overhead of any kind of generalized mapping.由于任何类型的广义映射的开销,任何涉及序列化的事情都会变得更糟。

I'm not specifically familiar with dozer-- but reflection doesn't have to be such a huge hit if it is used essentially to automate the code writing you are doing by hand;我对推土机并不特别熟悉——但是如果反射本质上用于自动化你正在手工编写的代码,它就不必如此受欢迎; that is, if it generates a class (or equivalent logic tree) a single time to define the copying operation and then runs this repeatedly.也就是说,如果它一次生成 class (或等效的逻辑树)来定义复制操作,然后重复运行。 The reflection cost is amortized over a large number of operations and becomes negligible.反射成本在大量操作中摊销,变得可以忽略不计。

At the end of the day, you really need a benchmark and minimum requirements.归根结底,您确实需要基准和最低要求。

If you already meet the minimum time requirements for the copy, you can avoid changing any code and move on.如果您已经满足复制的最低时间要求,则可以避免更改任何代码并继续前进。 If you don't, when you meet the minimum requirements you can stop.如果你不这样做,当你满足最低要求时,你可以停止。

For every "fast" way, there is always some other way that might be a bit faster.对于每一种“快速”方式,总有一些其他方式可能会更快一些。 Stating minimum requirements quickly tells you if this needs to be fast (or you're just optimizing prematurely) and when to stop putting in resources to make it even faster.快速说明最低要求会告诉您这是否需要快速(或者您只是过早地优化)以及何时停止投入资源以使其更快。

Crafting a good java benchmark is not overly difficult, but do read up on it before you attempt it (as crafting a bad benchmark is ridiculously easy).制作一个好的 java 基准测试并不太难,但在尝试之前一定要阅读它(因为制作一个糟糕的基准测试非常容易)。

Adapter of Facade pattern will be a good idea. Facade 模式的适配器将是一个好主意。 The less you copy data around - the better.您复制数据的次数越少 - 越好。 Also, it is a good idea to make your data immutable, so you can avoid concurrency issues in the future.此外,让您的数据不可变也是一个好主意,这样您就可以避免将来出现并发问题。

I would stay away from in-memory serialization, especially with changing format.我会远离内存中的序列化,尤其是在改变格式的情况下。

Same goes to reflection.反思也是如此。 If you have a lot of objects and don't want to write adapters by hand, it is much better to use model driven approach and generate said adapters as part of your build process.如果您有很多对象并且不想手动编写适配器,最好使用 model 驱动方法并生成所述适配器作为构建过程的一部分。

If all you're concerned about is transferring data, and not actually creating a composite object of fields from two different classes, then I suggest a facade.如果您关心的只是传输数据,而不是实际创建来自两个不同类的字段的复合 object,那么我建议使用外观。 The facade will be your dispatch into several different objects and return one data transfer object (DTO) from the two.外观将您分派到几个不同的对象中,并从这两个对象中返回一个数据传输 object (DTO)。

http://en.wikipedia.org/wiki/Facade_pattern http://en.wikipedia.org/wiki/Facade_pattern

Some people would argue since you're not executing functionality, but returning a composite, then this would be called a composite or wrapper or adapter, but either way, I would still go with that approach.有些人会争辩说,因为您没有执行功能,而是返回复合,所以这将被称为复合或包装器或适配器,但无论哪种方式,我仍然会使用这种方法 go。

I usually start here:我通常从这里开始:

http://www.javacamp.org/designPattern/ http://www.javacamp.org/designPattern/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java中将两个位集相交到新的BitSet中最快的方法是什么? - What would be the fastest way to intersect two bitsets into a new BitSet in Java? 从 Java 中的文件中读取整数的最快方法是什么? - What would be the fastest way to read integers from a file in Java? 在.Net和Java之间共享数据(而不是对象)的最快(以性能为导向)方式 - Fastest(performance-wise) way to share data(not objects) between .Net & Java 在Java服务器和C#客户端之间共享数据对象的最快方法 - Fastest way to share data objects between a Java server and a C# client 什么是从Oracle数据库获取大量数据到Java对象的最快方法 - what's the fastest way to get a large volume of data from an Oracle database into Java objects 检查两个java对象的Deep Equal的最快捷有效的方法是什么? - What is the fastest and efficient way to check Deep Equal for two java objects? 在 Java 中测试素性的最快方法是什么? - What would be the fastest method to test for primality in Java? 将数据从应用程序(Java)导入临时表的最快方法是什么? - What is the fastest way to import data from application (Java) into temporary table? 从Java应用程序获取数据到Cassandra 2的最快方法是什么? - What is the fastest way to get data into Cassandra 2 from a Java application? 用Java替换数据的最快方法 - Fastest way to replace data in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM