Re: Serialization Slow?
- From: Tom Anderson <twic@xxxxxxxxxxxxxxx>
- Date: Fri, 7 Nov 2008 17:45:19 +0000
On Fri, 7 Nov 2008, Jason Cavett wrote:
On Nov 7, 8:47 am, Tom Anderson <t...@xxxxxxxxxxxxxxx> wrote:On Thu, 6 Nov 2008, Jason Cavett wrote: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.
To provide a little more specifics...
I am overriding the method:
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException;
from the Serialization interface in a class that is (ultimately) being Serialized during cut/copy/paste. However, in my readObject implementation (see below), it is taking a ton of time during pasting (based on profiling that I have done). That's why this issue came up in the first place.
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException {
in.defaultReadObject();
this.open = false;
this.attributeLock = new Object();
this.childrenLock = new Object();
this.parentLock = new Object();
}
Since you're doing defaultReadObject, all the normal serialization stuff is happening, at normal serialization speed (as you're doubtless aware).
What I'm not sure of, is how I can change this to use the techniques you described above. I'll keep working at it, but if you can point me in the right direction here, I'd appreciate it.
I'd start by trying the JBoss serialization thing, which doesn't involve changes to your code, just a different stream.
If you're going to do externalization, you have to do write readExternal and writeExternal methods which write your fields to the stream, as described in the docs:
http://java.sun.com/javase/6/docs/api/java/io/Externalizable.html
Basically, if you have a class that looks like:
class MyClass {
private int foo ;
private String bar ;
private HisClass baz ;
}
Then your methods look like:
public void readExternal(ObjectInput in) {
foo = in.readInt() ;
bar = in.readUTF() ;
baz = (HisClass)in,readObject() ;
}
public void writeExternal(ObjectOutput out) {
out.writeInt(foo) ;
out.writeUTF(bar) ;
out.writeObject(baz) ;
}
Although you can (and perhaps should) use read/writeObject for strings instead of read/writeUTF.
You can take it a step further and take care of writing child objects manually if you like. For example, if i had:
class Dictionary {
private SortedMap<String, List<String>> words ;
}
I might write this:
public void writeExternal(ObjectOutput out) {
out.writeInt(words.size()) ;
for (Map.Entry<String, List<String>> entry: words.entrySet()) {
String word = entry.getKey() ;
List<String> definitions = entry.getValue() ;
out.writeUTF(word) ;
out.writeInt(definitions.size()) ;
for (String definition: definitions) out.writeUTF(definition) ;
}
}
public void readExternal(ObjectInput in) {
words = new TreeMap() ;
int numWords = in.readInt() ;
for (int i = 0 ; i < numWords ; ++i) {
String word = in.readUTF() ;
int numDefinitions = in.readInt() ;
List<String> definitions = new ArrayList(numDefinitions) ;
words.put(word, definitions) ;
for (int j = 0 ; j < numWords ; ++j) definitions.add(in.readUTF()) ;
}
}
It's a pain in the balls to have to write lots of methods like this, but it saves space and potentially a *lot* of time, so it can make a huge difference even if you only apply it to a few classes.
tom
--
VENN DIAGRAM THAT LOOK LIKE TWO BIG CIRCLES EQUAL BAD PUBLIC POLICY.
- References:
- Serialization Slow?
- From: Jason Cavett
- Re: Serialization Slow?
- From: Tom Anderson
- Re: Serialization Slow?
- From: Jason Cavett
- Serialization Slow?
- Prev by Date: Generics and for each
- Next by Date: Re: Generics and for each
- Previous by thread: Re: Serialization Slow?
- Next by thread: Re: Serialization Slow?
- Index(es):
Relevant Pages
|