From 4e1514e666624d2285b190ff1419a9fae2ae929f Mon Sep 17 00:00:00 2001 From: Patrick Wendell Date: Wed, 26 Feb 2014 18:34:08 -0800 Subject: [PATCH] Don't memory map for small files --- .../org/apache/spark/storage/DiskStore.scala | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/core/src/main/scala/org/apache/spark/storage/DiskStore.scala b/core/src/main/scala/org/apache/spark/storage/DiskStore.scala index d1f07ddb24bb2..4e0594ab81faf 100644 --- a/core/src/main/scala/org/apache/spark/storage/DiskStore.scala +++ b/core/src/main/scala/org/apache/spark/storage/DiskStore.scala @@ -84,12 +84,27 @@ private class DiskStore(blockManager: BlockManager, diskManager: DiskBlockManage override def getBytes(blockId: BlockId): Option[ByteBuffer] = { val segment = diskManager.getBlockLocation(blockId) val channel = new RandomAccessFile(segment.file, "r").getChannel() - val buffer = try { - channel.map(MapMode.READ_ONLY, segment.offset, segment.length) - } finally { - channel.close() + + val buffer = + // For small files, directly read rather than memory map + if (segment.length < 2 * 4096) { + val buf = ByteBuffer.allocate(segment.length.toInt) + try { + channel.read(buf, segment.offset) + } + finally { + channel.close() + } + Some(buf) + } else { + val buf = try { + channel.map(MapMode.READ_ONLY, segment.offset, segment.length) + } finally { + channel.close() + } + Some(buf) } - Some(buffer) + buffer } override def getValues(blockId: BlockId): Option[Iterator[Any]] = {