Skip to content

Commit

Permalink
Measuring sizes and counts
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Dec 27, 2023
1 parent e0b27a2 commit 634e9fb
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.function.Function;
Expand All @@ -13,6 +15,7 @@ final class PerGenerator {
static final byte[] HEADER = new byte[] {0x0a, 0x0d, 0x02, 0x0f};
private final OutputStream main;
private final Map<Object, Integer> knownObjects = new IdentityHashMap<>();
private final Map<Class, int[]> knownTypes = new HashMap<>();
final Function<Object, Object> writeReplace;
private int position;

Expand All @@ -36,6 +39,23 @@ static byte[] writeObject(Object obj, Function<Object, Object> writeReplace) thr
arr[9] = (byte) ((at >> 16) & 0xff);
arr[10] = (byte) ((at >> 8) & 0xff);
arr[11] = (byte) (at & 0xff);

var counts = g.knownTypes;
var list = new ArrayList<>(counts.entrySet());
list.sort(
(a, b) -> {
return a.getValue()[0] - b.getValue()[0];
});

for (var i = 0; i < list.size(); i++) {
if (i == 30) {
break;
}
var elem = list.get(list.size() - 1 - i);
System.err.println(
" " + elem.getValue()[0] + " " + elem.getValue()[1] + " " + elem.getKey().getName());
}

return arr;
}

Expand Down Expand Up @@ -78,6 +98,13 @@ final void writeIndirect(Object obj, Persistance.Output out) throws IOException
main.write(arr);
position += arr.length;
knownObjects.put(obj, found);
var c = knownTypes.get(obj.getClass());
if (c == null) {
c = new int[2];
knownTypes.put(obj.getClass(), c);
}
c[0] += arr.length;
c[1]++;
}
out.writeInt(found);
out.writeInt(p.id);
Expand Down

0 comments on commit 634e9fb

Please sign in to comment.