Changeset 8574 in josm for trunk/src/org


Ignore:
Timestamp:
2015-07-05T22:14:49+02:00 (9 years ago)
Author:
bastiK
Message:

applied #11655 - memory optimization (patch by shinigami, modified)

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java

    r8566 r8574  
    487487        }
    488488
    489         final Map<String, String> result = new HashMap<>();
     489        final Map<String, String> result = new HashMap<>(Utils.hashMapInitialCapacity(keys.length / 2));
    490490        for (int i = 0; i < keys.length; i += 2) {
    491491            result.put(keys[i], keys[i + 1]);
     
    654654    @Override
    655655    public final Collection<String> keySet() {
    656         String[] keys = this.keys;
    657         if (keys == null)
     656        final String[] keys = this.keys;
     657        if (keys == null){
    658658            return Collections.emptySet();
    659         Set<String> result = new HashSet<>(keys.length / 2);
     659        }
     660        if (keys.length == 1){
     661            return Collections.singleton(keys[0]);
     662        }
     663
     664        final Set<String> result = new HashSet<>(Utils.hashMapInitialCapacity(keys.length / 2));
    660665        for (int i = 0; i < keys.length; i += 2) {
    661666            result.add(keys[i]);
  • trunk/src/org/openstreetmap/josm/io/OsmReader.java

    r8540 r8574  
    439439        String time = parser.getAttributeValue(null, "timestamp");
    440440        if (time != null && !time.isEmpty()) {
    441             current.setTimestamp(DateUtils.fromString(time));
     441            current.setRawTimestamp((int)(DateUtils.tsFromString(time)/1000));
    442442        }
    443443
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r8570 r8574  
    13661366        }
    13671367    }
     1368
     1369    /**
     1370     * Returns the initial capacity to pass to the HashMap / HashSet constructor
     1371     * when it is initialized with a known number of entries.
     1372     *
     1373     * When a HashMap is filled with entries, the underlying array is copied over
     1374     * to a larger one multiple times. To avoid this process when the number of
     1375     * entries is known in advance, the initial capacity of the array can be
     1376     * given to the HashMap constructor. This method returns a suitable value
     1377     * that avoids rehashing but doesn't waste memory.
     1378     * @param nEntries the number of entries expected
     1379     * @param loadFactor the load factor
     1380     * @return the initial capacity for the HashMap constructor
     1381     */
     1382    public static int hashMapInitialCapacity(int nEntries, float loadFactor) {
     1383        return (int) Math.ceil(nEntries / loadFactor);
     1384    }
     1385
     1386    /**
     1387     * Returns the initial capacity to pass to the HashMap / HashSet constructor
     1388     * when it is initialized with a known number of entries.
     1389     *
     1390     * When a HashMap is filled with entries, the underlying array is copied over
     1391     * to a larger one multiple times. To avoid this process when the number of
     1392     * entries is known in advance, the initial capacity of the array can be
     1393     * given to the HashMap constructor. This method returns a suitable value
     1394     * that avoids rehashing but doesn't waste memory.
     1395     *
     1396     * Assumes default load factor (0.75).
     1397     * @param nEntries the number of entries expected
     1398     * @return the initial capacity for the HashMap constructor
     1399     */
     1400    public static int hashMapInitialCapacity(int nEntries) {
     1401        return hashMapInitialCapacity(nEntries, 0.75f);
     1402    }
    13681403}
  • trunk/src/org/openstreetmap/josm/tools/date/DateUtils.java

    r8565 r8574  
    6666     */
    6767    public static synchronized Date fromString(String str) {
     68        return new Date(tsFromString(str));
     69    }
     70
     71    /**
     72     * Parses XML date quickly, regardless of current locale.
     73     * @param str The XML date as string
     74     * @return The date in milliseconds since epoch
     75     */
     76    public static synchronized long tsFromString(String str) {
    6877        // "2007-07-25T09:26:24{Z|{+|-}01:00}"
    6978        if (checkLayout(str, "xxxx-xx-xxTxx:xx:xxZ") ||
     
    8392                int plusHr = parsePart2(str, 20);
    8493                int mul = str.charAt(19) == '+' ? -3600000 : 3600000;
    85                 calendar.setTimeInMillis(calendar.getTimeInMillis()+plusHr*mul);
     94                return calendar.getTimeInMillis()+plusHr*mul;
    8695            }
    8796
    88             return calendar.getTime();
     97            return calendar.getTimeInMillis();
    8998        } else if (checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxxZ") ||
    9099                checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx") ||
     
    99108                parsePart2(str, 17));
    100109            long millis = parsePart3(str, 20);
    101             if (str.length() == 29)
     110            if (str.length() == 29){
    102111                millis += parsePart2(str, 24) * (str.charAt(23) == '+' ? -3600000 : 3600000);
    103             calendar.setTimeInMillis(calendar.getTimeInMillis()+millis);
    104 
    105             return calendar.getTime();
     112            }
     113
     114            return calendar.getTimeInMillis() + millis;
    106115        } else {
    107116            // example date format "18-AUG-08 13:33:03"
     
    109118            Date d = f.parse(str, new ParsePosition(0));
    110119            if (d != null)
    111                 return d;
     120                return d.getTime();
    112121        }
    113122
    114123        try {
    115             return XML_DATE.newXMLGregorianCalendar(str).toGregorianCalendar().getTime();
     124            return XML_DATE.newXMLGregorianCalendar(str).toGregorianCalendar().getTimeInMillis();
    116125        } catch (Exception ex) {
    117             return new Date();
     126            return System.currentTimeMillis();
    118127        }
    119128    }
Note: See TracChangeset for help on using the changeset viewer.