You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2009/03/27 15:28:59 UTC

svn commit: r759151 - /incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSName.java

Author: lehmi
Date: Fri Mar 27 14:28:58 2009
New Revision: 759151

URL: http://svn.apache.org/viewvc?rev=759151&view=rev
Log:
PDFBOX-441: split up the WeakHashMap in a static and a nonstatic part to decrease synchronized calls the to maps

Modified:
    incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSName.java

Modified: incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSName.java
URL: http://svn.apache.org/viewvc/incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSName.java?rev=759151&r1=759150&r2=759151&view=diff
==============================================================================
--- incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSName.java (original)
+++ incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSName.java Fri Mar 27 14:28:58 2009
@@ -20,6 +20,7 @@
 import java.io.OutputStream;
 
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.WeakHashMap;
 import java.util.Map;
 
@@ -40,6 +41,12 @@
      * multiple threads.
      */
     private static Map nameMap = Collections.synchronizedMap( new WeakHashMap(8192) );
+    
+    /**
+     * All common COSName values are stored in a simple HashMap. They are already defined as
+     * static constants and don't need to be synchronized for multithreaded environments.
+     */
+    private static Map commonNameMap = new HashMap() ;
 
 
     /**
@@ -515,11 +522,17 @@
         COSName name = null;
         if( aName != null )
         {
-            name = (COSName)nameMap.get( aName );
+        	// Is it a common COSName ??
+            name = (COSName)commonNameMap.get( aName );
             if( name == null )
             {
-                //name is added to map in the constructor
-                name = new COSName( aName );
+            	// It seems to be a document specific COSName
+            	name = (COSName)nameMap.get( aName );
+            	if( name == null )
+            	{
+            		//name is added to the synchronized map in the constructor
+            		name = new COSName( aName, false );
+            	}	
             }
         }
         return name;
@@ -530,15 +543,30 @@
      * that are created.
      *
      * @param aName The name of the COSName object.
+     * @param staticValue Indicates if the COSName object is static so that it can be stored in the HashMap without synchronizing.
      */
-    private COSName( String aName )
+    private COSName( String aName, boolean staticValue )
     {
         name = aName;
-        nameMap.put( aName, this );
+        if ( staticValue )
+        	commonNameMap.put( aName, this);
+        else
+        	nameMap.put( aName, this );
         hashCode = name.hashCode();
     }
 
     /**
+     * Private constructor.  This will limit the number of COSName objects.
+     * that are created.
+     *
+     * @param aName The name of the COSName object.
+     */
+    private COSName( String aName )
+    {
+    	this( aName, true );
+    }
+
+    /**
      * This will get the name of this COSName object.
      *
      * @return The name of the object.
@@ -646,24 +674,5 @@
      {
          // Clear them all
          nameMap.clear();
-
-         // Add the statics back in
-         java.lang.reflect.Field f[] = COSName.class.getFields();
-         if (f != null && f.length > 0)
-         {
-             for (int i=0; i<f.length; i++)
-             {
-                 try
-                 {
-                     Object obj = f[i].get(null);
-                     if (obj != null && obj instanceof COSName)
-                     {
-                         COSName cosname = (COSName)obj;
-                         nameMap.put(cosname.getName(),cosname);
-                     }
-                 }
-                 catch (Exception ignore) {}
-             }
-         }
      }
 }