Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@
import org.enso.compiler.pass.analyse.alias.graph.GraphBuilder;
import org.enso.compiler.pass.analyse.alias.graph.GraphOccurrence;
import org.enso.persist.Persistance;
import org.junit.BeforeClass;
import org.junit.Test;
import scala.Option;

public class PassPersistanceTest {
@BeforeClass
public static void initializePersistables() {
org.enso.compiler.core.ir.Persistables.initialize();
org.enso.compiler.pass.analyse.Persistables.initialize();
org.enso.compiler.pass.analyse.alias.graph.Persistables.initialize();
}
private static final Persistance.Pool POOL =
Persistance.Pool.merge(
org.enso.compiler.core.ir.Persistables.POOL,
org.enso.compiler.pass.analyse.Persistables.POOL,
org.enso.compiler.pass.analyse.alias.graph.Persistables.POOL);

@Test
public void cachePreferences() throws Exception {
Expand Down Expand Up @@ -81,11 +79,11 @@ private static <T> T serde(Class<T> clazz, T l, int expectedSize) throws IOExcep

private static <T> T serde(Class<T> clazz, T l, int expectedSize, Function<Object, Object> fn)
throws IOException {
var arr = Persistance.write(l, fn);
var arr = POOL.write(l, fn);
if (expectedSize >= 0) {
assertEquals(expectedSize, arr.length - 12);
}
var ref = Persistance.read(arr, null);
var ref = POOL.read(arr, null);
return ref.get(clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.enso.compiler.data.BindingsMap
import org.enso.compiler.phase.exports.ExportsResolution
import org.enso.compiler.pass.analyse.{BindingAnalysis, GatherDiagnostics}
import org.enso.interpreter.runtime
import org.enso.persist.Persistance
import org.enso.pkg.QualifiedName
import org.enso.pkg.Package
import org.enso.common.LanguageInfo
Expand Down Expand Up @@ -1039,7 +1038,7 @@ class ImportExportTest
.toList
.collect({ case w: Warning.DuplicatedImport => w })
warn.size shouldEqual 1
val arr = Persistance.write(
val arr = org.enso.interpreter.caches.PersistUtils.POOL.write(
mainIr,
{
case metadata: ProcessingPass.Metadata =>
Expand Down Expand Up @@ -1080,7 +1079,7 @@ class ImportExportTest
.asInstanceOf[errors.ImportExport.AmbiguousImport]
ambiguousImport.symbolName shouldEqual "A_Type"
try {
val arr = Persistance.write(
val arr = org.enso.interpreter.caches.PersistUtils.POOL.write(
mainIr,
{
case metadata: ProcessingPass.Metadata =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.enso.persist.Persistable;
import org.enso.persist.Persistance;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import scala.Option;
Expand All @@ -26,11 +25,9 @@
import scala.collection.immutable.Seq;

public class IrPersistanceTest {
@BeforeClass
public static void initializeIrPersistance() {
org.enso.compiler.core.ir.Persistables.initialize();
org.enso.compiler.core.Persistables.initialize();
}
private static final Persistance.Pool POOL =
Persistance.Pool.merge(
org.enso.compiler.core.ir.Persistables.POOL, org.enso.compiler.core.Persistables.POOL);

@Before
public void resetDebris() {
Expand Down Expand Up @@ -352,71 +349,68 @@ public void inlineReferenceIsLazy() throws Exception {
@Test
public void readResolve() throws Exception {
var in = new Service(5);
var arr = Persistance.write(in, (Function<Object, Object>) null);
var arr = POOL.write(in, (Function<Object, Object>) null);

var plain = Persistance.read(arr, (Function<Object, Object>) null);
var plain = POOL.read(arr, (Function<Object, Object>) null);
assertEquals("Remains five", 5, plain.get(Service.class).value());

var multiOnRead =
Persistance.read(arr, (obj) -> obj instanceof Service s ? new Service(s.value() * 3) : obj);
POOL.read(arr, (obj) -> obj instanceof Service s ? new Service(s.value() * 3) : obj);
assertEquals("Multiplied on read", 15, multiOnRead.get(Service.class).value());
}

@Test
public void writeReplace() throws Exception {
var in = new Service(5);
var arr =
Persistance.write(in, (obj) -> obj instanceof Service s ? new Service(s.value() * 3) : obj);
var arr = POOL.write(in, (obj) -> obj instanceof Service s ? new Service(s.value() * 3) : obj);

var plain = Persistance.read(arr, (Function<Object, Object>) null);
var plain = POOL.read(arr, (Function<Object, Object>) null);
assertEquals("Multiplied on write", 15, plain.get(Service.class).value());
}

@Test
public void readResolveInline() throws Exception {
var in = new ServiceSupply(new Service(5));
var arr = Persistance.write(in, (Function<Object, Object>) null);
var arr = POOL.write(in, (Function<Object, Object>) null);

var plain = Persistance.read(arr, (Function<Object, Object>) null);
var plain = POOL.read(arr, (Function<Object, Object>) null);
assertEquals("Remains five", 5, plain.get(ServiceSupply.class).supply().value());

var multiOnRead =
Persistance.read(arr, (obj) -> obj instanceof Service s ? new Service(s.value() * 3) : obj);
POOL.read(arr, (obj) -> obj instanceof Service s ? new Service(s.value() * 3) : obj);
assertEquals("Multiplied on read", 15, multiOnRead.get(ServiceSupply.class).supply().value());
}

@Test
public void writeReplaceInline() throws Exception {
var in = new ServiceSupply(new Service(5));
var arr =
Persistance.write(in, (obj) -> obj instanceof Service s ? new Service(s.value() * 3) : obj);
var arr = POOL.write(in, (obj) -> obj instanceof Service s ? new Service(s.value() * 3) : obj);

var plain = Persistance.read(arr, (Function<Object, Object>) null);
var plain = POOL.read(arr, (Function<Object, Object>) null);
assertEquals("Multiplied on write", 15, plain.get(ServiceSupply.class).supply().value());
}

@Test
public void readResolveReference() throws Exception {
var in = new IntegerSupply(new Service(5));
var arr = Persistance.write(in, (Function<Object, Object>) null);
var arr = POOL.write(in, (Function<Object, Object>) null);

var plain = Persistance.read(arr, (Function<Object, Object>) null);
var plain = POOL.read(arr, (Function<Object, Object>) null);
assertEquals("Remains five", 5, (int) plain.get(IntegerSupply.class).supply().get());
assertEquals("Remains five 2", 5, (int) plain.get(IntegerSupply.class).supply().get());

var multiOnRead =
Persistance.read(arr, (obj) -> obj instanceof Service s ? new Service(s.value() * 3) : obj);
POOL.read(arr, (obj) -> obj instanceof Service s ? new Service(s.value() * 3) : obj);
assertEquals(
"Multiplied on read", 15, (int) multiOnRead.get(IntegerSupply.class).supply().get());
}

@Test
public void writeReplaceReference() throws Exception {
var in = new IntegerSupply(new Service(5));
var arr =
Persistance.write(in, (obj) -> obj instanceof Service s ? new Service(s.value() * 3) : obj);
var arr = POOL.write(in, (obj) -> obj instanceof Service s ? new Service(s.value() * 3) : obj);

var plain = Persistance.read(arr, (Function<Object, Object>) null);
var plain = POOL.read(arr, (Function<Object, Object>) null);
assertEquals("Multiplied on write", 15, (int) plain.get(IntegerSupply.class).supply().get());
}

Expand All @@ -436,11 +430,11 @@ private static <T> T serde(Class<T> clazz, T l, int expectedSize) throws IOExcep

private static <T> T serde(Class<T> clazz, T l, int expectedSize, Function<Object, Object> fn)
throws IOException {
var arr = Persistance.write(l, fn);
var arr = POOL.write(l, fn);
if (expectedSize >= 0) {
assertEquals(expectedSize, arr.length - 12);
}
var ref = Persistance.read(arr, null);
var ref = POOL.read(arr, null);
return ref.get(clazz);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public byte[] metadata(String sourceDigest, String blobDigest, CachedBindings en
@Override
public byte[] serialize(EnsoContext context, CachedBindings entry) throws IOException {
var arr =
Persistance.write(
PersistUtils.POOL.write(
entry.bindings(), CacheUtils.writeReplace(context.getCompiler().context(), false));
return arr;
}
Expand All @@ -71,7 +71,7 @@ public byte[] serialize(EnsoContext context, CachedBindings entry) throws IOExce
public CachedBindings deserialize(
EnsoContext context, ByteBuffer data, Metadata meta, TruffleLogger logger)
throws IOException {
var ref = Persistance.read(data, CacheUtils.readResolve(context.getCompiler().context()));
var ref = PersistUtils.POOL.read(data, CacheUtils.readResolve(context.getCompiler().context()));
var bindings = ref.get(MapToBindings.class);
return new CachedBindings(libraryName, bindings, Optional.empty());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.enso.compiler.core.ir.Module;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.persist.Persistance;
import org.enso.version.BuildVersion;

public final class ModuleCache
Expand Down Expand Up @@ -59,7 +58,7 @@ public byte[] metadata(String sourceDigest, String blobDigest, CachedModule entr
@Override
public byte[] serialize(EnsoContext context, CachedModule entry) throws IOException {
var arr =
Persistance.write(
PersistUtils.POOL.write(
entry.moduleIR(), CacheUtils.writeReplace(context.getCompiler().context(), true));
return arr;
}
Expand All @@ -68,7 +67,7 @@ public byte[] serialize(EnsoContext context, CachedModule entry) throws IOExcept
public CachedModule deserialize(
EnsoContext context, ByteBuffer data, Metadata meta, TruffleLogger logger)
throws IOException {
var ref = Persistance.read(data, CacheUtils.readResolve(context.getCompiler().context()));
var ref = PersistUtils.POOL.read(data, CacheUtils.readResolve(context.getCompiler().context()));
var mod = ref.get(Module.class);
return new CachedModule(
mod, CompilationStage.valueOf(meta.compilationStage()), module.getSource());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@
import org.enso.persist.Persistance;
import org.openide.util.lookup.ServiceProvider;

public class PersistUtils {
public final class PersistUtils {
private PersistUtils() {}

/** Pool of persistable instances in the caches. */
public static final Persistance.Pool POOL =
Persistance.Pool.merge(
org.enso.compiler.core.ir.Persistables.POOL,
org.enso.compiler.pass.analyse.Persistables.POOL,
org.enso.compiler.pass.analyse.types.Persistables.POOL,
org.enso.compiler.pass.analyse.alias.graph.Persistables.POOL,
org.enso.interpreter.caches.Persistables.POOL);

@ServiceProvider(service = Persistance.class)
public static final class PersistArrayList extends Persistance<ArrayList> {
public PersistArrayList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.enso.interpreter.caches.SuggestionsCache.CachedSuggestions;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.persist.Persistable;
import org.enso.persist.Persistance;
import org.enso.polyglot.Suggestion;
import org.enso.version.BuildVersion;

Expand Down Expand Up @@ -71,14 +70,15 @@ public byte[] metadata(String sourceDigest, String blobDigest, CachedSuggestions

@Override
public byte[] serialize(EnsoContext context, CachedSuggestions entry) throws IOException {
return Persistance.write(entry, CacheUtils.writeReplace(context.getCompiler().context(), true));
return PersistUtils.POOL.write(
entry, CacheUtils.writeReplace(context.getCompiler().context(), true));
}

@Override
public CachedSuggestions deserialize(
EnsoContext context, ByteBuffer data, Metadata meta, TruffleLogger logger)
throws IOException {
var ref = Persistance.read(data, CacheUtils.readResolve(context.getCompiler().context()));
var ref = PersistUtils.POOL.read(data, CacheUtils.readResolve(context.getCompiler().context()));
var cachedSuggestions = ref.get(CachedSuggestions.class);
return cachedSuggestions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@
* priority. Future rewrites of this class may optimize towards such direction.
*/
final class SerializationPool {
static {
org.enso.compiler.core.ir.Persistables.initialize();
org.enso.compiler.pass.analyse.Persistables.initialize();
org.enso.compiler.pass.analyse.types.Persistables.initialize();
org.enso.compiler.pass.analyse.alias.graph.Persistables.initialize();
org.enso.interpreter.caches.Persistables.initialize();
}

private final TruffleCompilerContext context;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,21 @@
import org.enso.persist.Persistance;
import org.enso.pkg.QualifiedName;
import org.enso.scala.wrapper.ScalaConversions;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* Currently the static type inference pass is optional and it is not computed as part of the cache
* indexing.
*/
public class TypeMetadataPersistanceTest {
@BeforeClass
public static void initializePersistance() {
org.enso.compiler.core.ir.Persistables.initialize();
org.enso.compiler.pass.analyse.types.Persistables.initialize();
}
private static final Persistance.Pool POOL =
Persistance.Pool.merge(
org.enso.compiler.core.ir.Persistables.POOL,
org.enso.compiler.pass.analyse.types.Persistables.POOL);

private static <T> T serde(Class<T> clazz, T l) throws IOException {
var arr = Persistance.write(l, null);
var ref = Persistance.read(arr, null);
var arr = POOL.write(l, null);
var ref = POOL.read(arr, null);
return ref.get(clazz);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,25 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
}
var src = processingEnv.getFiler().createSourceFile(cn);
try (var w = src.openWriter()) {
w.append("package " + entry.getKey() + ";\n");
w.append("public final class Persistables {\n");
w.append(" private Persistables() {}\n");
w.append(" public static void initialize() {\n");
w.append(" }\n");

// values from processor take preceedence
for (var idName : entry.getValue().entrySet()) {
props.setProperty("" + idName.getKey(), idName.getValue());
}

w.append("package " + entry.getKey() + ";\n");
w.append("import org.enso.persist.Persistance;\n");
w.append("public final class Persistables extends Persistance.Pool {\n");
w.append(" public static final Persistance.Pool POOL = new Persistables();\n");
w.append(" private Persistables() {\n");
w.append(" super(\"").append(entry.getKey()).append("\",");
var lineEnding = "\n";
for (var idName : props.entrySet()) {
w.append(
" private static final org.enso.persist.Persistance PERSIST_"
+ idName.getKey()
+ " = new "
+ idName.getValue()
+ "();\n");
w.append(lineEnding);
w.append(" new " + idName.getValue() + "()");
lineEnding = ",\n";
}
w.append("\n );\n");
w.append(" }\n");
w.append("}\n");
}
var out = processingEnv.getFiler().createResource(propsWhere, propsPkg, propsName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,25 @@ final class PerGenerator {
private int position;

private PerGenerator(
OutputStream out, Histogram histogram, int position, Function<Object, Object> writeReplace) {
this.map = PerMap.create();
PerMap map,
OutputStream out,
Histogram histogram,
int position,
Function<Object, Object> writeReplace) {
this.map = map;
this.main = out;
this.writeReplace = writeReplace == null ? Function.identity() : writeReplace;
this.position = position;
this.histogram = histogram;
}

static byte[] writeObject(Object obj, Function<Object, Object> writeReplace) throws IOException {
static byte[] writeObject(PerMap map, Object obj, Function<Object, Object> writeReplace)
throws IOException {
var histogram = PerUtils.LOG.isDebugEnabled() ? new Histogram() : null;

var out = new ByteArrayOutputStream();
var data = new DataOutputStream(out);
var g = new PerGenerator(out, histogram, 12, writeReplace);
var g = new PerGenerator(map, out, histogram, 12, writeReplace);
data.write(PerGenerator.HEADER);
data.writeInt(g.versionStamp());
data.write(new byte[4]); // space
Expand Down
Loading
Loading