Skip to content
This repository
Browse code

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...
commit 582b0f84459dff96e79be76a4120d90ae502d87d 1 parent 629211b
Charles Oliver Nutter authored August 02, 2012
BIN  spec/grammar/java_signature/helpers/.signature_parser_helper.rb.rej.swp
Binary file not shown
BIN  spec/grammar/java_signature/helpers/.signature_parser_helper.rb.swp
Binary file not shown
19  src/org/jruby/util/io/ChannelStream.java
@@ -90,6 +90,13 @@
90 90
     private final static int BULK_READ_SIZE = 16 * 1024;
91 91
     private final static ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
92 92
 
  93
+    /**
  94
+     * A cached EOFException. Since EOFException is only used by us internally,
  95
+     * we create a single instance to avoid stack trace generation. Comment out
  96
+     * the initialization of this field to cause a new one each time.
  97
+     */
  98
+    private static EOFException eofException = new EOFException();
  99
+
93 100
     private volatile Ruby runtime;
94 101
     protected ModeFlags modes;
95 102
     protected boolean sync = false;
@@ -933,13 +940,21 @@ private int bufferedRead(ByteList dst, int number) throws IOException, BadDescri
933 940
 
934 941
         if (bytesRead == 0 && number != 0) {
935 942
             if (eof) {
936  
-                throw new EOFException();
  943
+                throw newEOFException();
937 944
             }
938 945
         }
939 946
 
940 947
         return bytesRead;
941 948
     }
942 949
 
  950
+    private EOFException newEOFException() {
  951
+        if (eofException != null) {
  952
+            return eofException;
  953
+        } else {
  954
+            return new EOFException();
  955
+        }
  956
+    }
  957
+
943 958
     private int bufferedRead(ByteBuffer dst, boolean partial) throws IOException, BadDescriptorException {
944 959
         checkReadable();
945 960
         ensureRead();
@@ -1003,7 +1018,7 @@ private int bufferedRead(ByteBuffer dst, boolean partial) throws IOException, Ba
1003 1018
         }
1004 1019
 
1005 1020
         if (eof && bytesRead == 0 && dst.remaining() != 0) {
1006  
-            throw new EOFException();
  1021
+            throw newEOFException();
1007 1022
         }
1008 1023
 
1009 1024
         return bytesRead;

0 notes on commit 582b0f8

Please sign in to comment.
Something went wrong with that request. Please try again.