Changeset 17846 in josm


Ignore:
Timestamp:
2021-05-01T21:50:00+02:00 (3 years ago)
Author:
simon04
Message:

fix #20793 - Reduce memory consumption for GpxExtensionCollection (patch by Bjoeni, modified)

Location:
trunk/src/org/openstreetmap/josm
Files:
5 edited

Legend:

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

    r16913 r17846  
    2323    private static final long serialVersionUID = 1L;
    2424
    25     private final Stack<GpxExtension> childStack = new Stack<>();
     25    private Stack<GpxExtension> childStack;
    2626    private IWithAttributes parent;
    2727
     
    4747     */
    4848    public void openChild(String namespaceURI, String qName, Attributes atts) {
     49        if (childStack == null) {
     50            childStack = new Stack<>();
     51        }
    4952        GpxExtension child = new GpxExtension(namespaceURI, qName, atts);
    5053        if (!childStack.isEmpty()) {
     
    6366     */
    6467    public void closeChild(String qName, String value) {
    65         if (childStack.isEmpty())
     68        if (childStack == null || childStack.isEmpty())
    6669            throw new InvalidArgumentException("Can't close child " + qName + ", no element in stack.");
    6770
     
    259262    @Override
    260263    public void clear() {
    261         childStack.clear();
     264        if (childStack != null) {
     265            childStack.clear();
     266            childStack = null;
     267        }
    262268        super.clear();
    263269    }
  • trunk/src/org/openstreetmap/josm/data/gpx/GpxTrack.java

    r16436 r17846  
    105105
    106106    private Color getColorFromExtension() {
     107        if (!hasExtensions()) {
     108            return null;
     109        }
    107110        GpxExtension gpxd = getExtensions().find("gpxd", "color");
    108111        if (gpxd != null) {
  • trunk/src/org/openstreetmap/josm/data/gpx/IWithAttributes.java

    r15496 r17846  
    5858
    5959    /**
     60     * Returns whether the {@link GpxExtensionCollection} instance has been created yet, should be overridden.
     61     * The instance will usually be created on first call of {@link #getExtensions()}.
     62     * @return whether the {@link GpxExtensionCollection} instance has been created yet
     63     */
     64    default boolean hasExtensions() {
     65        return getExtensions() != null;
     66    }
     67
     68    /**
    6069     * Returns the extensions
    6170     * @return the extensions
  • trunk/src/org/openstreetmap/josm/data/gpx/WithAttributes.java

    r15496 r17846  
    2525     * The "exts" collection contains all extensions.
    2626     */
    27     private final GpxExtensionCollection exts = new GpxExtensionCollection(this);
     27    private GpxExtensionCollection exts;
    2828
    2929    /**
     
    8787
    8888    @Override
     89    public boolean hasExtensions() {
     90        return exts != null;
     91    }
     92
     93    @Override
    8994    public GpxExtensionCollection getExtensions() {
     95        if (exts == null) {
     96            exts = new GpxExtensionCollection(this);
     97        }
    9098        return exts;
    9199    }
  • trunk/src/org/openstreetmap/josm/io/GpxReader.java

    r17748 r17846  
    507507                    if (!currentTrackSeg.isEmpty()) {
    508508                        GpxTrackSegment seg = new GpxTrackSegment(currentTrackSeg);
    509                         seg.getExtensions().addAll(currentExtensionCollection);
     509                        if (!currentExtensionCollection.isEmpty()) {
     510                            seg.getExtensions().addAll(currentExtensionCollection);
     511                        }
    510512                        currentTrack.add(seg);
    511513                    }
     
    519521                    convertUrlToLink(currentTrackAttr);
    520522                    GpxTrack trk = new GpxTrack(new ArrayList<>(currentTrack), currentTrackAttr);
    521                     trk.getExtensions().addAll(currentTrackExtensionCollection);
     523                    if (!currentTrackExtensionCollection.isEmpty()) {
     524                        trk.getExtensions().addAll(currentTrackExtensionCollection);
     525                    }
    522526                    data.addTrack(trk);
    523527                    currentTrackExtensionCollection.clear();
Note: See TracChangeset for help on using the changeset viewer.