You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ch...@apache.org on 2006/03/06 15:06:15 UTC

svn commit: r383544 - in /incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire: OpenWireFormat.java OpenWireFormatFactory.java

Author: chirino
Date: Mon Mar  6 06:06:13 2006
New Revision: 383544

URL: http://svn.apache.org/viewcvs?rev=383544&view=rev
Log:
Better cache eviction in place.

Modified:
    incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java
    incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormatFactory.java

Modified: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java?rev=383544&r1=383543&r2=383544&view=diff
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java (original)
+++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java Mon Mar  6 06:06:13 2006
@@ -42,6 +42,8 @@
     
     static final byte NULL_TYPE = CommandTypes.NULL;
     private static final int MARSHAL_CACHE_SIZE = Short.MAX_VALUE/2;
+    private static final int MARSHAL_CACHE_PREFERED_SIZE = MARSHAL_CACHE_SIZE-100;
+    
     private DataStreamMarshaller dataMarshallers[];
     private int version;
     private boolean stackTraceEnabled=true;
@@ -52,7 +54,8 @@
 
     private HashMap marshallCacheMap = new HashMap();
     private short nextMarshallCacheIndex=0;    
-    private short lasMarshallCacheEvictionIndex=100;    
+    private short nextMarshallCacheEvictionIndex=0;
+    
     private DataStructure marshallCache[] = new DataStructure[MARSHAL_CACHE_SIZE];
     private DataStructure unmarshallCache[] = new DataStructure[MARSHAL_CACHE_SIZE];
     
@@ -92,6 +95,10 @@
     
     public Packet marshal(Object command) throws IOException {
         
+        if( cacheEnabled ) {
+            runMarshallCacheEvictionSweep();
+        }
+        
         MarshallAware ma=null;
         // If not using value caching, then the marshaled form is always the same
         if( !cacheEnabled && ((DataStructure)command).isMarshallAware() ) {
@@ -187,6 +194,11 @@
     }
     
     public void marshal(Object o, DataOutputStream ds) throws IOException {
+        
+        if( cacheEnabled ) {
+            runMarshallCacheEvictionSweep();
+        }
+        
         int size=1;
         if( o != null) {
             DataStructure c = (DataStructure) o;
@@ -202,7 +214,7 @@
             ds.writeInt(size);
             ds.writeByte(type);            
             bs.marshal(ds);
-            dsm.tightMarshal2(this, c, ds, bs);
+            dsm.tightMarshal2(this, c, ds, bs);            
         } else {
             ds.writeInt(size);
             ds.writeByte(NULL_TYPE);
@@ -359,26 +371,33 @@
         }
     }
 
+    public void runMarshallCacheEvictionSweep() {
+        // Do we need to start evicting??
+        while( marshallCacheMap.size() > MARSHAL_CACHE_PREFERED_SIZE ) {
+            
+            marshallCacheMap.remove(marshallCache[nextMarshallCacheEvictionIndex]);
+            marshallCache[nextMarshallCacheEvictionIndex]=null;
+
+            nextMarshallCacheEvictionIndex++;
+            if( nextMarshallCacheEvictionIndex >= MARSHAL_CACHE_SIZE ) {
+                nextMarshallCacheEvictionIndex=0;
+            }
+            
+        }
+    }
     
-    public Short getMarshallCacheIndex(Object o) {
+    public Short getMarshallCacheIndex(DataStructure o) {
         return (Short) marshallCacheMap.get(o);
     }
     
-    public Short addToMarshallCache(Object o) {
-        nextMarshallCacheIndex++;
+    public Short addToMarshallCache(DataStructure o) {
+        short i = nextMarshallCacheIndex++;
         if( nextMarshallCacheIndex >= MARSHAL_CACHE_SIZE ) {
             nextMarshallCacheIndex=0;
         }
-        lasMarshallCacheEvictionIndex++;
-        if( lasMarshallCacheEvictionIndex >= MARSHAL_CACHE_SIZE ) {
-            lasMarshallCacheEvictionIndex=0;
-        }
-        if( marshallCache[lasMarshallCacheEvictionIndex]!=null ) {
-            marshallCacheMap.remove(marshallCache[lasMarshallCacheEvictionIndex]);
-            marshallCache[lasMarshallCacheEvictionIndex]=null;
-        }
-        marshallCache[nextMarshallCacheIndex] = (DataStructure) o;
-        Short index = new Short(nextMarshallCacheIndex);
+        
+        marshallCache[i] = o;
+        Short index = new Short(i);
         marshallCacheMap.put(o, index);
         return index;
     }

Modified: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormatFactory.java
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormatFactory.java?rev=383544&r1=383543&r2=383544&view=diff
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormatFactory.java (original)
+++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormatFactory.java Mon Mar  6 06:06:13 2006
@@ -28,6 +28,8 @@
     private boolean stackTraceEnabled=true;
     private boolean tcpNoDelayEnabled=false;
     private boolean cacheEnabled=true;
+    private boolean tightEncodingEnabled=true;
+    private boolean prefixPacketSize=true;
 
     public WireFormat createWireFormat() {
         OpenWireFormat format = new OpenWireFormat();
@@ -35,6 +37,8 @@
         format.setStackTraceEnabled(stackTraceEnabled);
         format.setCacheEnabled(cacheEnabled);
         format.setTcpNoDelayEnabled(tcpNoDelayEnabled);
+        format.setTightEncodingEnabled(tightEncodingEnabled);
+        format.setPrefixPacketSize(prefixPacketSize);
         return format;
     }
 
@@ -68,5 +72,21 @@
 
     public void setCacheEnabled(boolean cacheEnabled) {
         this.cacheEnabled = cacheEnabled;
+    }
+
+    public boolean isTightEncodingEnabled() {
+        return tightEncodingEnabled;
+    }
+
+    public void setTightEncodingEnabled(boolean tightEncodingEnabled) {
+        this.tightEncodingEnabled = tightEncodingEnabled;
+    }
+
+    public boolean isPrefixPacketSize() {
+        return prefixPacketSize;
+    }
+
+    public void setPrefixPacketSize(boolean prefixPacketSize) {
+        this.prefixPacketSize = prefixPacketSize;
     }
 }