Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use fast calculation for totalRemaining number of bytes from multiple ByteBuffers #2633

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@
import com.google.storage.v2.ReadObjectResponse;
import java.io.Closeable;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ScatteringByteChannel;
import java.util.Arrays;
import java.util.Iterator;

final class GapicUnbufferedReadableByteChannel
Expand Down Expand Up @@ -80,7 +78,7 @@ public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
throw new ClosedChannelException();
}

long totalBufferCapacity = Arrays.stream(dsts).mapToLong(Buffer::remaining).sum();
long totalBufferCapacity = Buffers.totalRemaining(dsts, offset, length);
ReadCursor c = new ReadCursor(blobOffset, blobOffset + totalBufferCapacity);
while (c.hasRemaining()) {
if (leftovers != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.GatheringByteChannel;
Expand Down Expand Up @@ -173,7 +172,7 @@ private static final class ByteBufferContent extends RewindableContent {
private ByteBufferContent(ByteBuffer[] buffers) {
this.buffers = buffers;
this.positions = Arrays.stream(buffers).mapToInt(Buffers::position).toArray();
this.totalLength = Arrays.stream(buffers).mapToLong(Buffer::remaining).sum();
this.totalLength = Buffers.totalRemaining(buffers, 0, buffers.length);
this.dirty = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@

import com.google.common.base.MoreObjects;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.WritableByteChannel;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
Expand Down Expand Up @@ -262,7 +260,7 @@ public int write(ByteBuffer src) throws IOException {
@Override
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
boolean exception = false;
long available = Arrays.stream(srcs).mapToLong(Buffer::remaining).sum();
long available = Buffers.totalRemaining(srcs, offset, length);
Instant begin = clock.instant();
try {
return delegate.write(srcs, offset, length);
Expand All @@ -271,7 +269,7 @@ public long write(ByteBuffer[] srcs, int offset, int length) throws IOException
throw e;
} finally {
Instant end = clock.instant();
long remaining = Arrays.stream(srcs).mapToLong(Buffer::remaining).sum();
long remaining = Buffers.totalRemaining(srcs, offset, length);
Record record = Record.of(available - remaining, begin, end, exception);
sink.recordThroughput(record);
}
Expand All @@ -280,7 +278,7 @@ public long write(ByteBuffer[] srcs, int offset, int length) throws IOException
@Override
public long write(ByteBuffer[] srcs) throws IOException {
boolean exception = false;
long available = Arrays.stream(srcs).mapToLong(Buffer::remaining).sum();
long available = Buffers.totalRemaining(srcs, 0, srcs.length);
Instant begin = clock.instant();
try {
return delegate.write(srcs);
Expand All @@ -289,7 +287,7 @@ public long write(ByteBuffer[] srcs) throws IOException {
throw e;
} finally {
Instant end = clock.instant();
long remaining = Arrays.stream(srcs).mapToLong(Buffer::remaining).sum();
long remaining = Buffers.totalRemaining(srcs, 0, srcs.length);
Record record = Record.of(available - remaining, begin, end, exception);
sink.recordThroughput(record);
}
Expand Down
Loading