Changeset 12054 in josm


Ignore:
Timestamp:
2017-05-03T18:24:31+02:00 (18 months ago)
Author:
michael2402
Message:

Fix #14485: Increase sorting speed by removing compareTo complexity.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/StyledMapRenderer.java

    r12009 r12054  
    9999        private final OsmPrimitive osm;
    100100        private final int flags;
     101        private final long order;
    101102
    102103        StyleRecord(StyleElement style, OsmPrimitive osm, int flags) {
     
    104105            this.osm = osm;
    105106            this.flags = flags;
     107
     108            long order = 0;
     109            if ((this.flags & FLAG_DISABLED) != 0) {
     110                order |= 1;
     111            }
     112
     113            order <<= 24;
     114            order |= floatToFixed(this.style.majorZIndex, 24, 8);
     115
     116            // selected on top of member of selected on top of unselected
     117            // FLAG_DISABLED bit is the same at this point, but we simply ignore it
     118            order <<= 4;
     119            order |= this.flags & 0xf;
     120
     121            order <<= 24;
     122            order |= floatToFixed(this.style.zIndex, 24, 8);
     123
     124            order <<= 1;
     125            // simple node on top of icons and shapes
     126            if (NodeElement.SIMPLE_NODE_ELEMSTYLE.equals(this.style)) {
     127                order |= 1;
     128            }
     129
     130            this.order = order;
     131        }
     132
     133        /**
     134         * Converts a float to a fixed pointdecimal so that the order stays the same.
     135         *
     136         * @param number The float to convert
     137         * @param totalBits
     138         *            Total number of bits. 1 sign bit, then the bits before the
     139         *            decimal point, then those after.
     140         * @param afterDecimalBits
     141         *            Number of fixed bits after the decimal point.
     142         * @return The float converted to an integer.
     143         */
     144        private static long floatToFixed(double number, int totalBits, int afterDecimalBits) {
     145            long value = (long) (number * (1l << afterDecimalBits));
     146            long highestBitMask = 1l << totalBits - 1;
     147            long valueMask = highestBitMask - 1;
     148            long signBit = number < 0 ? 0 : highestBitMask;
     149            return signBit | value & valueMask;
    106150        }
    107151
    108152        @Override
    109153        public int compareTo(StyleRecord other) {
    110             if ((this.flags & FLAG_DISABLED) != 0 && (other.flags & FLAG_DISABLED) == 0)
    111                 return -1;
    112             if ((this.flags & FLAG_DISABLED) == 0 && (other.flags & FLAG_DISABLED) != 0)
    113                 return 1;
    114 
    115             int d0 = Float.compare(this.style.majorZIndex, other.style.majorZIndex);
    116             if (d0 != 0)
    117                 return d0;
    118 
    119             // selected on top of member of selected on top of unselected
    120             // FLAG_DISABLED bit is the same at this point
    121             if (this.flags > other.flags)
    122                 return 1;
    123             if (this.flags < other.flags)
    124                 return -1;
    125 
    126             int dz = Float.compare(this.style.zIndex, other.style.zIndex);
    127             if (dz != 0)
    128                 return dz;
    129 
    130             // simple node on top of icons and shapes
    131             if (NodeElement.SIMPLE_NODE_ELEMSTYLE.equals(this.style) && !NodeElement.SIMPLE_NODE_ELEMSTYLE.equals(other.style))
    132                 return 1;
    133             if (!NodeElement.SIMPLE_NODE_ELEMSTYLE.equals(this.style) && NodeElement.SIMPLE_NODE_ELEMSTYLE.equals(other.style))
    134                 return -1;
     154            int d = Long.compare(order, other.order);
     155            if (d != 0) {
     156                return d;
     157            }
    135158
    136159            // newer primitives to the front
Note: See TracChangeset for help on using the changeset viewer.