-
-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#323: Improved CLI performance by storing intermediate CLI tree state…
… snapshot as serialized data (using Kryo) and reading that back instead of rebuilding the tree from scratch (saves 1 seconds or roughly 25% of total time)
- Loading branch information
Showing
4 changed files
with
140 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
modules/cli-module/src/main/java/org/simplejavamail/internal/clisupport/CliDataLocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.simplejavamail.internal.clisupport; | ||
|
||
import java.io.File; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
|
||
/** | ||
* This helper makes sure we can load cli.data no matter how the code is run (from mvn, intellij, command line). | ||
*/ | ||
public class CliDataLocator { | ||
public static String determinelocateCLIDataFile() { | ||
// the following is needed bacause this is a project with submodules | ||
// and it changes depending on how the code is executed | ||
if (new File("src/test/resources/log4j2.xml").exists()) { | ||
return "src/main/resources/cli.data"; | ||
} else if (new File("modules/cli-module/src/test/resources/log4j2.xml").exists()) { | ||
return "modules/cli-module/src/main/resources/cli.data"; | ||
} else { | ||
URL resource = CliDataLocator.class.getClassLoader().getResource("log4j2.xml"); | ||
if (resource != null) { | ||
try { | ||
return new File(resource.toURI()).getParentFile().getPath() + "/cli.data"; | ||
} catch (URISyntaxException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
throw new AssertionError("Was unable to locate resources folder. Did you delete log4j2.xml?"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...src/main/java/org/simplejavamail/internal/clisupport/serialization/SerializationUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.simplejavamail.internal.clisupport.serialization; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.io.Input; | ||
import com.esotericsoftware.kryo.io.Output; | ||
import com.esotericsoftware.kryo.serializers.FieldSerializer; | ||
import com.esotericsoftware.kryo.serializers.JavaSerializer; | ||
import com.esotericsoftware.kryo.util.DefaultInstantiatorStrategy; | ||
import de.javakaffee.kryoserializers.UnmodifiableCollectionsSerializer; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.objenesis.strategy.StdInstantiatorStrategy; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Used to serialize attachments of nested Outlook messages. This is needed because outlook-message-parser returns a Java structure from a .msg source, but this conversion is 1-way. An Email object | ||
* represents attachments as DataSources however, so for this we need to serialize back from the java structure to a binary format. This Util does this. | ||
* <br> | ||
* Then for users to obtain the Javastructure again, they must use this util to deserialize the relevant attachment. | ||
* | ||
* @see <a href="https://github.com/bbottema/simple-java-mail/issues/298">GitHub issue #314</a> | ||
*/ | ||
public class SerializationUtil { | ||
|
||
private static final Kryo KRYO = initKryo(); | ||
|
||
@NotNull | ||
private static Kryo initKryo() { | ||
Kryo kryo = new Kryo(); | ||
kryo.setRegistrationRequired(false); | ||
kryo.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); | ||
// final FieldSerializer.FieldSerializerConfig config = new FieldSerializer.FieldSerializerConfig(); | ||
// config.setSerializeTransient(true); | ||
UnmodifiableCollectionsSerializer.registerSerializers(kryo); | ||
return kryo; | ||
} | ||
|
||
private static <T> void registerClassSerializer(final Kryo kryo, final FieldSerializer.FieldSerializerConfig config, Class<T> type) { | ||
kryo.register(type, new FieldSerializer<T>(kryo, type, config)); | ||
} | ||
|
||
private static <T> void registerClassJavaSerializer(final Kryo kryo, Class<T> type) { | ||
kryo.register(type, new JavaSerializer()); | ||
} | ||
|
||
@NotNull | ||
public static byte[] serialize(@NotNull final Object serializable) | ||
throws IOException { | ||
final Output output = new Output(1024, -1); | ||
KRYO.writeClassAndObject(output, serializable); | ||
return output.toBytes(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@NotNull | ||
public static <T> T deserialize(@NotNull final byte[] serialized) | ||
throws IOException { | ||
return (T) KRYO.readClassAndObject(new Input(serialized)); | ||
} | ||
} |