Skip to content

Commit

Permalink
Eliminate allocation in binwrite plus other optz
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Mar 2, 2021
1 parent 241347d commit f3e8f3b
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions core/src/main/java/org/jruby/util/io/OpenFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -2236,13 +2236,6 @@ public ByteList doWriteconv(ThreadContext context, byte[] bytes, int start, int
return newBytes;
}

private static class BinwriteArg {
OpenFile fptr;
byte[] ptrBytes;
int ptr;
int length;
}

// io_binwrite
public int binwriteInt(ThreadContext context, byte[] ptrBytes, int ptr, int len, boolean nosync) {
int n, r, offset = 0;
Expand All @@ -2253,7 +2246,7 @@ public int binwriteInt(ThreadContext context, byte[] ptrBytes, int ptr, int len,
boolean locked = lock();
try {
if ((n = len) <= 0) return n;
if (wbuf.ptr == null && !(!nosync && (mode & SYNC) != 0)) {
if (wbuf.ptr == null && (nosync || (mode & SYNC) == 0)) {
wbuf.off = 0;
wbuf.len = 0;
wbuf.capa = IO_WBUF_CAPA_MIN;
Expand All @@ -2267,8 +2260,6 @@ public int binwriteInt(ThreadContext context, byte[] ptrBytes, int ptr, int len,
// if the write buffer does not have enough capacity to store all incoming data...unbuffered write
if ((!nosync && (mode & (SYNC | TTY)) != 0) ||
(wbuf.ptr != null && wbuf.capa <= wbuf.len + len)) {
BinwriteArg arg = new BinwriteArg();

if (wbuf.len != 0 && wbuf.len + len <= wbuf.capa) {
if (wbuf.capa < wbuf.off + wbuf.len + len) {
System.arraycopy(wbuf.ptr, wbuf.off, wbuf.ptr, 0, wbuf.len);
Expand All @@ -2283,18 +2274,17 @@ public int binwriteInt(ThreadContext context, byte[] ptrBytes, int ptr, int len,
if (n == 0) return len;

checkClosed();
arg.fptr = this;
OpenFile fptr = this;
retry:
while (true) {
arg.ptrBytes = ptrBytes;
arg.ptr = ptr + offset;
arg.length = n;
int start = ptr + offset;
int length = n;
if (write_lock != null) {
// FIXME: not interruptible by Ruby
// r = rb_mutex_synchronize(fptr->write_lock, io_binwrite_string, (VALUE)&arg);
write_lock.writeLock().lock();
try {
r = binwriteString(context, arg);
r = binwriteString(fptr, ptrBytes, start, length);
} finally {
write_lock.writeLock().unlock();
}
Expand Down Expand Up @@ -2332,10 +2322,9 @@ public int binwriteInt(ThreadContext context, byte[] ptrBytes, int ptr, int len,
}

// io_binwrite_string
static int binwriteString(ThreadContext context, BinwriteArg arg) {
BinwriteArg p = arg;
int l = p.fptr.writableLength(p.length);
return p.fptr.writeInternal2(p.fptr.fd, p.ptrBytes, p.ptr, l);
static int binwriteString(OpenFile fptr, byte[] bytes, int start, int length) {
int l = fptr.writableLength(length);
return fptr.writeInternal2(fptr.fd, bytes, start, l);
}

public static class InternalWriteStruct {
Expand Down

0 comments on commit f3e8f3b

Please sign in to comment.