This is an automated email from the ASF dual-hosted git repository.

matthiasblaesing pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 7775ab1ebf Optimize MimePath#validate by first querying the MimePath 
cache before attempting to parse
     new e623b46231 Merge pull request #3976 from 
matthiasblaesing/mime_parser_performance
7775ab1ebf is described below

commit 7775ab1ebf2839b895c4300a4e8b8013a5225ca9
Author: Matthias Bläsing <mblaes...@doppel-helix.eu>
AuthorDate: Sat Apr 2 10:35:13 2022 +0200

    Optimize MimePath#validate by first querying the MimePath cache before 
attempting to parse
    
    For the changes regarding the switch from a synchronized access to a
    HashMap to a ConcurrentHashMap:
    
    The existing synchronisation only protected the get and set operations
    on the map and not some secondary functionality (iteration for example).
    Using a ConcurrentHashMap should thus improve performance and not
    change the semantics.
---
 .../netbeans/api/editor/mimelookup/MimePath.java   | 48 ++++++++++++----------
 1 file changed, 26 insertions(+), 22 deletions(-)

diff --git 
a/platform/editor.mimelookup/src/org/netbeans/api/editor/mimelookup/MimePath.java
 
b/platform/editor.mimelookup/src/org/netbeans/api/editor/mimelookup/MimePath.java
index 7a6b3cd487..50f9598006 100644
--- 
a/platform/editor.mimelookup/src/org/netbeans/api/editor/mimelookup/MimePath.java
+++ 
b/platform/editor.mimelookup/src/org/netbeans/api/editor/mimelookup/MimePath.java
@@ -29,12 +29,12 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.regex.Pattern;
 import org.netbeans.modules.editor.mimelookup.APIAccessor;
 import org.netbeans.modules.editor.mimelookup.MimeLookupCacheSPI;
 import org.netbeans.modules.editor.mimelookup.MimePathLookup;
 import org.openide.util.Lookup;
-import org.openide.util.lookup.Lookups;
 
 /**
  * The mime path is a concatenation of one or more mime types. The purpose of
@@ -112,14 +112,14 @@ public final class MimePath {
     private static final Object LOCK = new Object();
 
     /** The List of Recently Used mime paths. */
-    private static final ArrayList<MimePath> LRU = new ArrayList<MimePath>();
+    private static final ArrayList<MimePath> LRU = new ArrayList<>();
 
     /** The maximum size of the List of Recently Used mime paths.
     /* package */ static final int MAX_LRU_SIZE = 3;
 
     private static final Pattern REG_NAME_PATTERN = 
Pattern.compile("^[[\\p{Alnum}][!#$&.+\\-^_]]{1,127}$"); //NOI18N
 
-    private static final Set<String> WELL_KNOWN_TYPES = new 
HashSet<String>(Arrays.asList(
+    private static final Set<String> WELL_KNOWN_TYPES = new 
HashSet<>(Arrays.asList(
         "application", //NOI18N
         "audio", //NOI18N
         "content", //NOI18N   for content/unknown mime type
@@ -131,7 +131,7 @@ public final class MimePath {
         "video" //NOI18N
     ));
     
-    private static final Map<String,Reference<MimePath>> string2mimePath = new 
HashMap<String,Reference<MimePath>>();
+    private static final Map<String,Reference<MimePath>> string2mimePath = new 
ConcurrentHashMap<>();
     
     /**
      * Gets the mime path for the given mime type. The returned 
<code>MimePath</code>
@@ -199,14 +199,12 @@ public final class MimePath {
      */
     public static MimePath parse(String path) {
         assert path != null : "path cannot be null"; // NOI18N
-        
-        synchronized (string2mimePath) {
-            Reference<MimePath> mpRef = string2mimePath.get(path);
-            MimePath mimePath = mpRef != null ? mpRef.get() : null;
-            
-            if (mimePath != null) {
-                return mimePath;
-            }
+
+        Reference<MimePath> mpRef = string2mimePath.get(path);
+        MimePath mimePath = mpRef != null ? mpRef.get() : null;
+
+        if (mimePath != null) {
+            return mimePath;
         }
 
         // Parse the path
@@ -214,16 +212,14 @@ public final class MimePath {
         if (!(o instanceof MimePath)) {
             throw new IllegalArgumentException((String) o);
         }
-        
-        synchronized (string2mimePath) {
-            MimePath mimePath = (MimePath) o;
-            
-            // Intern the path since the language path's string path is also 
interned
-            // and thus they can be matched by identity
-            string2mimePath.put(path.intern(), new 
WeakReference<MimePath>(mimePath));
-            
-            return mimePath;
-        }
+
+        mimePath = (MimePath) o;
+
+        // Intern the path since the language path's string path is also 
interned
+        // and thus they can be matched by identity
+        string2mimePath.put(path.intern(), new WeakReference<>(mimePath));
+
+        return mimePath;
     }
 
     /**
@@ -278,6 +274,14 @@ public final class MimePath {
      * @since 1.7
      */
     public static boolean validate(CharSequence path) {
+        if(path == null) {
+            return false;
+        }
+
+        if (string2mimePath.containsKey(path.toString())) {
+            return true;
+        }
+
         // parseImpl will return error string if parsing fails
         return !(parseImpl(path, true) instanceof String);
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org
For additional commands, e-mail: commits-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to