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 | Avoid high mem alloc in TDSWriter#writeReader #1476

Merged
merged 3 commits into from
Jan 12, 2021
Merged
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
21 changes: 11 additions & 10 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3151,6 +3151,11 @@ boolean isEOMSent() {
private ByteBuffer socketBuffer;
private ByteBuffer logBuffer;

// Intermediate arrays
// It is assumed, startMessage is called before use, to alloc arrays
private char[] streamCharBuffer;
private byte[] streamByteBuffer;

private CryptoMetadata cryptoMeta = null;

TDSWriter(TDSChannel tdsChannel, SQLServerConnection con) {
Expand Down Expand Up @@ -3247,6 +3252,8 @@ void startMessage(TDSCommand command, byte tdsMessageType) throws SQLServerExcep
stagingBuffer = ByteBuffer.allocate(negotiatedPacketSize).order(ByteOrder.LITTLE_ENDIAN);
logBuffer = ByteBuffer.allocate(negotiatedPacketSize).order(ByteOrder.LITTLE_ENDIAN);
currentPacketSize = negotiatedPacketSize;
streamCharBuffer = new char[2 * currentPacketSize];
streamByteBuffer = new byte[4 * currentPacketSize];
}

((Buffer) socketBuffer).position(((Buffer) socketBuffer).limit());
Expand Down Expand Up @@ -4015,21 +4022,17 @@ void writeNonUnicodeReader(Reader reader, long advertisedLength, boolean isDestB
assert DataTypes.UNKNOWN_STREAM_LENGTH == advertisedLength || advertisedLength >= 0;

long actualLength = 0;
char[] streamCharBuffer = new char[currentPacketSize];
// The unicode version, writeReader() allocates a byte buffer that is 4 times the currentPacketSize, not sure
// why.
byte[] streamByteBuffer = new byte[currentPacketSize];
int charsRead = 0;
int charsToWrite;
int bytesToWrite;
String streamString;

do {
// Read in next chunk
for (charsToWrite = 0; -1 != charsRead && charsToWrite < streamCharBuffer.length;
for (charsToWrite = 0; -1 != charsRead && charsToWrite < currentPacketSize;
charsToWrite += charsRead) {
try {
charsRead = reader.read(streamCharBuffer, charsToWrite, streamCharBuffer.length - charsToWrite);
charsRead = reader.read(streamCharBuffer, charsToWrite, currentPacketSize - charsToWrite);
} catch (IOException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_errorReadingStream"));
Object[] msgArgs = {e.toString()};
Expand All @@ -4040,7 +4043,7 @@ void writeNonUnicodeReader(Reader reader, long advertisedLength, boolean isDestB
break;

// Check for invalid bytesRead returned from Reader.read
if (charsRead < 0 || charsRead > streamCharBuffer.length - charsToWrite) {
if (charsRead < 0 || charsRead > currentPacketSize - charsToWrite) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_errorReadingStream"));
Object[] msgArgs = {SQLServerException.getErrString("R_streamReadReturnedInvalidValue")};
error(form.format(msgArgs), SQLState.DATA_EXCEPTION_NOT_SPECIFIC, DriverError.NOT_SET);
Expand Down Expand Up @@ -4070,7 +4073,7 @@ void writeNonUnicodeReader(Reader reader, long advertisedLength, boolean isDestB
if (0 != charsToWrite)
bytesToWrite = charsToWrite / 2;

streamString = new String(streamCharBuffer);
streamString = new String(streamCharBuffer, 0, currentPacketSize);
byte[] bytes = ParameterUtils.HexToBin(streamString.trim());
writeInt(bytesToWrite);
writeBytes(bytes, 0, bytesToWrite);
Expand All @@ -4096,8 +4099,6 @@ void writeReader(Reader reader, long advertisedLength, boolean writeChunkSizes)
assert DataTypes.UNKNOWN_STREAM_LENGTH == advertisedLength || advertisedLength >= 0;

long actualLength = 0;
char[] streamCharBuffer = new char[2 * currentPacketSize];
byte[] streamByteBuffer = new byte[4 * currentPacketSize];
int charsRead = 0;
int charsToWrite;
do {
Expand Down