I'm doing some serialization of data (as a result of a TransferHandler
- basically, I'm doing a copy and paste) and I am noticing that it
becomes increasingly slow as I copy more and more data.
One piece of data that I copy is roughly 3MB of data. When I paste,
however, it takes relatively long (it's noticeable to the user, that's
for sure).
Any suggestions on how I might speed this up. I realize this is a
pretty generic question and if you need more data, I'd be happy to
provide it.
Firstly, i take it you're copy and pasting between applications, right? If
not, i think there's a way to do this without serialization at all.
If you do need to serialize, then you can look for ways to speed up
serialization. Default serialization does have a reputation for being a
bit slow - the problem is that it has to deal correctly with any
imaginable object structure, so it has to be very generic and quite
careful in how it does things. If you don't need everything it does you
can go faster.
The first thing you can do is mark as transient any fields you don't need
to be transported - anything that can be recomputed on the other side,
like caches. That cuts down the amount of stuff serialized, and the time
taken to do it. There might not be anything you can do this to, though.
Something i've heard of, but never tried, and am slightly skeptical about,
is declaring your persistent fields. In a class which will be serialized,
declare:
import java.i
bjectStreamField ;
private static final ObjectStreamField[] serialPersistFields = {
new ObjectStreamField("firstField", FirstFieldType.class),
new ObjectStreamField("secondField", SecondFieldType.class),
// etc
}
The serialization mechanism can use this array to guide its operation,
rather than reflection, and apparently this is faster. I'm dubious about
this: the thing is, you could fairly easily write a helper function would
would take a class and generate a serialPersistFields array for it using
reflection, and then call that to initialise your static
serialPersistFields variable. It would add a bit of a startup time, but
completely automate the process, and allegedly increase performance. But
if this is so, then why doesn't the serialization mechanism do this
itself?
Note that you can also use the ObjectStreamField objects to indicate that
a field is unshared, ie the object referenced is never pointed to from
elsewhere (or if it is, it doesn't matter), which makes serializing it
marginally faster.
Your next option is to implement Externalizable instead of Serializable,
and then write code to handle loading and storing your objects yourself.
This is tedious, but is pretty definitively the fastest way of doing it.
Read all about it:
http://java.sun.com/developer/TechTips/2000/tt0425.html#tip1
Another tack would be to use a third-party serialization implementation
that claims to be faster than Sun's:
http://www.jboss.org/serialization/http://jserial.sourceforge.net/
JBoss's even claims to be faster than Externalizable.
tom