Skip to content

Commit 026b497

Browse files
committed
Re-use a buffer in UnsafeShuffleWriter
1 parent 0748458 commit 026b497

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

core/src/main/java/org/apache/spark/shuffle/unsafe/UnsafeShuffleWriter.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ private long[] writeSortedRecordsToFile(
193193
int currentPartition = -1;
194194
BlockObjectWriter writer = null;
195195

196+
final byte[] arr = new byte[SER_BUFFER_SIZE];
196197
while (sortedRecords.hasNext()) {
197198
final RecordPointerAndKeyPrefix recordPointer = sortedRecords.next();
198199
final int partition = (int) recordPointer.keyPrefix;
@@ -211,16 +212,14 @@ private long[] writeSortedRecordsToFile(
211212
final Object baseObject = memoryManager.getPage(recordPointer.recordPointer);
212213
final long baseOffset = memoryManager.getOffsetInPage(recordPointer.recordPointer);
213214
final int recordLength = (int) PlatformDependent.UNSAFE.getLong(baseObject, baseOffset + 8);
214-
// TODO: re-use a buffer or avoid double-buffering entirely
215-
final byte[] arr = new byte[recordLength];
216-
PlatformDependent.copyMemory(
217-
baseObject,
218-
baseOffset + 16,
219-
arr,
220-
PlatformDependent.BYTE_ARRAY_OFFSET,
221-
recordLength);
215+
PlatformDependent.copyMemory(
216+
baseObject,
217+
baseOffset + 16,
218+
arr,
219+
PlatformDependent.BYTE_ARRAY_OFFSET,
220+
recordLength);
222221
assert (writer != null); // To suppress an IntelliJ warning
223-
writer.write(arr);
222+
writer.write(arr, 0, recordLength);
224223
// TODO: add a test that detects whether we leave this call out:
225224
writer.recordWritten();
226225
}

0 commit comments

Comments
 (0)