From 28c58cb8fe50f12b6d4948e796d1d4b0354a6f43 Mon Sep 17 00:00:00 2001 From: Etienne CHAMPETIER Date: Thu, 7 Apr 2016 09:16:44 +0200 Subject: [PATCH] OveruseEstimator replace LinkedList with double[] this avoid a lot of autoboxing, and is more efficient Signed-off-by: Etienne CHAMPETIER --- .../OveruseEstimator.java | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/src/org/jitsi/impl/neomedia/rtp/remotebitrateestimator/OveruseEstimator.java b/src/org/jitsi/impl/neomedia/rtp/remotebitrateestimator/OveruseEstimator.java index 719542c29..9338c12d8 100644 --- a/src/org/jitsi/impl/neomedia/rtp/remotebitrateestimator/OveruseEstimator.java +++ b/src/org/jitsi/impl/neomedia/rtp/remotebitrateestimator/OveruseEstimator.java @@ -86,7 +86,17 @@ private static double[][] clone(double[][] matrix) private double slope; - private final List tsDeltaHist = new LinkedList<>(); + /** + * Store the tsDelta history into a {@code double[]} used as a circular buffer + * Original c++ code uses std::list but in Java this translate + * to {@code List} which causes a lot of autoboxing + */ + private final double[] tsDeltaHist = new double[kMinFramePeriodHistoryLength]; + + /** + * Index to insert next value into {@link tsDeltaHist} + */ + private int tsDeltaHistInsIdx; private double varNoise; @@ -99,6 +109,11 @@ public OveruseEstimator(OverUseDetectorOptions options) varNoise = options.initialVarNoise; E = clone(options.initialE); processNoise = options.initialProcessNoise.clone(); + /** + * Initialize {@link tsDeltaHist} with {@code Double.MAX_VALUE} + * to simplify {@link updateMinFramePeriod} + */ + Arrays.fill(tsDeltaHist, Double.MAX_VALUE); } /** @@ -229,14 +244,24 @@ public void update( private double updateMinFramePeriod(double tsDelta) { - double minFramePeriod = tsDelta; - - if (tsDeltaHist.size() >= kMinFramePeriodHistoryLength) - tsDeltaHist.remove(0); - for (Double d : tsDeltaHist) - minFramePeriod = Math.min(d, minFramePeriod); - tsDeltaHist.add(tsDelta); - return minFramePeriod; + /** + * Change from C++ version: + * We use {@link tsDeltaHist} as a circular buffer initialized + * with {@code Double.MAX_VALUE}, so we insert new {@link tsDelta} + * value at {@link tsDeltaHistInsIdx} and we search for the + * minimum value in {@link tsDeltaHist} + */ + tsDeltaHist[tsDeltaHistInsIdx] = tsDelta; + tsDeltaHistInsIdx = (tsDeltaHistInsIdx + 1) % tsDeltaHist.length; + + double min = tsDelta; + for (double d : tsDeltaHist) + { + if (d < min) + min = d; + } + + return min; } private void updateNoiseEstimate(