Permalink
Browse files

Use a cached EOFException rather than constructing new.

Exception creation (or more correctly, stack trace generation) is
not cheap on the JVM. By creating a new EOFException for every
read EOF, we introduce a lot of overhead into processes that open
many small files and read them to completion. The EOFExceptions we
raise are also immediately caught and turned into appropriate
Ruby errors or return values. In order to reduce the cost of EOF
reads, I've introduced a single cached EOFException we throw for
all EOF events. Commenting out its initialization in
ChannelStream will return the original behavior, if necessary for
debugging.
  • Loading branch information...
1 parent 629211b commit 582b0f84459dff96e79be76a4120d90ae502d87d @headius headius committed Aug 2, 2012
@@ -90,6 +90,13 @@
private final static int BULK_READ_SIZE = 16 * 1024;
private final static ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
+ /**
+ * A cached EOFException. Since EOFException is only used by us internally,
+ * we create a single instance to avoid stack trace generation. Comment out
+ * the initialization of this field to cause a new one each time.
+ */
+ private static EOFException eofException = new EOFException();
+
private volatile Ruby runtime;
protected ModeFlags modes;
protected boolean sync = false;
@@ -933,13 +940,21 @@ private int bufferedRead(ByteList dst, int number) throws IOException, BadDescri
if (bytesRead == 0 && number != 0) {
if (eof) {
- throw new EOFException();
+ throw newEOFException();
}
}
return bytesRead;
}
+ private EOFException newEOFException() {
+ if (eofException != null) {
+ return eofException;
+ } else {
+ return new EOFException();
+ }
+ }
+
private int bufferedRead(ByteBuffer dst, boolean partial) throws IOException, BadDescriptorException {
checkReadable();
ensureRead();
@@ -1003,7 +1018,7 @@ private int bufferedRead(ByteBuffer dst, boolean partial) throws IOException, Ba
}
if (eof && bytesRead == 0 && dst.remaining() != 0) {
- throw new EOFException();
+ throw newEOFException();
}
return bytesRead;

0 comments on commit 582b0f8

Please sign in to comment.