Skip to content

Commit

Permalink
[jOOQ#1483] Inefficient cloning of default settings using JAXB unmars…
Browse files Browse the repository at this point in the history
…halling

leads to non-negligible overall overhead. Use serialisation instead
(short of a useful XJC clone plugin)
  • Loading branch information
lukaseder committed Jun 3, 2012
1 parent a53d13b commit d7191bd
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions jOOQ/src/main/java/org/jooq/conf/SettingsTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
import static org.jooq.conf.StatementType.PREPARED_STATEMENT;
import static org.jooq.conf.StatementType.STATIC_STATEMENT;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.PreparedStatement;
import java.sql.Statement;

Expand Down Expand Up @@ -151,8 +153,29 @@ public static final Settings defaultSettings() {
* Clone some settings
*/
public static final Settings clone(Settings settings) {
StringWriter xml = new StringWriter();
JAXB.marshal(settings, xml);
return JAXB.unmarshal(new StringReader(xml.toString()), Settings.class);
// [#1483] Benchmarks showed that this is 10x faster
// return (Settings) settings.clone();

// ... than this, which is in turn 5x faster
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(out);
o.writeObject(settings);
o.flush();

ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream i = new ObjectInputStream(in);
return (Settings) i.readObject();
}
catch (Exception ignore) {
throw new RuntimeException(ignore);
}

// ... than this:
// StringWriter xml = new StringWriter();
// JAXB.marshal(settings, xml);
// return JAXB.unmarshal(new StringReader(xml.toString()), Settings.class);

// TODO [#1483] [#1484] Let Settings implement Cloneable for even more speed!
}
}

0 comments on commit d7191bd

Please sign in to comment.