Skip to content

Commit 38b3036

Browse files
committed
Allocate buffers in TdsEncoder as late as possible.
TdsEncoder.writeChunkedMessage(…) now allocates a buffer to write when it is actually needed. Previously, we allocated buffers eagerly without considering that the buffer might be not needed at all. [resolves #195] Signed-off-by: Mark Paluch <mpaluch@vmware.com>
1 parent a51ff11 commit 38b3036

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/main/java/io/r2dbc/mssql/client/TdsEncoder.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,10 @@ private void writeChunkedMessage(ChannelHandlerContext ctx, ChannelPromise promi
262262
try {
263263
while (body.readableBytes() > 0) {
264264

265-
ByteBuf chunk = body.alloc().buffer(estimateChunkSize(getBytesToWrite(body.readableBytes())));
266-
267265
if (this.lastChunkRemainder != null) {
268266

267+
ByteBuf chunk = body.alloc().buffer(estimateChunkSize(getBytesToWrite(body.readableBytes())));
268+
269269
int combinedSize = this.lastChunkRemainder.readableBytes() + body.readableBytes();
270270
HeaderOptions optionsToUse = isLastTransportPacket(combinedSize, lastLogicalPacket) ? getLastHeader(headerOptions) : getChunkedHeaderOptions(headerOptions);
271271
Header.encode(chunk, optionsToUse, this.packetSize, this.packetIdProvider);
@@ -277,6 +277,7 @@ private void writeChunkedMessage(ChannelHandlerContext ctx, ChannelPromise promi
277277
this.lastChunkRemainder.release();
278278
this.lastChunkRemainder = null;
279279

280+
combiner.add(ctx.write(chunk, ctx.newPromise()));
280281
} else {
281282

282283
if (!lastLogicalPacket && !requiresChunking(body.readableBytes())) {
@@ -287,15 +288,15 @@ private void writeChunkedMessage(ChannelHandlerContext ctx, ChannelPromise promi
287288
break;
288289
}
289290

291+
ByteBuf chunk = body.alloc().buffer(estimateChunkSize(getBytesToWrite(body.readableBytes())));
290292
HeaderOptions optionsToUse = isLastTransportPacket(body.readableBytes(), lastLogicalPacket) ? getLastHeader(headerOptions) : getChunkedHeaderOptions(headerOptions);
291293

292294
int byteCount = getEffectiveChunkSizeWithoutHeader(body.readableBytes());
293295
Header.encode(chunk, optionsToUse, Header.LENGTH + byteCount, this.packetIdProvider);
294296

295297
chunk.writeBytes(body, byteCount);
298+
combiner.add(ctx.write(chunk, ctx.newPromise()));
296299
}
297-
298-
combiner.add(ctx.write(chunk, ctx.newPromise()));
299300
}
300301

301302
combiner.finish(promise);

0 commit comments

Comments
 (0)