Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

using bytelist instead of a linkedlist for a better performance #693

Merged
merged 1 commit into from

2 participants

@josedonizetti
Collaborator

No description provided.

@headius
Owner

Hmm, looks like something's not quite right in there...

@josedonizetti
Collaborator

@headius Think it is ok now. Thanks.

@headius headius merged commit f4312ae into jruby:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 34 additions and 20 deletions.
  1. +34 −20 src/org/jruby/util/io/ChannelStream.java
View
54 src/org/jruby/util/io/ChannelStream.java
@@ -104,7 +104,7 @@
protected boolean reading; // are we reading or writing?
private ChannelDescriptor descriptor;
private boolean blocking = true;
- private LinkedList<Byte> ungotChars = new LinkedList<Byte>();
+ private ByteList ungotChars = new ByteList();
private volatile boolean closedExplicitly = false;
private volatile boolean eof = false;
@@ -187,7 +187,7 @@ public boolean readDataBuffered() {
}
private boolean hasUngotChars() {
- return ungotChars.size() > 0;
+ return ungotChars.length() > 0;
}
public boolean writeDataBuffered() {
@@ -292,12 +292,13 @@ public synchronized int getline(ByteList dst, byte terminator) throws IOExceptio
boolean found = false;
if (hasUngotChars()) {
- Byte ungotc;
- while((ungotc = ungotChars.poll()) != null){
+ for(int i = 0; i < ungotChars.length(); i++){
+ byte ungotc = (byte) ungotChars.get(i);
dst.append(ungotc);
found = ungotc == terminator;
++totalRead;
}
+ clearUngotChars();
}
while (!found) {
@@ -335,13 +336,14 @@ public synchronized int getline(ByteList dst, byte terminator, long limit) throw
boolean found = false;
if (hasUngotChars()) {
- Byte ungotc;
- while((ungotc = ungotChars.poll()) != null){
+ for(int i = 0; i < ungotChars.length(); i++){
+ byte ungotc = (byte) ungotChars.get(i);
dst.append(ungotc);
found = ungotc == terminator;
limit--;
++totalRead;
}
+ clearUngotChars();
}
while (!found) {
@@ -373,6 +375,15 @@ public synchronized int getline(ByteList dst, byte terminator, long limit) throw
}
/**
+ *
+ */
+ private void clearUngotChars() {
+ if(ungotChars.length() > 0) {
+ ungotChars.delete(0, ungotChars.length());
+ }
+ }
+
+ /**
* @deprecated readall do busy loop for the IO which has NONBLOCK bit. You
* should implement the logic by yourself with fread().
*/
@@ -465,12 +476,13 @@ public synchronized ByteList readall() throws IOException, BadDescriptorExceptio
*/
private final int copyBufferedBytes(ByteBuffer dst) {
final int bytesToCopy = dst.remaining();
-
+
if (hasUngotChars() && dst.hasRemaining()) {
- Byte ungotc;
- while((ungotc = ungotChars.poll()) != null){
+ for(int i = 0; i < ungotChars.length(); i++){
+ byte ungotc = (byte) ungotChars.get(i);
dst.put(ungotc);
}
+ clearUngotChars();
}
if (buffer.hasRemaining() && dst.hasRemaining()) {
@@ -505,11 +517,12 @@ private final int copyBufferedBytes(byte[] dst, int off, int len) {
int bytesCopied = 0;
if (hasUngotChars() && len > 0) {
- Byte ungotc;
- while((ungotc = ungotChars.poll()) != null){
+ for(int i = 0; i < ungotChars.length(); i++){
+ byte ungotc = (byte) ungotChars.get(i);
dst[off++] = ungotc;
++bytesCopied;
}
+ clearUngotChars();
}
final int n = Math.min(len - bytesCopied, buffer.remaining());
@@ -531,12 +544,13 @@ private final int copyBufferedBytes(ByteList dst, int len) {
dst.ensure(Math.min(len, bufferedInputBytesRemaining()));
- if (bytesCopied < len && hasUngotChars()) {
- Byte ungotc;
- while((ungotc = ungotChars.poll()) != null){
+ if (hasUngotChars() && hasUngotChars()) {
+ for(int i = 0; i < ungotChars.length(); i++){
+ byte ungotc = (byte) ungotChars.get(i);
++bytesCopied;
dst.append(ungotc);
}
+ clearUngotChars();
}
//
@@ -557,7 +571,7 @@ private final int copyBufferedBytes(ByteList dst, int len) {
* @return The number of bytes that can be read without reading the underlying stream.
*/
private final int bufferedInputBytesRemaining() {
- return reading ? (buffer.remaining() + (ungotChars.size())) : 0;
+ return reading ? (buffer.remaining() + (ungotChars.length())) : 0;
}
/**
@@ -745,7 +759,7 @@ public synchronized long fgetpos() throws IOException, PipeException, InvalidVal
// Adjust for buffered data
if (reading) {
pos -= buffer.remaining();
- return pos - (pos > 0 && hasUngotChars() ? 1 : 0);
+ return pos - (pos > 0 && hasUngotChars() ? ungotChars.length() : 0);
} else {
return pos + buffer.position();
}
@@ -767,7 +781,7 @@ public synchronized long fgetpos() throws IOException, PipeException, InvalidVal
public synchronized void lseek(long offset, int type) throws IOException, InvalidValueException, PipeException, BadDescriptorException {
if (descriptor.isSeekable()) {
FileChannel fileChannel = (FileChannel)descriptor.getChannel();
- ungotChars.clear();
+ clearUngotChars();
int adj = 0;
if (reading) {
// for SEEK_CUR, need to adjust for buffered data
@@ -1232,7 +1246,7 @@ public int ungetc(int c) {
eof = false;
// save the ungot
- ungotChars.push((byte) c);
+ ungotChars.prepend((byte)c);
return c;
}
@@ -1368,8 +1382,8 @@ public synchronized int read() throws IOException, BadDescriptorException {
descriptor.checkOpen();
if (hasUngotChars()) {
- int c = ungotChars.poll();
- ungotChars.clear();
+ int c = ungotChars.get(0);
+ ungotChars.delete(0,1);
return c;
}
Something went wrong with that request. Please try again.