Skip to content

Commit

Permalink
Don't memory map for small files
Browse files Browse the repository at this point in the history
  • Loading branch information
pwendell committed Feb 27, 2014
1 parent d238b88 commit 4e1514e
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions core/src/main/scala/org/apache/spark/storage/DiskStore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]] = {
Expand Down

0 comments on commit 4e1514e

Please sign in to comment.