Update of /cvsroot/freenet/freenet/src/freenet
In directory sc8-pr-cvs1:/tmp/cvs-serv12430/src/freenet
Modified Files:
PeerPacketMessage.java
Log Message:
Use a pool of ByteArrayOutputStream:s instead of keeping one single static one, avoid
quite evil blocking of other threads.
Index: PeerPacketMessage.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/PeerPacketMessage.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -w -r1.18 -r1.19
--- PeerPacketMessage.java 24 Oct 2003 02:24:57 -0000 1.18
+++ PeerPacketMessage.java 27 Oct 2003 15:20:03 -0000 1.19
@@ -4,6 +4,7 @@
import freenet.support.Logger;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.util.LinkedList;
/**
* PeerPacketMessage - a message to be sent to a particular peer node.
@@ -15,7 +16,7 @@
final MessageSendCallback cb;
final PeerHandler ph;
final Identity id;
- static ByteArrayOutputStream bais = new ByteArrayOutputStream();
+ static BAOSPool baosPool = new BAOSPool(); //Use a pool to avoid memory churn, no
other reason
boolean finished = false;
final long startTime;
final long expiryTime;
@@ -73,12 +74,12 @@
}
this.raw = msg.toRawMessage(p, ph);
try {
- synchronized (bais) {
- bais.reset();
- raw.writeMessage(bais);
- bais.flush();
- this.content = bais.toByteArray();
- }
+ ByteArrayOutputStream baos = baosPool.get();
+ baos.reset();
+ raw.writeMessage(baos);
+ baos.flush();
+ this.content = baos.toByteArray();
+ baosPool.release(baos);
} catch (IOException e) {
Core.logger.log(this, "Impossible exception: " + e + " writing
message " + raw + "," + cb + " to BAIS", Logger.ERROR);
throw new IllegalStateException("Impossible exception!: " + e);
@@ -171,6 +172,20 @@
} catch (Throwable t) {
Core.logger.log(this, toString() + ".notifyFailure(" + detail
+ ") caught " + t, t, Logger.ERROR);
Core.logger.log(this, toString() + ".notifyFailure: detail was
" + detail, detail, Logger.ERROR);
+ }
+ }
+ public static class BAOSPool {
+ private LinkedList lPool = new LinkedList();
+ synchronized ByteArrayOutputStream get()
+ {
+ if(lPool.size()>0)
+ return (ByteArrayOutputStream)lPool.removeFirst();
+ else
+ return new ByteArrayOutputStream();
+ }
+ synchronized void release(ByteArrayOutputStream s)
+ {
+ lPool.add(s);
}
}
}
_______________________________________________
cvs mailing list
[EMAIL PROTECTED]
http://dodo.freenetproject.org/cgi-bin/mailman/listinfo/cvs