Changeset 18691 in josm


Ignore:
Timestamp:
2023-03-14T14:37:10+01:00 (21 months ago)
Author:
taylor.smock
Message:

Significantly reduce cost of large presets like Name Suggestion Index

This reduces the startup CPU of TaggingPresets#initialize by ~29% and the memory
by ~55%. This was done by adding a batch addActiveLayerChangeListeners method
in MainLayerManager.

This additionally reduces the cost of
TaggingPresetSelect$PresetClassification.simplifyString (used by the preset
search dialog) by ~28% CPU cycles and ~58% memory allocations. This was done by
having a single static Pattern which was reused -- most of the remaining
potentially reducible cost for simplifyString is from Matcher initialization.
This would require thread local variables, so may not be worthwhile for now.

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/MainLayerManager.java

    r18392 r18691  
    169169
    170170    /**
    171      * Adds a active/edit layer change listener
     171     * Adds an active/edit layer change listener
    172172     *
    173173     * @param listener the listener.
    174174     */
    175175    public synchronized void addActiveLayerChangeListener(ActiveLayerChangeListener listener) {
    176         for (int i = 0; i < activeLayerChangeListeners.size(); i++) {
    177             if (activeLayerChangeListeners.get(i) == listener) {
     176        for (ActiveLayerChangeListener activeLayerChangeListener : activeLayerChangeListeners) {
     177            if (activeLayerChangeListener == listener) {
    178178                Logging.error("");
    179179                Logging.error("Attempted to add listener that was already in list: " + listener);
     
    183183        }
    184184        activeLayerChangeListeners.add(listener);
     185    }
     186
     187    /**
     188     * Adds multiple active/edit layer change listeners. Either all listeners are added or none are added.
     189     *
     190     * @param listeners the listeners.
     191     * @return {@code false} if the listener list did not change
     192     * @since 18691
     193     */
     194    public synchronized boolean addActiveLayerChangeListeners(Collection<? extends ActiveLayerChangeListener> listeners) {
     195        for (ActiveLayerChangeListener activeLayerChangeListener : activeLayerChangeListeners) {
     196            if (listeners.contains(activeLayerChangeListener)) {
     197                Logging.error("");
     198                Logging.error("Attempted to add listener that was already in list: " + listeners);
     199                showStackTrace(Thread.currentThread().getStackTrace());
     200                return false;
     201            }
     202        }
     203        return activeLayerChangeListeners.addAll(listeners);
    185204    }
    186205
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetSelector.java

    r17055 r18691  
    1818import java.util.Objects;
    1919import java.util.Set;
     20import java.util.regex.Pattern;
    2021import java.util.stream.Collectors;
    2122
     
    6263    private static final int CLASSIFICATION_TAGS_MATCH = 100;
    6364
     65    private static final Pattern PATTERN_PUNCTUATION = Pattern.compile("\\p{Punct}", Pattern.UNICODE_CHARACTER_CLASS);
     66
    6467    private static final BooleanProperty SEARCH_IN_TAGS = new BooleanProperty("taggingpreset.dialog.search-in-tags", true);
    6568    private static final BooleanProperty ONLY_APPLICABLE = new BooleanProperty("taggingpreset.dialog.only-applicable-to-selection", true);
     
    136139
    137140        private static String simplifyString(String s) {
    138             return Utils.deAccent(s).toLowerCase(Locale.ENGLISH).replaceAll("\\p{Punct}", "");
     141            return PATTERN_PUNCTUATION.matcher(Utils.deAccent(s).toLowerCase(Locale.ENGLISH)).replaceAll("");
    139142        }
    140143
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresets.java

    r18363 r18691  
    99import java.util.HashMap;
    1010import java.util.HashSet;
     11import java.util.List;
    1112import java.util.Map;
    1213import java.util.Set;
     
    8889
    8990        readFromPreferences();
     91        final List<TaggingPreset> activeLayerChangeListeners = new ArrayList<>(taggingPresets.size());
    9092        for (TaggingPreset tp: taggingPresets) {
    9193            if (!(tp instanceof TaggingPresetSeparator)) {
    9294                MainApplication.getToolbar().register(tp);
    93                 MainApplication.getLayerManager().addActiveLayerChangeListener(tp);
    94             }
    95         }
     95                activeLayerChangeListeners.add(tp);
     96            }
     97        }
     98        MainApplication.getLayerManager().addActiveLayerChangeListeners(activeLayerChangeListeners);
    9699        if (taggingPresets.isEmpty()) {
    97100            presetsMenu.setVisible(false);
  • trunk/test/functional/org/openstreetmap/josm/io/MultiFetchServerObjectReaderTest.java

    r18690 r18691  
    1111import java.io.FileInputStream;
    1212import java.io.FileNotFoundException;
    13 import java.io.FileOutputStream;
    1413import java.io.IOException;
    1514import java.io.OutputStreamWriter;
  • trunk/test/performance/org/openstreetmap/josm/io/OsmReaderPerformanceTest.java

    r18690 r18691  
    77import java.io.ByteArrayOutputStream;
    88import java.io.File;
    9 import java.io.FileInputStream;
    109import java.io.IOException;
    1110import java.io.InputStream;
  • trunk/test/unit/org/openstreetmap/josm/data/validation/routines/DomainValidatorTestIT.java

    r18690 r18691  
    2323import java.io.Closeable;
    2424import java.io.File;
    25 import java.io.FileInputStream;
    2625import java.io.FileOutputStream;
    2726import java.io.IOException;
  • trunk/test/unit/org/openstreetmap/josm/gui/datatransfer/ClipboardUtilsTest.java

    r18690 r18691  
    1515import java.awt.datatransfer.Transferable;
    1616import java.awt.datatransfer.UnsupportedFlavorException;
    17 import java.io.IOException;
    1817
    1918import net.trajano.commons.testing.UtilityClassTestUtil;
  • trunk/test/unit/org/openstreetmap/josm/gui/datatransfer/LayerTransferableTest.java

    r18690 r18691  
    1616import org.junit.jupiter.api.Test;
    1717import org.openstreetmap.josm.gui.datatransfer.LayerTransferable.Data;
    18 import org.openstreetmap.josm.gui.layer.Layer;
    1918import org.openstreetmap.josm.gui.layer.LayerManagerTest;
    2019import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
Note: See TracChangeset for help on using the changeset viewer.