mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-12 08:37:25 -05:00
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:
@@ -117,7 +117,7 @@ public class Broadcaster<T> implements Listener<T>
|
||||
*/
|
||||
public void removeListener(Listener<T> listener)
|
||||
{
|
||||
if(listener != null && mListeners.contains(listener))
|
||||
if(listener != null)
|
||||
{
|
||||
mListeners.remove(listener);
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ import java.nio.ByteOrder;
|
||||
public class ComplexShortAdapter extends ComplexSampleAdapter
|
||||
{
|
||||
private final static Logger mLog = LoggerFactory.getLogger(ComplexShortAdapter.class);
|
||||
private ShortToFloatMap mMap = new ShortToFloatMap();
|
||||
private ByteOrder mByteOrder = ByteOrder.LITTLE_ENDIAN;
|
||||
private ByteBuffer mByteBuffer;
|
||||
|
||||
@@ -57,7 +56,7 @@ public class ComplexShortAdapter extends ComplexSampleAdapter
|
||||
|
||||
while(mByteBuffer.hasRemaining())
|
||||
{
|
||||
convertedSamples[pointer] = mMap.get(mByteBuffer.getShort());
|
||||
convertedSamples[pointer] = (float) mByteBuffer.getShort() / 32768.0f;
|
||||
pointer++;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,6 @@ import java.nio.ByteOrder;
|
||||
public class RealChannelShortAdapter extends RealSampleAdapter
|
||||
{
|
||||
private final static Logger mLog = LoggerFactory.getLogger(RealChannelShortAdapter.class);
|
||||
private ShortToFloatMap mMap = new ShortToFloatMap();
|
||||
private ByteOrder mByteOrder = ByteOrder.LITTLE_ENDIAN;
|
||||
private MixerChannel mMixerChannel;
|
||||
private ByteBuffer mByteBuffer;
|
||||
@@ -62,7 +61,7 @@ public class RealChannelShortAdapter extends RealSampleAdapter
|
||||
{
|
||||
if(mMixerChannel == MixerChannel.LEFT)
|
||||
{
|
||||
convertedSamples[pointer++] = mMap.get(mByteBuffer.getShort());
|
||||
convertedSamples[pointer++] = (float) mByteBuffer.getShort() / 32768.0f;
|
||||
|
||||
//Throw away the right channel
|
||||
mByteBuffer.getShort();
|
||||
@@ -72,7 +71,7 @@ public class RealChannelShortAdapter extends RealSampleAdapter
|
||||
//Throw away the left channel
|
||||
mByteBuffer.getShort();
|
||||
|
||||
convertedSamples[pointer++] = mMap.get(mByteBuffer.getShort());
|
||||
convertedSamples[pointer++] = (float) mByteBuffer.getShort() / 32768.0f;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,6 @@ import java.nio.ByteOrder;
|
||||
public class RealShortAdapter extends RealSampleAdapter
|
||||
{
|
||||
private ByteBuffer mByteBuffer;
|
||||
private ShortToFloatMap mMap = new ShortToFloatMap();
|
||||
private ByteOrder mByteOrder = ByteOrder.LITTLE_ENDIAN;
|
||||
|
||||
/**
|
||||
@@ -54,7 +53,7 @@ public class RealShortAdapter extends RealSampleAdapter
|
||||
|
||||
while(mByteBuffer.hasRemaining())
|
||||
{
|
||||
convertedSamples[pointer] = mMap.get(mByteBuffer.getShort());
|
||||
convertedSamples[pointer] = (float) mByteBuffer.getShort() / 32768.0f;
|
||||
pointer++;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package io.github.dsheirer.sample.adapter;
|
||||
|
||||
public class ShortToFloatMap
|
||||
{
|
||||
private static float[] MAP = new float[ 65536 ];
|
||||
|
||||
static
|
||||
{
|
||||
for( int x = 0; x < 65536; x++ )
|
||||
{
|
||||
MAP[ x] = (float)( x - 32768 ) / 32768.0f;
|
||||
}
|
||||
}
|
||||
|
||||
public float get( short value )
|
||||
{
|
||||
return MAP[ 32768 + value ];
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
package io.github.dsheirer.sample.complex;
|
||||
|
||||
import org.apache.commons.math3.util.FastMath;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -213,12 +214,12 @@ public class Complex implements Serializable
|
||||
*/
|
||||
public float magnitude()
|
||||
{
|
||||
return (float)Math.sqrt(magnitudeSquared());
|
||||
return (float)FastMath.sqrt(magnitudeSquared());
|
||||
}
|
||||
|
||||
public static float magnitude(float inphase, float quadrature)
|
||||
{
|
||||
return (float)Math.sqrt((inphase * inphase) + (quadrature * quadrature));
|
||||
return (float) FastMath.sqrt((inphase * inphase) + (quadrature * quadrature));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -266,8 +267,8 @@ public class Complex implements Serializable
|
||||
*/
|
||||
public void normalizeFast()
|
||||
{
|
||||
float inphaseAbsolute = Math.abs(inphase());
|
||||
float quadratureAbsolute = Math.abs(quadrature());
|
||||
float inphaseAbsolute = FastMath.abs(inphase());
|
||||
float quadratureAbsolute = FastMath.abs(quadrature());
|
||||
|
||||
float gain;
|
||||
|
||||
@@ -303,7 +304,7 @@ public class Complex implements Serializable
|
||||
*/
|
||||
public float inPhaseAbsolute()
|
||||
{
|
||||
return Math.abs(mLeft);
|
||||
return FastMath.abs(mLeft);
|
||||
}
|
||||
|
||||
public float quadrature()
|
||||
@@ -316,7 +317,7 @@ public class Complex implements Serializable
|
||||
*/
|
||||
public float quadratureAbsolute()
|
||||
{
|
||||
return Math.abs(mRight);
|
||||
return FastMath.abs(mRight);
|
||||
}
|
||||
|
||||
public float x()
|
||||
@@ -339,7 +340,7 @@ public class Complex implements Serializable
|
||||
*/
|
||||
public float realAbsolute()
|
||||
{
|
||||
return Math.abs(mLeft);
|
||||
return FastMath.abs(mLeft);
|
||||
}
|
||||
|
||||
public float imaginary()
|
||||
@@ -352,7 +353,7 @@ public class Complex implements Serializable
|
||||
*/
|
||||
public float imaginaryAbsolute()
|
||||
{
|
||||
return Math.abs(mRight);
|
||||
return FastMath.abs(mRight);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -360,14 +361,7 @@ public class Complex implements Serializable
|
||||
*/
|
||||
public float maximumAbsolute()
|
||||
{
|
||||
if(Math.abs(mLeft) > Math.abs(mRight))
|
||||
{
|
||||
return Math.abs(mLeft);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Math.abs(mRight);
|
||||
}
|
||||
return FastMath.max(FastMath.abs(mLeft), FastMath.abs(mRight));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -379,7 +373,7 @@ public class Complex implements Serializable
|
||||
*/
|
||||
public static Complex fromAngle(double angle)
|
||||
{
|
||||
return new Complex((float)Math.cos(angle), (float)Math.sin(angle));
|
||||
return new Complex((float)FastMath.cos(angle), (float)FastMath.sin(angle));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -388,8 +382,8 @@ public class Complex implements Serializable
|
||||
*/
|
||||
public void setAngle(double angle)
|
||||
{
|
||||
setInphase((float)Math.cos(angle));
|
||||
setQuadrature((float)Math.sin(angle));
|
||||
setInphase((float)FastMath.cos(angle));
|
||||
setQuadrature((float)FastMath.sin(angle));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -397,7 +391,7 @@ public class Complex implements Serializable
|
||||
*/
|
||||
public float angle()
|
||||
{
|
||||
return (float)Math.atan2(y(), x());
|
||||
return (float)FastMath.atan2(y(), x());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -406,7 +400,7 @@ public class Complex implements Serializable
|
||||
*/
|
||||
public double angleDegrees()
|
||||
{
|
||||
return Math.toDegrees(Math.atan2(y(), x()));
|
||||
return FastMath.toDegrees(FastMath.atan2(y(), x()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -414,7 +408,7 @@ public class Complex implements Serializable
|
||||
*/
|
||||
public double polarAngleDegrees()
|
||||
{
|
||||
double angle = Math.toDegrees(Math.atan2(y(), x()));
|
||||
double angle = FastMath.toDegrees(FastMath.atan2(y(), x()));
|
||||
return (450.0 - angle) % 360.0;
|
||||
}
|
||||
|
||||
@@ -449,7 +443,7 @@ public class Complex implements Serializable
|
||||
*/
|
||||
public float polarAngle()
|
||||
{
|
||||
return (float)Math.toDegrees(angle());
|
||||
return (float)FastMath.toDegrees(angle());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -462,8 +456,8 @@ public class Complex implements Serializable
|
||||
|
||||
public static float envelope(float inphase, float quadrature)
|
||||
{
|
||||
float inphaseAbsolute = Math.abs(inphase);
|
||||
float quadratureAbsolute = Math.abs(quadrature);
|
||||
float inphaseAbsolute = FastMath.abs(inphase);
|
||||
float quadratureAbsolute = FastMath.abs(quadrature);
|
||||
|
||||
if(inphaseAbsolute > quadratureAbsolute)
|
||||
{
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
package io.github.dsheirer.sample.complex;
|
||||
|
||||
|
||||
import org.apache.commons.math3.util.FastMath;
|
||||
|
||||
public class ComplexSampleUtils
|
||||
{
|
||||
|
||||
@@ -37,7 +39,7 @@ public class ComplexSampleUtils
|
||||
|
||||
public static double magnitude( Complex sample )
|
||||
{
|
||||
return Math.sqrt( magnitudeSquared( sample ) );
|
||||
return FastMath.sqrt( magnitudeSquared( sample ) );
|
||||
}
|
||||
|
||||
public static int magnitudeSquared( Complex sample )
|
||||
|
||||
Reference in New Issue
Block a user