Configurable Kryo pool size.
- Add constructor to specify initial capacity
- Add warning comment about registration
Change-Id: I9b7a04ff319fbf44bd2b4c04e3f77913eb157105
diff --git a/src/main/java/net/onrc/onos/ofcontroller/util/serializers/KryoFactory.java b/src/main/java/net/onrc/onos/ofcontroller/util/serializers/KryoFactory.java
index 68e34fe..9eb4e10 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/util/serializers/KryoFactory.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/util/serializers/KryoFactory.java
@@ -51,18 +51,30 @@
* serialization/deserialization of classes.
*/
public class KryoFactory {
+ private static final int DEFAULT_PREALLOCATIONS = 100;
private ArrayList<Kryo> kryoList = new ArrayList<Kryo>();
/**
* Default constructor.
+ *
+ * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances.
*/
public KryoFactory() {
- Kryo kryo;
- // Preallocate
- for (int i = 0; i < 100; i++) {
- kryo = newKryoImpl();
- kryoList.add(kryo);
- }
+ this(DEFAULT_PREALLOCATIONS);
+ }
+
+ /**
+ * Constructor to explicitly specify number of Kryo instances to pool
+ *
+ * @param initialCapacity number of Kryo instance to preallocate
+ */
+ public KryoFactory(final int initialCapacity) {
+ // Preallocate
+ kryoList.ensureCapacity(initialCapacity);
+ for (int i = 0; i < initialCapacity; i++) {
+ Kryo kryo = newKryoImpl();
+ kryoList.add(kryo);
+ }
}
/**
@@ -118,6 +130,12 @@
private Kryo newKryoImpl() {
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(true);
+ //
+ // WARNING: Order of register() calls affects serialized bytes.
+ // - Do no insert new entry in the middle, always add to the end.
+ // - Do not simply remove existing entry
+ //
+
// kryo.setReferences(false);
//
kryo.register(ArrayList.class);