Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Fast deep copy

Thread view: 
Dlugi - 20 Jul 2007 19:10 GMT
Hi,

I need fast deep copy algorithm for cache. For now I was using
serialization to make deep copy of object, and this solution cause 30
times more overhead then disk access :/

TIA
Stefan Ram - 20 Jul 2007 19:20 GMT
>I need fast deep copy algorithm for cache. For now I was using
>serialization to make deep copy of object, and this solution cause 30
>times more overhead then disk access :/

 Using reflection you can copy all fields (recursively, but
 keeping a list of objects already copied so as to not copy
 them again in the case of cycles).

 If you can modify the classes, make each class implement a
 »deepCopy« method instead (This should also accept a Map of
 objects already copied, if the data structure should contain
 cycles).
nebulous99@gmail.com - 21 Jul 2007 12:03 GMT
>   If you can modify the classes, make each class implement a
>   ?deepCopy? method instead (This should also accept a Map of
>   objects already copied, if the data structure should contain
>   cycles).

Why use a Map instead of a Set for this?

I do prefer this to using the clone() method, as you'll be reminded
when writing recursive deepCopy() methods that there's no deepCopy()
on List, etc.; writing a recursive clone() method you may well
unthinkingly invoke clone() on a collection and get a shallow copy
instead of a won't compile as a result. Obvious bugs are better than
subtle bugs, especially ones that may initially be completely silent.

You also don't need to copy Strings and other value objects that are
not mutated, unless there's a need for the original and the copy of
the value object to compare != for some reason. Otherwise just copy
the reference.

Mutable objects that are semantically-constant might also not need
copying. (E.g. a protocol handler object that is stateful, but where
this is logically always the same object and it makes sense for both
copies of your larger structure to share the same one rather than use
two separate ones.)
Dlugi - 20 Jul 2007 19:56 GMT
It's not actual no longer, I implement deep copy in clone method and it
is 1000 times faster than serialization!!
Daniel Pitts - 20 Jul 2007 20:10 GMT
> It's not actual no longer, I implement deep copy in clone method and it
> is 1000 times faster than serialization!!

Careful, clone doesn't do a deep copy automatically.
All the objects you clone will have a shallow copy only, unless you
override the clone method.
Dlugi - 20 Jul 2007 20:39 GMT
Yes, I know it
Roedy Green - 20 Jul 2007 22:34 GMT
>It's not actual no longer, I implement deep copy in clone method and it
>is 1000 times faster than serialization!!

If you have any immutable objects in the tree, they likely don't need
to be duplicated.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.