/** * Creates a new predictor with the specified parameters. * * @param minimum the inclusive lower bound of the expected buffer size * @param initial the initial buffer size when no feed back was received * @param maximum the inclusive upper bound of the expected buffer size */ public AdaptiveBufferSizePredictor(int minimum, int initial, int maximum) { if (minimum <= 0) throw new IllegalArgumentException("minimum: " + minimum); if (initial < minimum) throw new IllegalArgumentException("initial: " + initial); if (maximum < initial) throw new IllegalArgumentException("maximum: " + maximum); int minIndex = getSizeTableIndex(minimum); if (SIZE_TABLE[minIndex] < minimum) this.minIndex = minIndex + 1; else this.minIndex = minIndex; int maxIndex = getSizeTableIndex(maximum); if (SIZE_TABLE[maxIndex] > maximum) this.maxIndex = maxIndex - 1; else this.maxIndex = maxIndex; index = getSizeTableIndex(initial); nextBufferSize = SIZE_TABLE[index]; }
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; } } }
/** * 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())); } }