Ignore:
Timestamp:
2011-06-07T19:05:14+02:00 (13 years ago)
Author:
bastiK
Message:

memory optimizations for Node & WayPoint (Patch by Gubaer, modified)

The field 'proj' in CachedLatLon is a waste of memory. For the 2 classes where this has the greatest impact, the cache for the projected coordinates is replaced by 2 simple double fields (east & north). On projection change, they have to be invalidated explicitly. This is handled by the DataSet & the GpxLayer.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/gpx/WayPoint.java

    r3321 r4126  
    77import java.util.Date;
    88
    9 import org.openstreetmap.josm.data.coor.CachedLatLon;
     9import org.openstreetmap.josm.Main;
    1010import org.openstreetmap.josm.data.coor.EastNorth;
    1111import org.openstreetmap.josm.data.coor.LatLon;
     12import org.openstreetmap.josm.data.projection.Projections;
    1213import org.openstreetmap.josm.tools.PrimaryDateParser;
    1314
     
    2627
    2728    public WayPoint(LatLon ll) {
    28         coor = new CachedLatLon(ll);
     29        lat = ll.lat();
     30        lon = ll.lon();
    2931    }
    3032
    31     private final CachedLatLon coor;
     33    /*
     34     * We "inline" lat/lon, rather than usinga LatLon internally => reduces memory overhead. Relevant
     35     * because a lot of GPX waypoints are created when GPS tracks are downloaded from the OSM server.
     36     */
     37    private double lat = 0;
     38    private double lon = 0;
     39
     40    /*
     41     * internal cache of projected coordinates
     42     */
     43    private double east = Double.NaN;
     44    private double north = Double.NaN;
     45
     46    /**
     47     * Invalidate the internal cache of east/north coordinates.
     48     */
     49    public void invalidateEastNorthCache() {
     50        this.east = Double.NaN;
     51        this.north = Double.NaN;
     52    }
    3253
    3354    public final LatLon getCoor() {
    34         return coor;
     55        return new LatLon(lat,lon);
    3556    }
    3657
     58    /**
     59     * <p>Replies the projected east/north coordinates.</p>
     60     *
     61     * <p>Uses the {@link Main#getProjection() global projection} to project the lan/lon-coordinates.
     62     * Internally caches the projected coordinates.</p>
     63     *
     64     * <p><strong>Caveat:</strong> doesn't listen to projection changes. Clients must
     65     * {@link #reproject() trigger a reprojection} or {@link #invalidateEastNorthCache() invalidate the internal cache}.</p>
     66     *
     67     * @return the east north coordinates or {@code null}
     68     * @see #invalidateEastNorthCache()
     69     *
     70     */
    3771    public final EastNorth getEastNorth() {
    38         return coor.getEastNorth();
     72        if (Double.isNaN(east) || Double.isNaN(north)) {
     73            // projected coordinates haven't been calculated yet,
     74            // so fill the cache of the projected waypoint coordinates
     75            EastNorth en = Projections.project(new LatLon(lat, lon));
     76            this.east = en.east();
     77            this.north = en.north();
     78        }
     79        return new EastNorth(east, north);
    3980    }
    4081
    4182    @Override
    4283    public String toString() {
    43         return "WayPoint (" + (attr.containsKey("name") ? attr.get("name") + ", " :"") + coor.toString() + ", " + attr + ")";
     84        return "WayPoint (" + (attr.containsKey("name") ? attr.get("name") + ", " :"") + getCoor().toString() + ", " + attr + ")";
    4485    }
    4586
     
    5798    }
    5899
    59     public int compareTo(WayPoint w)
    60     {
     100    public int compareTo(WayPoint w) {
    61101        return Double.compare(time, w.time);
    62102    }
Note: See TracChangeset for help on using the changeset viewer.