Performance improvements (#746)

* Misc minor performance improvements

* Use more concise loop syntax

* Use more concise Collection sort syntax

* Use StringUtils pad methods

* Optimize some loops and double-map-lookups

* Optimize array instantiation

There are two styles to convert a collection to an array: either
using a pre-sized array (like c.toArray(new String[c.size()])) or
using an empty array (like c.toArray(new String[0]).

In older Java versions using pre-sized array was recommended,
as the reflection call which is necessary to create an array
of proper size was quite slow. However since late updates of
OpenJDK 6 this call was intrinsified, making the performance
of the empty array version the same and sometimes even better,
compared to the pre-sized version. Also passing pre-sized array
is dangerous for a concurrent or synchronized collection as
a data race is possible between the size and toArray call
which may result in extra nulls at the end of the array,
if the collection was concurrently shrunk during the operation.

* Cleanup string concatenation

* Use EnumMap where possible

* Use FastMath instead of Math

* Initialize complex things only once

* Reduce method size to aloow more JIT inlining

* remove map of short->float

* Move field to local variable

* Cleanup Map lookup

Co-authored-by: Rob Ekl <xwq876@zebra.com>
This commit is contained in:
robekl
2020-04-25 07:57:56 -04:00
committed by GitHub
parent 649258bd2b
commit 39f6d6c831
186 changed files with 2840 additions and 3464 deletions

View File

@@ -26,6 +26,7 @@ import io.github.dsheirer.dsp.psk.pll.IPhaseLockedLoop;
import io.github.dsheirer.sample.Listener;
import io.github.dsheirer.sample.buffer.ReusableComplexBuffer;
import io.github.dsheirer.sample.complex.Complex;
import org.apache.commons.math3.util.FastMath;
public class DQPSKDecisionDirectedDemodulatorInstrumented extends DQPSKDecisionDirectedDemodulator
{
@@ -102,7 +103,7 @@ public class DQPSKDecisionDirectedDemodulatorInstrumented extends DQPSKDecisionD
{
double loopFrequency = ((CostasLoop)getPLL()).getLoopFrequency();
loopFrequency *= mSampleRate / (2.0 * Math.PI);
loopFrequency *= mSampleRate / (2.0 * FastMath.PI);
mPLLFrequencyListener.receive(loopFrequency);
}

View File

@@ -17,13 +17,14 @@ package io.github.dsheirer.dsp.psk;
import io.github.dsheirer.dsp.symbol.Dibit;
import io.github.dsheirer.sample.complex.Complex;
import org.apache.commons.math3.util.FastMath;
public class DQPSKDecisionDirectedSymbolEvaluator implements IPSKSymbolEvaluator<Dibit>
{
private static final Complex ROTATE_FROM_PLUS_135 = Complex.fromAngle(-3.0 * Math.PI / 4.0);
private static final Complex ROTATE_FROM_PLUS_45 = Complex.fromAngle(-1.0 * Math.PI / 4.0);
private static final Complex ROTATE_FROM_MINUS_45 = Complex.fromAngle(1.0 * Math.PI / 4.0);
private static final Complex ROTATE_FROM_MINUS_135 = Complex.fromAngle(3.0 * Math.PI / 4.0);
private static final Complex ROTATE_FROM_PLUS_135 = Complex.fromAngle(-3.0 * FastMath.PI / 4.0);
private static final Complex ROTATE_FROM_PLUS_45 = Complex.fromAngle(-1.0 * FastMath.PI / 4.0);
private static final Complex ROTATE_FROM_MINUS_45 = Complex.fromAngle(1.0 * FastMath.PI / 4.0);
private static final Complex ROTATE_FROM_MINUS_135 = Complex.fromAngle(3.0 * FastMath.PI / 4.0);
private Complex mEvaluationSymbol = new Complex(0, 0);
private float mPhaseError = 0.0f;

View File

@@ -26,6 +26,7 @@ import io.github.dsheirer.dsp.psk.pll.IPhaseLockedLoop;
import io.github.dsheirer.sample.Listener;
import io.github.dsheirer.sample.buffer.ReusableComplexBuffer;
import io.github.dsheirer.sample.complex.Complex;
import org.apache.commons.math3.util.FastMath;
public class DQPSKGardnerDemodulatorInstrumented extends DQPSKGardnerDemodulator
{
@@ -99,7 +100,7 @@ public class DQPSKGardnerDemodulatorInstrumented extends DQPSKGardnerDemodulator
{
double loopFrequency = ((CostasLoop)getPLL()).getLoopFrequency();
loopFrequency *= mSampleRate / (2.0 * Math.PI);
loopFrequency *= mSampleRate / (2.0 * FastMath.PI);
mPLLFrequencyListener.receive(loopFrequency);
}

View File

@@ -17,13 +17,14 @@ package io.github.dsheirer.dsp.psk;
import io.github.dsheirer.dsp.symbol.Dibit;
import io.github.dsheirer.sample.complex.Complex;
import org.apache.commons.math3.util.FastMath;
public class DQPSKGardnerSymbolEvaluator implements IPSKSymbolEvaluator<Dibit>
{
private static final Complex ROTATE_FROM_PLUS_135 = Complex.fromAngle(-3.0 * Math.PI / 4.0);
private static final Complex ROTATE_FROM_PLUS_45 = Complex.fromAngle(-1.0 * Math.PI / 4.0);
private static final Complex ROTATE_FROM_MINUS_45 = Complex.fromAngle(1.0 * Math.PI / 4.0);
private static final Complex ROTATE_FROM_MINUS_135 = Complex.fromAngle(3.0 * Math.PI / 4.0);
private static final Complex ROTATE_FROM_PLUS_135 = Complex.fromAngle(-3.0 * FastMath.PI / 4.0);
private static final Complex ROTATE_FROM_PLUS_45 = Complex.fromAngle(-1.0 * FastMath.PI / 4.0);
private static final Complex ROTATE_FROM_MINUS_45 = Complex.fromAngle(1.0 * FastMath.PI / 4.0);
private static final Complex ROTATE_FROM_MINUS_135 = Complex.fromAngle(3.0 * FastMath.PI / 4.0);
private float mPhaseError = 0.0f;
private float mTimingError = 0.0f;

View File

@@ -16,6 +16,7 @@
package io.github.dsheirer.dsp.psk;
import io.github.dsheirer.sample.complex.Complex;
import org.apache.commons.math3.util.FastMath;
public class GardnerDetector
{
@@ -41,8 +42,8 @@ public class GardnerDetector
*/
public float getError(Complex previous, Complex middle, Complex current)
{
mErrorInphase = (Math.abs(previous.inphase()) - Math.abs(current.inphase())) * Math.abs(middle.inphase());
mErrorQuadrature = (Math.abs(previous.quadrature()) - Math.abs(current.quadrature())) * Math.abs(middle.quadrature());
mErrorInphase = (FastMath.abs(previous.inphase()) - FastMath.abs(current.inphase())) * FastMath.abs(middle.inphase());
mErrorQuadrature = (FastMath.abs(previous.quadrature()) - FastMath.abs(current.quadrature())) * FastMath.abs(middle.quadrature());
mError = normalize(mErrorInphase + mErrorQuadrature, 1.0f);
return mError;

View File

@@ -23,6 +23,7 @@ package io.github.dsheirer.dsp.psk;
import io.github.dsheirer.dsp.filter.interpolator.RealInterpolator;
import io.github.dsheirer.sample.complex.Complex;
import org.apache.commons.math3.util.FastMath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -60,7 +61,7 @@ public class InterpolatingSampleBuffer
mDetectedSamplesPerSymbol = samplesPerSymbol;
mMaximumSamplesPerSymbol = samplesPerSymbol * (1.0f + MAXIMUM_DEVIATION_SAMPLES_PER_SYMBOL);
mMinimumSamplesPerSymbol = samplesPerSymbol * (1.0f - MAXIMUM_DEVIATION_SAMPLES_PER_SYMBOL);
mTwiceSamplesPerSymbol = (int)Math.floor(2.0 * samplesPerSymbol);
mTwiceSamplesPerSymbol = (int) FastMath.floor(2.0 * samplesPerSymbol);
mDelayLineInphase = new float[2 * mTwiceSamplesPerSymbol];
mDelayLineQuadrature = new float[2 * mTwiceSamplesPerSymbol];
@@ -190,7 +191,7 @@ public class InterpolatingSampleBuffer
}
else
{
int offset = (int)Math.floor(interpolation);
int offset = (int)FastMath.floor(interpolation);
return mInterpolator.filter(mDelayLineInphase, mDelayLinePointer + offset, interpolation - offset);
}
}
@@ -208,7 +209,7 @@ public class InterpolatingSampleBuffer
}
else
{
int offset = (int)Math.floor(interpolation);
int offset = (int)FastMath.floor(interpolation);
return mInterpolator.filter(mDelayLineQuadrature, mDelayLinePointer + offset, interpolation - offset);
}
}

View File

@@ -23,6 +23,7 @@ package io.github.dsheirer.dsp.psk;
import io.github.dsheirer.sample.complex.Complex;
import io.github.dsheirer.sample.complex.ComplexSampleListener;
import org.apache.commons.math3.util.FastMath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -36,7 +37,7 @@ public class InterpolatingSampleBufferInstrumented extends InterpolatingSampleBu
public InterpolatingSampleBufferInstrumented(float samplesPerSymbol, float symbolTimingGain)
{
super(samplesPerSymbol, symbolTimingGain);
mBufferLength = (int)Math.ceil(samplesPerSymbol);
mBufferLength = (int) FastMath.ceil(samplesPerSymbol);
mSymbolDecisionData = new SymbolDecisionData(mBufferLength);
}

View File

@@ -19,6 +19,7 @@ import io.github.dsheirer.dsp.symbol.Dibit;
import io.github.dsheirer.sample.Broadcaster;
import io.github.dsheirer.sample.Listener;
import io.github.dsheirer.sample.complex.Complex;
import org.apache.commons.math3.util.FastMath;
public class QPSKSymbolDecoder implements Listener<Complex>, IQPSKSymbolDecoder
{
@@ -63,7 +64,7 @@ public class QPSKSymbolDecoder implements Listener<Complex>, IQPSKSymbolDecoder
public Dibit decode(Complex complex )
{
if( Math.abs( complex.inphase() ) > Math.abs( complex.quadrature() ) )
if( FastMath.abs( complex.inphase() ) > FastMath.abs( complex.quadrature() ) )
{
if( complex.inphase() > 0 )
{

View File

@@ -16,6 +16,7 @@
package io.github.dsheirer.dsp.psk;
import io.github.dsheirer.sample.complex.Complex;
import org.apache.commons.math3.util.FastMath;
/**
* Calculates the phase error for a QPSK polar-orientation constellation
@@ -29,7 +30,7 @@ public class QPSKSymbolPhaseErrorCalculator implements ISymbolPhaseErrorCalculat
{
mPhaseError = 0.0f;
if(Math.abs(symbol.inphase()) > Math.abs(symbol.quadrature()))
if(FastMath.abs(symbol.inphase()) > FastMath.abs(symbol.quadrature()))
{
if(symbol.inphase() > 0)
{

View File

@@ -23,11 +23,12 @@ package io.github.dsheirer.dsp.psk;
import io.github.dsheirer.buffer.ComplexCircularBuffer;
import io.github.dsheirer.sample.complex.Complex;
import org.apache.commons.math3.util.FastMath;
public class SymbolDecisionData
{
//45 degrees rotation to orient the symbol to a polar axis to make error calculation easy/efficient
public static final Complex DIFFERENTIAL_OFFSET = Complex.fromAngle(Math.PI / 4.0d);
public static final Complex DIFFERENTIAL_OFFSET = Complex.fromAngle(FastMath.PI / 4.0d);
private ComplexCircularBuffer mBuffer;
private float mSamplingPoint;

View File

@@ -15,11 +15,13 @@
******************************************************************************/
package io.github.dsheirer.dsp.psk;
import org.apache.commons.math3.util.FastMath;
public enum SymbolInversion
{
DEGREES_90_PLUS(Math.PI / 2.0),
DEGREES_90_MINUS(Math.PI / -2.0),
DEGREES_180(Math.PI);
DEGREES_90_PLUS(FastMath.PI / 2.0),
DEGREES_90_MINUS(FastMath.PI / -2.0),
DEGREES_180(FastMath.PI);
private double mAngleRadians;

View File

@@ -22,6 +22,7 @@
package io.github.dsheirer.dsp.psk.pll;
import io.github.dsheirer.sample.complex.Complex;
import org.apache.commons.math3.util.FastMath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -38,13 +39,13 @@ public class CostasLoop implements IPhaseLockedLoop
{
private final static Logger mLog = LoggerFactory.getLogger(CostasLoop.class);
public static final double TWO_PI = 2.0 * Math.PI;
public static final double TWO_PI = 2.0 * FastMath.PI;
private Complex mCurrentVector = new Complex(0, 0);
private double mLoopPhase = 0.0;
private double mLoopFrequency = 0.0;
private double mMaximumLoopFrequency;
private double mDamping = Math.sqrt(2.0) / 2.0;
private double mDamping = FastMath.sqrt(2.0) / 2.0;
private double mAlphaGain;
private double mBetaGain;
private PLLBandwidth mPLLBandwidth = PLLBandwidth.BW_400;

View File

@@ -86,9 +86,9 @@ public enum PLLBandwidth
*/
public static PLLBandwidth fromSyncCount(int syncCount)
{
if(LOOKUP_MAP.containsKey(syncCount))
{
return LOOKUP_MAP.get(syncCount);
PLLBandwidth mapValue = LOOKUP_MAP.get(syncCount);
if (mapValue != null) {
return mapValue;
}
return BW_200;

View File

@@ -70,9 +70,9 @@ public class QPSKPhaseInversionCalculator
}
System.out.println("NORMAL: " + sync);
System.out.println(" +90: " + sbP90.toString());
System.out.println(" -90: " + sbM90.toString());
System.out.println(" 180: " + sb180.toString());
System.out.println(" +90: " + sbP90);
System.out.println(" -90: " + sbM90);
System.out.println(" 180: " + sb180);
}
}