提问人:Gavin Ray 提问时间:9/11/2022 更新时间:9/11/2022 访问量:280
Kotlin:高性能并发文件 I/O 最佳实践?
Kotlin: High-Performance concurrent file I/O best-practices?
问:
在 Kotlin 中,以多读取器、单写入器方式允许并发文件 I/O 的最高性能方法是什么?
我有以下内容,但我不确定协程设施产生了多少开销:
AsyncFileChannel
,具有扩展函数以在上下文中使用它suspend
DiskManager
类,使用自定义ReadWriteMutex
搜索这方面的例子并不多(尝试在 Github 上搜索,有极少数 Kotlin 存储库实现了这一点)。ReadWriteMutex
class DiskManagerImpl(file: File) : DiskManager {
private val mutex = ReadWriteMutexImpl()
private val channel = AsynchronousFileChannel.open(file.toPath(),
StandardOpenOption.READ, StandardOpenOption.WRITE)
override suspend fun readPage(pageId: PageId, buffer: MemorySegment) = withContext(Dispatchers.IO) {
mutex.withReadLock {
val offset = pageId * PAGE_SIZE
val bytesRead = channel.readAsync(buffer.asByteBuffer(), offset.toLong())
require(bytesRead == PAGE_SIZE) { "Failed to read page $pageId" }
}
}
override suspend fun writePage(pageId: PageId, buffer: MemorySegment) = withContext(Dispatchers.IO) {
mutex.withWriteLock {
val offset = pageId * PAGE_SIZE
val bytesWritten = channel.writeAsync(buffer.asByteBuffer(), offset.toLong())
require(bytesWritten == PAGE_SIZE) { "Failed to write page $pageId" }
}
}
}
class ReadWriteMutexImpl : ReadWriteMutex {
private val read = Mutex()
private val write = Mutex()
private val readers = atomic(0)
override suspend fun lockRead() {
if (readers.getAndIncrement() == 0) {
read.lock()
}
}
override fun unlockRead() {
if (readers.decrementAndGet() == 0) {
read.unlock()
}
}
override suspend fun lockWrite() {
read.lock()
write.lock()
}
override fun unlockWrite() {
write.unlock()
read.unlock()
}
}
suspend inline fun <T> ReadWriteMutex.withReadLock(block: () -> T): T {
lockRead()
return try {
block()
} finally {
unlockRead()
}
}
suspend inline fun <T> ReadWriteMutex.withWriteLock(block: () -> T): T {
lockWrite()
return try {
block()
} finally {
unlockWrite()
}
}
答: 暂无答案
评论
AsynchronousFileChannel
AsynchronousFileChannel
Dispatchers.IO
channel.readAsync()
withContext(Dispatchers.IO)
Dispatchers.IO
AsynchronousFileChannel