Changeset 17805 in josm for trunk/src


Ignore:
Timestamp:
2021-04-20T21:08:56+02:00 (4 years ago)
Author:
simon04
Message:

see #20745 - Avoid heap allocations in StyleCache

Location:
trunk/src/org/openstreetmap/josm/gui/mappaint
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/DividedScale.java

    r17793 r17805  
    177177    @Override
    178178    public int hashCode() {
    179         return Objects.hash(ranges, data);
     179        return 31 * ranges.hashCode() + data.hashCode();
    180180    }
    181181
  • trunk/src/org/openstreetmap/josm/gui/mappaint/Range.java

    r16322 r17805  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.mappaint;
    3 
    4 import java.util.Objects;
    53
    64/**
     
    117115    @Override
    118116    public int hashCode() {
    119         return Objects.hash(lower, upper);
     117        return 31 * Double.hashCode(lower) + Double.hashCode(upper);
    120118    }
    121119}
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleCache.java

    r17101 r17805  
    22package org.openstreetmap.josm.gui.mappaint;
    33
    4 import java.util.Arrays;
    54import java.util.Map;
    6 import java.util.Optional;
     5import java.util.Objects;
    76import java.util.concurrent.ConcurrentHashMap;
    87import java.util.function.Function;
     
    2524    public static final StyleCache EMPTY_STYLECACHE = new StyleCache().intern();
    2625
    27     private static final int PLAIN = 0;
    28     private static final int SELECTED = 1;
    29 
    30     @SuppressWarnings("unchecked")
    31     private final DividedScale<StyleElementList>[] states = new DividedScale[2];
     26    private DividedScale<StyleElementList> plainStyle;
     27    private DividedScale<StyleElementList> selectedStyle;
    3228
    3329    private StyleCache(StyleCache sc) {
    34         states[0] = sc.states[0];
    35         states[1] = sc.states[1];
     30        plainStyle = sc.plainStyle;
     31        selectedStyle = sc.selectedStyle;
    3632    }
    3733
     
    4945        StyleCache s = new StyleCache(this);
    5046
    51         int idx = getIndex(selected);
    52         s.states[idx] = Optional.ofNullable(s.states[idx]).orElseGet(DividedScale::new).put(o, r);
     47        if (selected) {
     48            if (s.selectedStyle == null) {
     49                s.selectedStyle = new DividedScale<>();
     50            }
     51            s.selectedStyle.put(o, r);
     52        } else {
     53            if (s.plainStyle == null) {
     54                s.plainStyle = new DividedScale<>();
     55            }
     56            s.plainStyle.put(o, r);
     57        }
    5358        return s.intern();
    5459    }
     
    6166     */
    6267    public Pair<StyleElementList, Range> getWithRange(double scale, boolean selected) {
    63         int idx = getIndex(selected);
    64         if (states[idx] == null) {
    65             return Pair.create(null, Range.ZERO_TO_INFINITY);
    66         }
    67         return states[idx].getWithRange(scale);
    68     }
    69 
    70     private static int getIndex(boolean selected) {
    71         return selected ? SELECTED : PLAIN;
     68        DividedScale<StyleElementList> style = selected ? selectedStyle : plainStyle;
     69        return style != null ? style.getWithRange(scale) : Pair.create(null, Range.ZERO_TO_INFINITY);
    7270    }
    7371
    7472    @Override
    7573    public String toString() {
    76         return "StyleCache{PLAIN: " + this.states[PLAIN] + " SELECTED: " + this.states[SELECTED] + "}";
     74        return "StyleCache{PLAIN: " + plainStyle + " SELECTED: " + selectedStyle + "}";
    7775    }
    7876
    7977    @Override
    8078    public int hashCode() {
    81         return Arrays.deepHashCode(this.states);
     79        return 31 * Objects.hashCode(plainStyle) + Objects.hashCode(selectedStyle);
    8280    }
    8381
     
    9189        }
    9290        final StyleCache other = (StyleCache) obj;
    93         return Arrays.deepEquals(this.states, other.states);
     91        return Objects.equals(plainStyle, other.plainStyle) && Objects.equals(selectedStyle, other.selectedStyle);
    9492    }
    9593
Note: See TracChangeset for help on using the changeset viewer.