Skip to content

Commit

Permalink
break cycles by registering the object _before_ it is written
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Mar 9, 2024
1 parent 8b700be commit 771e359
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ final <T> int writeObject(T t) throws IOException {
java.lang.Object obj = writeReplace.apply(t);
java.lang.Integer found = knownObjects.get(obj);
if (found == null) {
found = this.position;
knownObjects.put(obj, found);

org.enso.persist.Persistance<?> p = map.forType(obj.getClass());
java.io.ByteArrayOutputStream os = new ByteArrayOutputStream();
p.writeInline(obj, new ReferenceOutput(this, os));
found = this.position;
byte[] arr = os.toByteArray();
main.write(arr);
this.position += arr.length;
knownObjects.put(obj, found);
}
return found;
}
Expand All @@ -83,18 +84,18 @@ final void writeIndirect(Object obj, Persistance.Output out) throws IOException
}
java.lang.Integer found = knownObjects.get(obj);
if (found == null) {
found = position;
knownObjects.put(obj, found);

var os = new ByteArrayOutputStream();
var osData = new ReferenceOutput(this, os);
p.writeInline(obj, osData);
found = position;
if (os.size() == 0) {
os.write(0);
}
byte[] arr = os.toByteArray();
main.write(arr);
position += arr.length;
knownObjects.put(obj, found);
if (histogram != null) {
histogram.register(obj.getClass(), arr.length);
}
Expand Down

0 comments on commit 771e359

Please sign in to comment.