Skip to content

Commit 4e1514e

Browse files
committed
Don't memory map for small files
1 parent d238b88 commit 4e1514e

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

core/src/main/scala/org/apache/spark/storage/DiskStore.scala

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,27 @@ private class DiskStore(blockManager: BlockManager, diskManager: DiskBlockManage
8484
override def getBytes(blockId: BlockId): Option[ByteBuffer] = {
8585
val segment = diskManager.getBlockLocation(blockId)
8686
val channel = new RandomAccessFile(segment.file, "r").getChannel()
87-
val buffer = try {
88-
channel.map(MapMode.READ_ONLY, segment.offset, segment.length)
89-
} finally {
90-
channel.close()
87+
88+
val buffer =
89+
// For small files, directly read rather than memory map
90+
if (segment.length < 2 * 4096) {
91+
val buf = ByteBuffer.allocate(segment.length.toInt)
92+
try {
93+
channel.read(buf, segment.offset)
94+
}
95+
finally {
96+
channel.close()
97+
}
98+
Some(buf)
99+
} else {
100+
val buf = try {
101+
channel.map(MapMode.READ_ONLY, segment.offset, segment.length)
102+
} finally {
103+
channel.close()
104+
}
105+
Some(buf)
91106
}
92-
Some(buffer)
107+
buffer
93108
}
94109

95110
override def getValues(blockId: BlockId): Option[Iterator[Any]] = {

0 commit comments

Comments
 (0)