mirror of
https://github.com/intrepidcs/libicsneo.git
synced 2026-08-05 01:18:36 +02:00
Disk: NeoMemory: Cache last read sector for one second
This prevents constant re-reads if reading in small chunks
This commit is contained in:
@@ -12,6 +12,8 @@ optional<uint64_t> NeoMemoryDiskReadDriver::readLogicalDiskAligned(Communication
|
||||
if(amount != SectorSize)
|
||||
return 0;
|
||||
|
||||
if(cachePos != pos || std::chrono::steady_clock::now() > cachedAt + CacheTime) {
|
||||
// The cache does not have this data, go get it
|
||||
const uint64_t currentSector = pos / SectorSize;
|
||||
auto msg = com.waitForMessageSync([¤tSector, &com] {
|
||||
return com.sendCommand(Command::NeoReadMemory, {
|
||||
@@ -36,6 +38,11 @@ optional<uint64_t> NeoMemoryDiskReadDriver::readLogicalDiskAligned(Communication
|
||||
return nullopt;
|
||||
}
|
||||
|
||||
memcpy(into, sdmsg->data.data(), SectorSize);
|
||||
memcpy(cache.data(), sdmsg->data.data(), SectorSize);
|
||||
cachedAt = std::chrono::steady_clock::now();
|
||||
cachePos = pos;
|
||||
}
|
||||
|
||||
memcpy(into, cache.data(), SectorSize);
|
||||
return SectorSize;
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "icsneo/disk/diskreaddriver.h"
|
||||
#include <limits>
|
||||
#include <chrono>
|
||||
|
||||
namespace icsneo {
|
||||
|
||||
@@ -26,6 +27,11 @@ public:
|
||||
|
||||
private:
|
||||
static constexpr const uint8_t MemoryTypeSD = 0x01; // Logical Disk
|
||||
static constexpr const std::chrono::duration CacheTime = std::chrono::seconds(1);
|
||||
|
||||
std::array<uint8_t, SectorSize> cache;
|
||||
uint64_t cachePos = 0;
|
||||
std::chrono::time_point<std::chrono::steady_clock> cachedAt;
|
||||
|
||||
optional<uint64_t> readLogicalDiskAligned(Communication& com, device_eventhandler_t report,
|
||||
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout) override;
|
||||
|
||||
Reference in New Issue
Block a user