/** * Get the serialized form size predictor for a particular type. * * @param type Marshallable type for which serialized form size will be predicted * @return an instance of {@link BufferSizePredictor} */ public BufferSizePredictor getBufferSizePredictor(Class<?> type) { MarshallingType marshallingType = typeHints.get(type); if (marshallingType == null) { // Initialise with isMarshallable to null, meaning it's unknown marshallingType = new MarshallingType(null, new AdaptiveBufferSizePredictor()); MarshallingType prev = typeHints.putIfAbsent(type, marshallingType); if (prev != null) { marshallingType = prev; } else { if (trace) { log.tracef("Cache a buffer size predictor for '%s' assuming " + "its serializability is unknown", type.getName()); } } } return marshallingType.sizePredictor; }
/** * Marks a particular type as being marshallable or not being not marshallable. * * @param type Class to mark as serializable or non-serializable * @param isMarshallable Whether the type can be marshalled or not. */ public void markMarshallable(Class<?> type, boolean isMarshallable) { MarshallingType marshallType = typeHints.get(type); if (marshallableUpdateRequired(isMarshallable, marshallType)) { boolean replaced = typeHints.replace(type, marshallType, new MarshallingType( Boolean.valueOf(isMarshallable), marshallType.sizePredictor)); if (replaced && trace) { log.tracef("Replacing '%s' type to be marshallable=%b", type.getName(), isMarshallable); } } else if (marshallType == null) { if (trace) { log.tracef("Cache '%s' type to be marshallable=%b", type.getName(), isMarshallable); } typeHints.put(type, new MarshallingType( Boolean.valueOf(isMarshallable), new AdaptiveBufferSizePredictor())); } }
public void testAdaptivenesOfBufferSizeChanges() throws Exception { AdaptiveBufferSizePredictor predictor = new AdaptiveBufferSizePredictor(); int size = 32; int nextSize; int prevNextSize = AdaptiveBufferSizePredictor.DEFAULT_INITIAL; for (int i = 0; i < 100; i++) { predictor.recordSize(size); nextSize = predictor.nextSize(null); if (i % 2 != 0) { if ((nextSize * 0.88) < size) break; else { assert nextSize < prevNextSize; prevNextSize = nextSize; } } } size = 32768; for (int i = 0; i < 100; i++) { predictor.recordSize(size); nextSize = predictor.nextSize(null); if ((nextSize * 0.89) > size) { break; } else { assert nextSize > prevNextSize; prevNextSize = nextSize; } } }