Changeset 17571 in josm for trunk


Ignore:
Timestamp:
2021-03-16T21:56:57+01:00 (4 years ago)
Author:
simon04
Message:

fix #20563 - PluginListParser.parse amounts to 80% of allocations during startup

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java

    r17562 r17571  
    185185        public String getText() {
    186186            StringBuilder b = new StringBuilder();
    187             for (Entry<String, String> e : info.attr.entrySet()) {
     187            for (Entry<Object, Object> e : info.attr.entrySet()) {
    188188                b.append(e.getKey());
    189189                b.append(": ");
  • trunk/src/org/openstreetmap/josm/plugins/PluginInformation.java

    r16874 r17571  
    1919import java.util.Map;
    2020import java.util.Optional;
    21 import java.util.TreeMap;
    2221import java.util.jar.Attributes;
    2322import java.util.jar.JarInputStream;
     
    9493    public List<URL> libraries = new LinkedList<>();
    9594    /** All manifest attributes. */
    96     public final Map<String, String> attr = new TreeMap<>();
     95    public Attributes attr;
    9796    /** Invalid manifest entries */
    9897    final List<String> invalidManifestEntries = new ArrayList<>();
     
    134133            if (manifest == null)
    135134                throw new PluginException(tr("The plugin file ''{0}'' does not include a Manifest.", file.toString()));
    136             scanManifest(manifest, false);
     135            scanManifest(manifest.getMainAttributes(), false);
    137136            libraries.add(0, Utils.fileToURL(file));
    138137        } catch (IOException | InvalidPathException e) {
     
    151150     */
    152151    public PluginInformation(InputStream manifestStream, String name, String url) throws PluginException {
    153         this.name = name;
    154152        try {
    155153            Manifest manifest = new Manifest();
     
    158156                downloadlink = url;
    159157            }
    160             scanManifest(manifest, url != null);
     158            scanManifest(manifest.getMainAttributes(), url != null);
    161159        } catch (IOException e) {
    162160            throw new PluginException(name, e);
    163161        }
     162    }
     163
     164    /**
     165     * Creates a plugin information object by reading plugin information in Manifest format
     166     * from the input stream {@code manifestStream}.
     167     *
     168     * @param attr the manifest attributes
     169     * @param name the plugin name
     170     * @param url the download URL for the plugin
     171     * @throws PluginException if the plugin information can't be read from the input stream
     172     */
     173    public PluginInformation(Attributes attr, String name, String url) throws PluginException {
     174        this.name = name;
     175        if (url != null) {
     176            downloadlink = url;
     177        }
     178        scanManifest(attr, url != null);
    164179    }
    165180
     
    189204        this.canloadatruntime = other.canloadatruntime;
    190205        this.libraries = other.libraries;
    191         this.attr.clear();
    192         this.attr.putAll(other.attr);
     206        this.attr = new Attributes(other.attr);
    193207        this.invalidManifestEntries.clear();
    194208        this.invalidManifestEntries.addAll(other.invalidManifestEntries);
     
    215229    }
    216230
    217     private void scanManifest(Manifest manifest, boolean oldcheck) {
     231    private void scanManifest(Attributes attr, boolean oldcheck) {
    218232        String lang = LanguageInfo.getLanguageCodeManifest();
    219         Attributes attr = manifest.getMainAttributes();
    220233        className = attr.getValue("Plugin-Class");
    221234        String s = Optional.ofNullable(attr.getValue(lang+"Plugin-Link")).orElseGet(() -> attr.getValue("Plugin-Link"));
     
    318331            }
    319332        }
    320         for (Object o : attr.keySet()) {
    321             this.attr.put(o.toString(), attr.getValue(o.toString()));
    322         }
     333        this.attr = attr;
    323334    }
    324335
  • trunk/src/org/openstreetmap/josm/plugins/PluginListParser.java

    r16643 r17571  
    55
    66import java.io.BufferedReader;
    7 import java.io.ByteArrayInputStream;
    87import java.io.IOException;
    98import java.io.InputStream;
     
    1211import java.util.LinkedList;
    1312import java.util.List;
     13import java.util.jar.Attributes;
    1414
    1515import org.openstreetmap.josm.tools.Logging;
     
    3030     * @param name the plugin name
    3131     * @param url the plugin download url
    32      * @param manifest the plugin manifest
     32     * @param manifest the plugin manifest attributes
    3333     * @return a plugin information object
    3434     * @throws PluginListParseException if plugin manifest cannot be parsed
    3535     */
    36     public static PluginInformation createInfo(String name, String url, String manifest) throws PluginListParseException {
     36    public static PluginInformation createInfo(String name, String url, Attributes manifest) throws PluginListParseException {
    3737        try {
    3838            return new PluginInformation(
    39                     new ByteArrayInputStream(manifest.getBytes(StandardCharsets.UTF_8)),
     39                    manifest,
    4040                    name.substring(0, name.length() - 4),
    4141                    url
     
    6262            String name = null;
    6363            String url = null;
    64             StringBuilder manifest = new StringBuilder();
     64            Attributes manifest = new Attributes();
    6565            for (String line = r.readLine(); line != null; line = r.readLine()) {
    6666                if (line.startsWith("\t")) {
    67                     line = line.substring(1);
    68                     /* NOTE: Although manifest specification says line should not be longer than 72 bytes it
    69                        supports more than 500 bytes and thus even the longest possible 72 character UTF-8, so
    70                        this code correctly splits the text at 70 characters, not bytes. */
    71                     while (line.length() > 70) {
    72                         manifest.append(line.substring(0, 70)).append('\n');
    73                         line = ' ' + line.substring(70);
    74                     }
    75                     manifest.append(line).append('\n');
     67                    final String[] keyValue = line.split("\\s*:\\s*", 2);
     68                    manifest.put(new Attributes.Name(keyValue[0].substring(1)), keyValue[1]);
    7669                    continue;
    7770                }
    78                 addPluginInformation(ret, name, url, manifest.toString());
     71                addPluginInformation(ret, name, url, manifest);
    7972                String[] x = line.split(";", -1);
    8073                if (x.length != 2)
     
    8275                name = x[0];
    8376                url = x[1];
    84                 manifest = new StringBuilder();
     77                manifest = new Attributes();
    8578            }
    86             addPluginInformation(ret, name, url, manifest.toString());
     79            addPluginInformation(ret, name, url, manifest);
    8780            return ret;
    8881        } catch (IOException e) {
     
    9184    }
    9285
    93     private static void addPluginInformation(List<PluginInformation> ret, String name, String url, String manifest) {
     86    private static void addPluginInformation(List<PluginInformation> ret, String name, String url, Attributes manifest) {
    9487        try {
    9588            if (name != null) {
  • trunk/test/unit/org/openstreetmap/josm/gui/MainApplicationTest.java

    r17275 r17571  
    2222import java.util.concurrent.ExecutionException;
    2323import java.util.concurrent.Future;
     24import java.util.jar.Attributes;
    2425
    2526import javax.swing.JComponent;
     
    205206    private static PluginInformation newPluginInformation(String plugin) throws PluginListParseException {
    206207        return PluginListParser.createInfo(plugin+".jar", "https://josm.openstreetmap.de/osmsvn/applications/editors/josm/dist/"+plugin+".jar",
    207                 "");
     208                new Attributes());
    208209    }
    209210
  • trunk/test/unit/org/openstreetmap/josm/plugins/PluginHandlerTest.java

    r17275 r17571  
    154154    void testPluginInformationAction() throws PluginException {
    155155        TestUtils.assumeWorkingJMockit();
    156         final String expectedText = "Ant-Version: Apache Ant 1.9.6\n" +
    157             "Author: Don-vip\n" +
    158             "Created-By: 1.7.0_91-b02 (Oracle Corporation)\n" +
    159             "Manifest-Version: 1.0\n" +
    160             "Plugin-Canloadatruntime: true\n" +
    161             "Plugin-Class: org.openstreetmap.josm.plugins.fr.epci.EpciPlugin\n" +
    162             "Plugin-Date: 2015-11-19T08:21:07.645033Z\n" +
    163             "Plugin-Description: Handling of French EPCIs (boundary=local_authority)\n" +
    164             "Plugin-Early: true\n" +
    165             "Plugin-Link: http://wiki.openstreetmap.org/wiki/FR:JOSM/Fr:Plugin/EPCI-fr\n" +
    166             "Plugin-Mainversion: 7001\n" +
    167             "Plugin-Version: 31772\n";
     156        final String expectedText = "Manifest-Version: 1.0\n" +
     157                "Ant-Version: Apache Ant 1.9.6\n" +
     158                "Created-By: 1.7.0_91-b02 (Oracle Corporation)\n" +
     159                "Plugin-Mainversion: 7001\n" +
     160                "Plugin-Version: 31772\n" +
     161                "Plugin-Class: org.openstreetmap.josm.plugins.fr.epci.EpciPlugin\n" +
     162                "Plugin-Description: Handling of French EPCIs (boundary=local_authority)\n" +
     163                "Plugin-Date: 2015-11-19T08:21:07.645033Z\n" +
     164                "Author: Don-vip\n" +
     165                "Plugin-Link: http://wiki.openstreetmap.org/wiki/FR:JOSM/Fr:Plugin/EPCI-fr\n" +
     166                "Plugin-Early: true\n" +
     167                "Plugin-Canloadatruntime: true\n";
    168168        final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker() {
    169169            @Override
Note: See TracChangeset for help on using the changeset viewer.