From: Tuukka Lehtonen Date: Fri, 9 Mar 2018 11:20:47 +0000 (+0200) Subject: Include cached hash code in TransferableGraph1 extensions map X-Git-Tag: v1.43.0~136^2~551 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F43%2F1543%2F2;p=simantics%2Fplatform.git Include cached hash code in TransferableGraph1 extensions map The cached hashcode is calculated before inserting the cached hash value in the TransferableGraph1.extensions TreeMap. Reading and inspection must be done with this in mind. This cached value is used to optimize platform startup by removing the need to calculate the TransferableGraph1 structure hashcode at runtime which can be quite expensive because the standard databoard Bindings are slow compared to optimal methods. Of course an optimized binding could be used to speed the calculation up dramatically but it still can't beat caching the final (immutable) hashCode value. refs #7806 Change-Id: I9d4fc6549cba567f95b19f80bd15c1ccfbbedf07 --- diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/GraphCompiler.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/GraphCompiler.java index 98852bcef..26663b08a 100644 --- a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/GraphCompiler.java +++ b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/GraphCompiler.java @@ -15,12 +15,11 @@ import java.util.Locale; import org.simantics.databoard.Bindings; import org.simantics.databoard.Files; import org.simantics.databoard.adapter.AdaptException; +import org.simantics.databoard.binding.Binding; import org.simantics.databoard.binding.error.BindingException; -import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; import org.simantics.databoard.binding.mutable.Variant; import org.simantics.databoard.container.DataContainer; import org.simantics.databoard.container.DataContainers; -import org.simantics.databoard.serialization.SerializationException; import org.simantics.graph.compiler.internal.parsing.Parsing; import org.simantics.graph.compiler.internal.procedures.AddConsistsOf; import org.simantics.graph.compiler.internal.procedures.ApplyTemplates; @@ -39,6 +38,7 @@ import org.simantics.graph.compiler.internal.validation.ValidateGraph; import org.simantics.graph.query.CompositeGraph; import org.simantics.graph.query.Paths; import org.simantics.graph.query.TransferableGraphConversion; +import org.simantics.graph.representation.Extensions; import org.simantics.graph.representation.TransferableGraph1; import org.simantics.graph.store.GraphStore; import org.simantics.ltk.FileSource; @@ -62,10 +62,17 @@ public class GraphCompiler { } public static InputStream write(TransferableGraph1 tg) throws BindingException, IOException { - byte[] buffer = DataContainers.writeFile( - new DataContainer("graph", 1, new Variant(TransferableGraph1.BINDING, tg)) - ); - return new ByteArrayInputStream(buffer); + Binding binding = TransferableGraph1.BINDING; + int hashCode = binding.hashValue(tg); + tg.extensions.put(Extensions.CACHED_HASHCODE, new Variant(Bindings.INTEGER, hashCode)); + try { + byte[] buffer = DataContainers.writeFile( + new DataContainer("graph", 1, new Variant(TransferableGraph1.BINDING, tg)) + ); + return new ByteArrayInputStream(buffer); + } finally { + tg.extensions.remove(Extensions.CACHED_HASHCODE); + } } public static CompilationResult compile( @@ -200,9 +207,10 @@ public class GraphCompiler { public static void reportTime(String taskName, long beginTime, long endTime) { StringBuilder sb = new StringBuilder(); - Formatter formatter = new Formatter(sb, Locale.US); - formatter.format("%-25s %8.4f ms", taskName, (endTime - beginTime)*1e-6); - + @SuppressWarnings("resource") + Formatter formatter = new Formatter(sb, Locale.US); + formatter.format("%-25s %8.4f ms", taskName, (endTime - beginTime)*1e-6); + out.println(sb.toString()); } diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/representation/Extensions.java b/bundles/org.simantics.graph/src/org/simantics/graph/representation/Extensions.java index 8a8cb3d56..375fb1416 100644 --- a/bundles/org.simantics.graph/src/org/simantics/graph/representation/Extensions.java +++ b/bundles/org.simantics.graph/src/org/simantics/graph/representation/Extensions.java @@ -2,18 +2,33 @@ package org.simantics.graph.representation; import java.util.TreeMap; +import org.simantics.databoard.Bindings; +import org.simantics.databoard.binding.Binding; import org.simantics.databoard.binding.mutable.Variant; public class Extensions { - + + public static final Binding BINDING = Bindings.getBindingUnchecked(Extensions.class); + + /** + * Used for storing a cached hash code computed from a + * {@link TransferableGraph1} instance without this cached hashcode + * key,value pair in the extensions map. + */ + public final static String CACHED_HASHCODE = "cached.hashCode"; + final public static String CLUSTERING = "clustering"; final public static String CLUSTER_SETS = "clusterSets"; final public static int NO_CLUSTER_SET = -1; final public static int ROOT_LIBRARY_CLUSTER_SET = -1; final public static int INDEX_ROOT_CLUSTER_SET = -2; - - public TreeMap map = new TreeMap(); - + + public TreeMap map; + + public Extensions() { + this.map = new TreeMap<>(); + } + public Extensions(TreeMap map) { this.map = map; }