You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by fh...@apache.org on 2006/03/01 15:54:41 UTC
svn commit: r382040 - in
/tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes:
demos/ group/interceptors/ io/ mcast/
Author: fhanik
Date: Wed Mar 1 06:54:35 2006
New Revision: 382040
URL: http://svn.apache.org/viewcvs?rev=382040&view=rev
Log:
Improved performance by reducing all the byte[] copy methods. The XByteBuffer can grow dynamically and utilizie its internal array for direct writes
Modified:
tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/demos/LoadTest.java
tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/group/interceptors/FragmentationInterceptor.java
tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/group/interceptors/OrderInterceptor.java
tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/io/ClusterData.java
tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/io/XByteBuffer.java
tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/mcast/McastMember.java
Modified: tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/demos/LoadTest.java
URL: http://svn.apache.org/viewcvs/tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/demos/LoadTest.java?rev=382040&r1=382039&r2=382040&view=diff
==============================================================================
--- tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/demos/LoadTest.java (original)
+++ tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/demos/LoadTest.java Wed Mar 1 06:54:35 2006
@@ -15,24 +15,16 @@
*/
package org.apache.catalina.tribes.demos;
-import org.apache.catalina.tribes.Member;
-import org.apache.catalina.tribes.MembershipListener;
-import org.apache.catalina.tribes.ChannelListener;
import java.io.Serializable;
-import org.apache.catalina.tribes.ManagedChannel;
-import java.io.Externalizable;
-import java.io.ObjectOutput;
-import java.io.IOException;
import java.util.Random;
-import java.io.ObjectInput;
-import org.apache.catalina.tribes.io.XByteBuffer;
-import org.apache.catalina.tribes.tcp.ReplicationListener;
-import org.apache.catalina.tribes.group.GroupChannel;
-import org.apache.catalina.tribes.tcp.ReplicationTransmitter;
-import org.apache.catalina.tribes.mcast.McastService;
+
import org.apache.catalina.tribes.ByteMessage;
-import org.apache.catalina.tribes.group.interceptors.GzipInterceptor;
import org.apache.catalina.tribes.ChannelException;
+import org.apache.catalina.tribes.ChannelListener;
+import org.apache.catalina.tribes.ManagedChannel;
+import org.apache.catalina.tribes.Member;
+import org.apache.catalina.tribes.MembershipListener;
+import org.apache.catalina.tribes.io.XByteBuffer;
/**
@@ -264,38 +256,39 @@
}
protected byte[] message = null;
- private int msgNr;
-
+ protected int nr = -1;
static {
r.nextBytes(outdata);
+ }
+
+ public LoadMessage() {
}
+ public LoadMessage(int nr) {
+ this.nr = nr;
+ }
+
public int getMsgNr() {
return XByteBuffer.toInt(getMessage(),0);
}
public void setMsgNr(int nr) {
- byte[] data = XByteBuffer.toBytes(nr);
- System.arraycopy(data,0,getMessage(),0,4);
- setMessage(getMessage());
+ XByteBuffer.toBytes(nr,getMessage(),0);
}
public byte[] getMessage() {
- byte[] data = new byte[size+4];
- XByteBuffer.toBytes(msgNr,data,0);
- if ( message != null ) {
- System.arraycopy(message, 0, data, 4, message.length);
- }else {
+ if ( message == null ) {
+ byte[] data = new byte[size+4];
+ XByteBuffer.toBytes(nr,data,0);
System.arraycopy(outdata, 0, data, 4, outdata.length);
+ this.message = data;
}
- return data;
+ return message;
}
public void setMessage(byte[] data) {
- this.msgNr = XByteBuffer.toInt(data,0);
- this.message = new byte[data.length-4];
- System.arraycopy(data,4,message,0,message.length);
+ this.message = data;
}
}
Modified: tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/group/interceptors/FragmentationInterceptor.java
URL: http://svn.apache.org/viewcvs/tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/group/interceptors/FragmentationInterceptor.java?rev=382040&r1=382039&r2=382040&view=diff
==============================================================================
--- tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/group/interceptors/FragmentationInterceptor.java (original)
+++ tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/group/interceptors/FragmentationInterceptor.java Wed Mar 1 06:54:35 2006
@@ -56,8 +56,9 @@
frag(destination, msg, payload);
}
else {
- byte[] flag = XByteBuffer.toBytes(frag);
- msg.getMessage().append(flag,0,flag.length);
+ //byte[] flag = XByteBuffer.toBytes(frag);
+ //msg.getMessage().append(flag,0,flag.length);
+ msg.getMessage().append(frag);
super.sendMessage(destination, msg, payload);
}
}
@@ -117,12 +118,15 @@
tmp.getMessage().clear();
tmp.getMessage().append(msg.getMessage().getBytesDirect(),offset,length);
//add the msg nr
- tmp.getMessage().append(XByteBuffer.toBytes(i),0,4);
+ //tmp.getMessage().append(XByteBuffer.toBytes(i),0,4);
+ tmp.getMessage().append(i);
//add the total nr of messages
- tmp.getMessage().append(XByteBuffer.toBytes(count),0,4);
+ //tmp.getMessage().append(XByteBuffer.toBytes(count),0,4);
+ tmp.getMessage().append(count);
//add true as the frag flag
- byte[] flag = XByteBuffer.toBytes(true);
- tmp.getMessage().append(flag,0,flag.length);
+ //byte[] flag = XByteBuffer.toBytes(true);
+ //tmp.getMessage().append(flag,0,flag.length);
+ tmp.getMessage().append(true);
messages[i] = tmp;
remaining -= length;
Modified: tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/group/interceptors/OrderInterceptor.java
URL: http://svn.apache.org/viewcvs/tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/group/interceptors/OrderInterceptor.java?rev=382040&r1=382039&r2=382040&view=diff
==============================================================================
--- tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/group/interceptors/OrderInterceptor.java (original)
+++ tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/group/interceptors/OrderInterceptor.java Wed Mar 1 06:54:35 2006
@@ -62,7 +62,9 @@
for ( int i=0; i<destination.length; i++ ) {
ChannelMessage tmp = msg.clone();
int nr = incCounter(destination[i]);
- tmp.getMessage().append(XByteBuffer.toBytes(nr),0,4);
+ //reduce byte copy
+ //tmp.getMessage().append(XByteBuffer.toBytes(nr),0,4);
+ tmp.getMessage().append(nr);
getNext().sendMessage(new Member[] {destination[i]}, tmp, payload);
}
}
Modified: tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/io/ClusterData.java
URL: http://svn.apache.org/viewcvs/tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/io/ClusterData.java?rev=382040&r1=382039&r2=382040&view=diff
==============================================================================
--- tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/io/ClusterData.java (original)
+++ tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/io/ClusterData.java Wed Mar 1 06:54:35 2006
@@ -133,8 +133,11 @@
long msb = id.getMostSignificantBits();
long lsb = id.getLeastSignificantBits();
byte[] data = new byte[16];
- System.arraycopy(XByteBuffer.toBytes(msb),0,data,0,8);
- System.arraycopy(XByteBuffer.toBytes(lsb),0,data,8,8);
+ //reduce byte copy
+ //System.arraycopy(XByteBuffer.toBytes(msb),0,data,0,8);
+ XByteBuffer.toBytes(msb,data,0);
+ //System.arraycopy(XByteBuffer.toBytes(lsb),0,data,8,8);
+ XByteBuffer.toBytes(lsb,data,8);
setUniqueId(data);
}
Modified: tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/io/XByteBuffer.java
URL: http://svn.apache.org/viewcvs/tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/io/XByteBuffer.java?rev=382040&r1=382039&r2=382040&view=diff
==============================================================================
--- tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/io/XByteBuffer.java (original)
+++ tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/io/XByteBuffer.java Wed Mar 1 06:54:35 2006
@@ -16,19 +16,12 @@
package org.apache.catalina.tribes.io;
-import org.apache.catalina.tribes.ChannelMessage;
-
-import java.io.ObjectOutputStream;
-import java.util.zip.GZIPOutputStream;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.io.ByteArrayInputStream;
import java.io.InputStream;
-import java.util.zip.GZIPInputStream;
-import org.apache.catalina.tribes.io.ReplicationStream;
+import java.io.ObjectOutputStream;
import java.io.Serializable;
-import org.apache.catalina.tribes.Member;
-import java.util.UUID;
import java.nio.ByteBuffer;
/**
@@ -36,6 +29,8 @@
* One, it stores message bytes and automatically extends the byte buffer if needed.<BR>
* Two, it can encode and decode packages so that they can be defined and identified
* as they come in on a socket.
+ * <br>
+ * <b>THIS CLASS IS NOT THREAD SAFE</B><BR>
* <br/>
* Transfer package:
* <ul>
@@ -160,10 +155,7 @@
public boolean append(ByteBuffer b, int len) {
int newcount = bufSize + len;
if (newcount > buf.length) {
- //don't change the allocation strategy
- byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
- System.arraycopy(buf, 0, newbuf, 0, bufSize);
- buf = newbuf;
+ expand(newcount);
}
b.get(buf,bufSize,len);
@@ -179,6 +171,47 @@
return true;
}
+
+ public boolean append(byte i) {
+ int newcount = bufSize + 1;
+ if (newcount > buf.length) {
+ expand(newcount);
+ }
+ buf[bufSize] = i;
+ bufSize = newcount;
+ return true;
+ }
+
+
+ public boolean append(boolean i) {
+ int newcount = bufSize + 1;
+ if (newcount > buf.length) {
+ expand(newcount);
+ }
+ XByteBuffer.toBytes(i,buf,bufSize);
+ bufSize = newcount;
+ return true;
+ }
+
+ public boolean append(long i) {
+ int newcount = bufSize + 8;
+ if (newcount > buf.length) {
+ expand(newcount);
+ }
+ XByteBuffer.toBytes(i,buf,bufSize);
+ bufSize = newcount;
+ return true;
+ }
+
+ public boolean append(int i) {
+ int newcount = bufSize + 4;
+ if (newcount > buf.length) {
+ expand(newcount);
+ }
+ XByteBuffer.toBytes(i,buf,bufSize);
+ bufSize = newcount;
+ return true;
+ }
public boolean append(byte[] b, int off, int len) {
if ((off < 0) || (off > b.length) || (len < 0) ||
@@ -190,10 +223,7 @@
int newcount = bufSize + len;
if (newcount > buf.length) {
- //don't change the allocation strategy
- byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
- System.arraycopy(buf, 0, newbuf, 0, bufSize);
- buf = newbuf;
+ expand(newcount);
}
System.arraycopy(b, off, buf, bufSize, len);
bufSize = newcount;
@@ -208,6 +238,13 @@
return true;
}
+ private void expand(int newcount) {
+ //don't change the allocation strategy
+ byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
+ System.arraycopy(buf, 0, newbuf, 0, bufSize);
+ buf = newbuf;
+ }
+
/**
* Internal mechanism to make a check if a complete package exists
@@ -297,7 +334,7 @@
System.arraycopy(END_DATA, 0, result, START_DATA.length + 4 + data.length, END_DATA.length);
return result;
}
-
+
/**
* Convert four bytes to an int
@@ -336,12 +373,24 @@
* Converts an integer to four bytes
* @param n - the integer
* @return - four bytes in an array
+ * @deprecated use toBytes(boolean,byte[],int)
*/
public static byte[] toBytes(boolean bool) {
- byte[] b = new byte[] {(byte)(bool?1:0)};
- return b;
+ byte[] b = new byte[1] ;
+ return toBytes(bool,b,0);
+
}
+ public static byte[] toBytes(boolean bool, byte[] data, int offset) {
+ data[offset] = (byte)(bool?1:0);
+ return data;
+ }
+
+ /**
+ *
+ * @param <any> long
+ * @return use
+ */
public static boolean toBoolean(byte[] b, int offset) {
return b[offset] != 0;
}
@@ -351,6 +400,7 @@
* Converts an integer to four bytes
* @param n - the integer
* @return - four bytes in an array
+ * @deprecated use toBytes(int,byte[],int)
*/
public static byte[] toBytes(int n) {
return toBytes(n,new byte[4],0);
@@ -371,6 +421,7 @@
* Converts an long to eight bytes
* @param n - the long
* @return - eight bytes in an array
+ * @deprecated use toBytes(long,byte[],int)
*/
public static byte[] toBytes(long n) {
return toBytes(n,new byte[8],0);
Modified: tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/mcast/McastMember.java
URL: http://svn.apache.org/viewcvs/tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/mcast/McastMember.java?rev=382040&r1=382039&r2=382040&view=diff
==============================================================================
--- tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/mcast/McastMember.java (original)
+++ tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/mcast/McastMember.java Wed Mar 1 06:54:35 2006
@@ -146,10 +146,21 @@
byte[] addr = host;
byte[] data = new byte[8+4+addr.length+4+domaind.length];
long alive=System.currentTimeMillis()-getServiceStartTime();
- System.arraycopy(XByteBuffer.toBytes((long)alive),0,data,0,8);
- System.arraycopy(XByteBuffer.toBytes(port),0,data,8,4);
+
+ //reduce byte copying
+ //System.arraycopy(XByteBuffer.toBytes((long)alive),0,data,0,8);
+ XByteBuffer.toBytes((long)alive,data,0);
+
+ //reduce byte copying
+ //System.arraycopy(XByteBuffer.toBytes(port),0,data,8,4);
+ XByteBuffer.toBytes(port,data,8);
+
System.arraycopy(addr,0,data,12,addr.length);
- System.arraycopy(XByteBuffer.toBytes(domaind.length),0,data,16,4);
+
+ //reduce byte copying
+ //System.arraycopy(XByteBuffer.toBytes(domaind.length),0,data,16,4);
+ XByteBuffer.toBytes(domaind.length,data,16);
+
System.arraycopy(domaind,0,data,20,domaind.length);
return data;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org