Permalink
Browse files

StdInClient with static arrays and zero copy

  • Loading branch information...
pzemtsov committed Jan 29, 2015
1 parent 854a9a8 commit cd1153d9d1e821e090bb25c74618cc45210abf30
Showing with 13 additions and 8 deletions.
  1. +13 −8 StdInClient.java
View
@@ -5,6 +5,11 @@
public class StdInClient
{
+ static byte[] type = new byte[4];
+ static byte[] msg = new byte [100];
+ static byte[] tmp = new byte [4];
+
+
public static void main (String [] args) throws Exception
{
InputStream in = System.in;
@@ -13,10 +18,10 @@ public static void main (String [] args) throws Exception
long sum = 0;
int N = 10000000;
for (int i = 0; i < N; i++) {
- byte [] type = readBytes (in, 4);
+ readBytes (in, type, 4);
int len = readInt (in);
- byte [] msg = readBytes (in, len);
- processMessage (type, msg);
+ readBytes (in, msg, len);
+ processMessage (type, msg, len);
sum += msg.length + 8;
}
long t1 = System.currentTimeMillis ();
@@ -26,25 +31,25 @@ public static void main (String [] args) throws Exception
}
}
- private static byte [] readBytes (InputStream in, int expectedSize) throws IOException
+ private static void readBytes (InputStream in, byte[] buffer, int expectedSize) throws IOException
{
- final byte[] buffer = new byte[expectedSize];
int totalReadSize = 0;
while (totalReadSize < expectedSize) {
int readSize = in.read(buffer, totalReadSize, expectedSize - totalReadSize);
if (readSize < 0) throw new EOFException ();
totalReadSize += readSize;
}
- return buffer;
}
private static final int readInt (InputStream in) throws IOException
{
- byte [] b = readBytes (in, 4);
+ byte[] b = tmp;
+ readBytes (in, b, 4);
return (((b[0] & 0xFF) << 24) + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8) + ((b[3] & 0xFF) << 0));
}
- private static void processMessage (byte [] type, byte [] msg)
+ private static void processMessage (byte [] type, byte [] msg, int len)
{
}
+
}

0 comments on commit cd1153d

Please sign in to comment.