Skip to content
Snippets Groups Projects
Commit bcb5597b authored by Radek Ošlejšek's avatar Radek Ošlejšek
Browse files

Merge branch '84-implement-faster-de-serialization' into 'master'

Resolve "Implement faster (de)serialization"

Closes #84

See merge request grp-fidentis/analyst2!87
parents 524d2adc a3d62667
No related branches found
No related tags found
No related merge requests found
......@@ -93,6 +93,11 @@
<artifactId>guava</artifactId>
<version>30.1-jre</version>
</dependency>
<dependency>
<groupId>de.ruedigermoeller</groupId>
<artifactId>fst</artifactId>
<version>2.57</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
......
......@@ -7,15 +7,18 @@ import cz.fidentis.analyst.mesh.events.MeshEvent;
import cz.fidentis.analyst.mesh.events.MeshListener;
import cz.fidentis.analyst.mesh.io.MeshObjLoader;
import cz.fidentis.analyst.symmetry.Plane;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Objects;
import org.nustaq.serialization.FSTConfiguration;
import org.nustaq.serialization.FSTObjectInput;
import org.nustaq.serialization.FSTObjectOutput;
/**
* This class encapsulates data for a 3D scan of a single human face.
......@@ -50,6 +53,11 @@ public class HumanFace implements MeshListener, Serializable {
private final String id;
/**
* Fast (de)serialization handler
*/
private static final FSTConfiguration FST_CONF = FSTConfiguration.createDefaultConfiguration();
/**
* Reads a 3D human face from the given OBJ file.
* Use {@link restoreFromFile} to restore the human face from a dump file.
......@@ -154,9 +162,14 @@ public class HumanFace implements MeshListener, Serializable {
public File dumpToFile() throws IOException {
File tempFile = File.createTempFile(this.getClass().getSimpleName(), ".ser");
tempFile.deleteOnExit();
try (ObjectOutputStream fos = new ObjectOutputStream(new FileOutputStream(tempFile))) {
fos.writeObject(this);
//try (ObjectOutputStream fos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(tempFile)))) {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tempFile))) {
FSTObjectOutput out = FST_CONF.getObjectOutput(bos);
out.writeObject(this, HumanFace.class);
out.flush();
}
return tempFile;
}
......@@ -168,9 +181,12 @@ public class HumanFace implements MeshListener, Serializable {
* @throws IOException on error in reading the dump file
* @throws java.lang.ClassNotFoundException on error when instantiating the human face
*/
public static HumanFace restoreFromFile(File dumpFile) throws IOException, ClassNotFoundException {
try (ObjectInputStream fos = new ObjectInputStream(new FileInputStream(dumpFile))) {
return (HumanFace) fos.readObject();
public static HumanFace restoreFromFile(File dumpFile) throws IOException, ClassNotFoundException, Exception {
//try (ObjectInputStream fos = new ObjectInputStream(new BufferedInputStream(new FileInputStream(dumpFile)))) {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(dumpFile))) {
FSTObjectInput in = FST_CONF.getObjectInput(bis);
HumanFace face = (HumanFace) in.readObject(HumanFace.class);
return face;
}
}
......
......@@ -33,7 +33,7 @@ public abstract class HumanFaceCache {
/**
* Keep at least this portion of the Java heap memory free
*/
public static final double MIN_FREE_MEMORY = 0.05; // 5%
public static final double MIN_FREE_MEMORY = 0.2; // 20%
private final Map<String, HumanFace> inMemoryFaces = new HashMap<>();
private final Map<String, File> dumpedFaces = new HashMap<>();
......
......@@ -38,7 +38,7 @@ public class EfficiencyTests {
* @param args Input arguments
* @throws IOException on IO error
*/
public static void main(String[] args) throws IOException, ClassNotFoundException {
public static void main(String[] args) throws IOException, ClassNotFoundException, Exception {
face1 = printFaceLoad(faceFile4);
face2 = printFaceLoad(faceFile4);
......@@ -46,15 +46,15 @@ public class EfficiencyTests {
face2.getMeshModel().simplifyModel();
for (int i = 0; i < 10; i++) {
HumanFace face = HumanFacePrivilegedCache.instance().loadFace(faceFile4);
face.getMeshModel().simplifyModel();
System.out.println(i+".\t" + HumanFacePrivilegedCache.instance());
//HumanFace face = HumanFacePrivilegedCache.instance().loadFace(faceFile4);
//face.getMeshModel().simplifyModel();
//System.out.println(i+".\t" + HumanFacePrivilegedCache.instance());
printFaceDump(face1);
}
boolean relativeDist = false;
boolean printDetails = false;
printFaceDump(face1);
//face1.getMeshModel().compute(new GaussianCurvature()); // initialize everything, then measure
......@@ -129,7 +129,7 @@ public class EfficiencyTests {
);
}
private static void printFaceDump(HumanFace face) throws IOException, ClassNotFoundException {
private static void printFaceDump(HumanFace face) throws IOException, ClassNotFoundException, Exception {
long startTime = System.currentTimeMillis();
File file = face.dumpToFile();
long endTime = System.currentTimeMillis();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment