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

@@ -33,6 +33,7 @@ package org.jdesktop.swingx;
import jiconfont.icons.font_awesome.FontAwesome;
import jiconfont.swing.IconFontSwing;
import org.apache.commons.math3.util.FastMath;
import org.jdesktop.swingx.mapviewer.GeoPosition;
import org.jdesktop.swingx.mapviewer.Tile;
import org.jdesktop.swingx.mapviewer.TileFactory;
@@ -233,8 +234,8 @@ public class JXMapViewer extends JPanel implements DesignMode
// TilePoint topLeftTile = getTileFactory().getTileCoordinate(
// new Point2D.Double(viewportBounds.x, viewportBounds.y));
TileFactoryInfo info = getTileFactory().getInfo();
int tpx = (int) Math.floor(viewportBounds.getX() / info.getTileSize(0));
int tpy = (int) Math.floor(viewportBounds.getY() / info.getTileSize(0));
int tpx = (int) FastMath.floor(viewportBounds.getX() / info.getTileSize(0));
int tpy = (int) FastMath.floor(viewportBounds.getY() / info.getTileSize(0));
// TilePoint topLeftTile = new TilePoint(tpx, tpy);
// p("top tile = " + topLeftTile);

View File

@@ -21,6 +21,7 @@
*/
package org.jdesktop.swingx.mapviewer;
import org.apache.commons.math3.util.FastMath;
import org.jdesktop.swingx.mapviewer.util.GeoUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -101,7 +102,7 @@ public abstract class AbstractTileFactory extends TileFactory
int numTilesWide = (int) getMapSize(zoom).getWidth();
if (tileX < 0)
{
tileX = numTilesWide - (Math.abs(tileX) % numTilesWide);
tileX = numTilesWide - (FastMath.abs(tileX) % numTilesWide);
}
tileX = tileX % numTilesWide;

View File

@@ -17,6 +17,8 @@
******************************************************************************/
package org.jdesktop.swingx.mapviewer;
import org.apache.commons.math3.util.FastMath;
import java.awt.geom.Rectangle2D;
import java.util.Set;
@@ -60,10 +62,10 @@ public class GeoBounds
double maxLng = Integer.MIN_VALUE;
for (GeoPosition position : geoPositions)
{
minLat = Math.min(minLat, position.getLatitude());
minLng = Math.min(minLng, position.getLongitude());
maxLat = Math.max(maxLat, position.getLatitude());
maxLng = Math.max(maxLng, position.getLongitude());
minLat = FastMath.min(minLat, position.getLatitude());
minLng = FastMath.min(minLng, position.getLongitude());
maxLat = FastMath.max(maxLat, position.getLatitude());
maxLng = FastMath.max(maxLng, position.getLongitude());
}
setRect(minLat, minLng, maxLat, maxLng);
}

View File

@@ -9,6 +9,8 @@
package org.jdesktop.swingx.mapviewer;
import org.apache.commons.math3.util.FastMath;
import java.awt.geom.Point2D;
/**
@@ -140,7 +142,7 @@ public class TileFactoryInfo
// how wide is each degree of longitude in pixels
longitudeDegreeWidthInPixels[z] = tilesize / 360.0;
// how wide is each radian of longitude in pixels
longitudeRadianWidthInPixels[z] = tilesize / (2.0 * Math.PI);
longitudeRadianWidthInPixels[z] = tilesize / (2.0 * FastMath.PI);
int t2 = tilesize / 2;
mapCenterInPixelsAtZoom[z] = new Point2D.Double(t2, t2);
mapWidthInTilesAtZoom[z] = tilesize / this.getTileSize(0);

View File

@@ -17,6 +17,7 @@
******************************************************************************/
package org.jdesktop.swingx.mapviewer.bmng;
import org.apache.commons.math3.util.FastMath;
import org.jdesktop.swingx.mapviewer.DefaultTileFactory;
import org.jdesktop.swingx.mapviewer.GeoPosition;
@@ -50,7 +51,7 @@ public class CylindricalProjectionTileFactory extends DefaultTileFactory
int midpoint = ((SLMapServerInfo) getInfo()).getMidpoint();
if (zoom < midpoint)
{
int w = (int) Math.pow(2, midpoint - zoom);
int w = (int) FastMath.pow(2, midpoint - zoom);
return new Dimension(w, w / 2);
// return super.getMapSize(zoom);
}

View File

@@ -17,6 +17,7 @@
******************************************************************************/
package org.jdesktop.swingx.mapviewer.bmng;
import org.apache.commons.math3.util.FastMath;
import org.jdesktop.swingx.mapviewer.TileFactoryInfo;
/**
@@ -85,7 +86,7 @@ public class SLMapServerInfo extends TileFactoryInfo
{
if (zoom < midpoint)
{
return (int) Math.pow(2, midpoint - zoom);
return (int) FastMath.pow(2, midpoint - zoom);
}
else
{

View File

@@ -9,6 +9,7 @@
package org.jdesktop.swingx.mapviewer.util;
import org.apache.commons.math3.util.FastMath;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.mapviewer.GeoBounds;
import org.jdesktop.swingx.mapviewer.GeoPosition;
@@ -99,7 +100,7 @@ public final class GeoUtil
{
double x = info.getMapCenterInPixelsAtZoom(zoomLevel).getX() + longitude
* info.getLongitudeDegreeWidthInPixels(zoomLevel);
double e = Math.sin(latitude * (Math.PI / 180.0));
double e = FastMath.sin(latitude * (FastMath.PI / 180.0));
if (e > 0.9999)
{
e = 0.9999;
@@ -108,7 +109,7 @@ public final class GeoUtil
{
e = -0.9999;
}
double y = info.getMapCenterInPixelsAtZoom(zoomLevel).getY() + 0.5 * Math.log((1 + e) / (1 - e)) * -1
double y = info.getMapCenterInPixelsAtZoom(zoomLevel).getY() + 0.5 * FastMath.log((1 + e) / (1 - e)) * -1
* (info.getLongitudeRadianWidthInPixels(zoomLevel));
return new Point2D.Double(x, y);
}
@@ -129,7 +130,7 @@ public final class GeoUtil
double flon = (wx - info.getMapCenterInPixelsAtZoom(zoom).getX()) / info.getLongitudeDegreeWidthInPixels(zoom);
double e1 = (wy - info.getMapCenterInPixelsAtZoom(zoom).getY())
/ (-1 * info.getLongitudeRadianWidthInPixels(zoom));
double e2 = (2 * Math.atan(Math.exp(e1)) - Math.PI / 2) / (Math.PI / 180.0);
double e2 = (2 * FastMath.atan(FastMath.exp(e1)) - FastMath.PI / 2) / (FastMath.PI / 180.0);
double flat = e2;
GeoPosition wc = new GeoPosition(flat, flon);
return wc;

View File

@@ -9,6 +9,8 @@
package org.jdesktop.swingx.mapviewer.util;
import org.apache.commons.math3.util.FastMath;
/**
* A utility class of methods that help when
* dealing with standard Mercator projections.
@@ -31,7 +33,7 @@ public final class MercatorUtils
*/
public static int longToX(double longitudeDegrees, double radius)
{
double longitude = Math.toRadians(longitudeDegrees);
double longitude = FastMath.toRadians(longitudeDegrees);
return (int) (radius * longitude);
}
@@ -42,8 +44,8 @@ public final class MercatorUtils
*/
public static int latToY(double latitudeDegrees, double radius)
{
double latitude = Math.toRadians(latitudeDegrees);
double y = radius / 2.0 * Math.log((1.0 + Math.sin(latitude)) / (1.0 - Math.sin(latitude)));
double latitude = FastMath.toRadians(latitudeDegrees);
double y = radius / 2.0 * FastMath.log((1.0 + FastMath.sin(latitude)) / (1.0 - FastMath.sin(latitude)));
return (int) y;
}
@@ -55,12 +57,12 @@ public final class MercatorUtils
public static double xToLong(int x, double radius)
{
double longRadians = x / radius;
double longDegrees = Math.toDegrees(longRadians);
double longDegrees = FastMath.toDegrees(longRadians);
/*
* The user could have panned around the world a lot of times. Lat long goes from -180 to 180. So every time a
* user gets to 181 we want to subtract 360 degrees. Every time a user gets to -181 we want to add 360 degrees.
*/
int rotations = (int) Math.floor((longDegrees + 180) / 360);
int rotations = (int) FastMath.floor((longDegrees + 180) / 360);
double longitude = longDegrees - (rotations * 360);
return longitude;
}
@@ -72,7 +74,7 @@ public final class MercatorUtils
*/
public static double yToLat(int y, double radius)
{
double latitude = (Math.PI / 2) - (2 * Math.atan(Math.exp(-1.0 * y / radius)));
return Math.toDegrees(latitude);
double latitude = (FastMath.PI / 2) - (2 * FastMath.atan(FastMath.exp(-1.0 * y / radius)));
return FastMath.toDegrees(latitude);
}
}

View File

@@ -9,6 +9,7 @@
package org.jdesktop.swingx.mapviewer.wms;
import org.apache.commons.math3.util.FastMath;
import org.jdesktop.swingx.mapviewer.util.MercatorUtils;
/**
@@ -57,7 +58,7 @@ public class WMSService
String srs = "EPSG:4326";
int ts = tileSize;
int circumference = widthOfWorldInPixels(zoom, tileSize);
double radius = circumference / (2 * Math.PI);
double radius = circumference / (2 * FastMath.PI);
double ulx = MercatorUtils.xToLong(x * ts, radius);
double uly = MercatorUtils.yToLat(y * ts, radius);
double lrx = MercatorUtils.xToLong((x + 1) * ts, radius);
@@ -73,7 +74,7 @@ public class WMSService
private int widthOfWorldInPixels(int zoom, int TILE_SIZE)
{
// int TILE_SIZE = 256;
int tiles = (int) Math.pow(2, zoom);
int tiles = (int) FastMath.pow(2, zoom);
int circumference = TILE_SIZE * tiles;
return circumference;
}

View File

@@ -9,6 +9,7 @@
package org.jdesktop.swingx.mapviewer.wms;
import org.apache.commons.math3.util.FastMath;
import org.jdesktop.swingx.mapviewer.DefaultTileFactory;
import org.jdesktop.swingx.mapviewer.TileFactoryInfo;
@@ -32,7 +33,7 @@ public class WMSTileFactory extends DefaultTileFactory
{
int zz = 17 - zoom;
int z = 4;
z = (int) Math.pow(2, (double) zz - 1);
z = (int) FastMath.pow(2, (double) zz - 1);
return wms.toWMSURL(x - z, z - 1 - y, zz, getTileSize(zoom));
}

View File

@@ -20,6 +20,8 @@
*/
package org.jdesktop.swingx.util;
import org.apache.commons.math3.util.FastMath;
import java.awt.*;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
@@ -86,10 +88,10 @@ public final class ShapeUtils {
AffineTransform trans = new AffineTransform();
Polygon poly = new Polygon();
for (int i = 0; i < sides; i++) {
trans.rotate(Math.PI * 2 / sides / 2);
trans.rotate(FastMath.PI * 2 / sides / 2);
Point2D out = trans.transform(new Point2D.Float(0, outsideRadius), null);
poly.addPoint((int) out.getX(), (int) out.getY());
trans.rotate(Math.PI * 2 / sides / 2);
trans.rotate(FastMath.PI * 2 / sides / 2);
if (insideRadius > 0) {
Point2D in = trans.transform(new Point2D.Float(0, insideRadius), null);
poly.addPoint((int) in.getX(), (int) in.getY());