Disk: NeoMemory: Cache last read sector for one second
This prevents constant re-reads if reading in small chunksv0.3.0-dev
parent
4ef8fe8794
commit
4c9d6c5ee7
|
|
@ -12,30 +12,37 @@ optional<uint64_t> NeoMemoryDiskReadDriver::readLogicalDiskAligned(Communication
|
||||||
if(amount != SectorSize)
|
if(amount != SectorSize)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
const uint64_t currentSector = pos / SectorSize;
|
if(cachePos != pos || std::chrono::steady_clock::now() > cachedAt + CacheTime) {
|
||||||
auto msg = com.waitForMessageSync([¤tSector, &com] {
|
// The cache does not have this data, go get it
|
||||||
return com.sendCommand(Command::NeoReadMemory, {
|
const uint64_t currentSector = pos / SectorSize;
|
||||||
MemoryTypeSD,
|
auto msg = com.waitForMessageSync([¤tSector, &com] {
|
||||||
uint8_t(currentSector & 0xFF),
|
return com.sendCommand(Command::NeoReadMemory, {
|
||||||
uint8_t((currentSector >> 8) & 0xFF),
|
MemoryTypeSD,
|
||||||
uint8_t((currentSector >> 16) & 0xFF),
|
uint8_t(currentSector & 0xFF),
|
||||||
uint8_t((currentSector >> 24) & 0xFF),
|
uint8_t((currentSector >> 8) & 0xFF),
|
||||||
uint8_t(SectorSize & 0xFF),
|
uint8_t((currentSector >> 16) & 0xFF),
|
||||||
uint8_t((SectorSize >> 8) & 0xFF),
|
uint8_t((currentSector >> 24) & 0xFF),
|
||||||
uint8_t((SectorSize >> 16) & 0xFF),
|
uint8_t(SectorSize & 0xFF),
|
||||||
uint8_t((SectorSize >> 24) & 0xFF)
|
uint8_t((SectorSize >> 8) & 0xFF),
|
||||||
});
|
uint8_t((SectorSize >> 16) & 0xFF),
|
||||||
}, NeoMemorySDRead, timeout);
|
uint8_t((SectorSize >> 24) & 0xFF)
|
||||||
|
});
|
||||||
|
}, NeoMemorySDRead, timeout);
|
||||||
|
|
||||||
if(!msg)
|
if(!msg)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
const auto sdmsg = std::dynamic_pointer_cast<NeoReadMemorySDMessage>(msg);
|
const auto sdmsg = std::dynamic_pointer_cast<NeoReadMemorySDMessage>(msg);
|
||||||
if(!sdmsg || sdmsg->data.size() != SectorSize) {
|
if(!sdmsg || sdmsg->data.size() != SectorSize) {
|
||||||
report(APIEvent::Type::PacketDecodingError, APIEvent::Severity::Error);
|
report(APIEvent::Type::PacketDecodingError, APIEvent::Severity::Error);
|
||||||
return nullopt;
|
return nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(cache.data(), sdmsg->data.data(), SectorSize);
|
||||||
|
cachedAt = std::chrono::steady_clock::now();
|
||||||
|
cachePos = pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(into, sdmsg->data.data(), SectorSize);
|
memcpy(into, cache.data(), SectorSize);
|
||||||
return SectorSize;
|
return SectorSize;
|
||||||
}
|
}
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
#include "icsneo/disk/diskreaddriver.h"
|
#include "icsneo/disk/diskreaddriver.h"
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
namespace icsneo {
|
namespace icsneo {
|
||||||
|
|
||||||
|
|
@ -26,6 +27,11 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr const uint8_t MemoryTypeSD = 0x01; // Logical Disk
|
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,
|
optional<uint64_t> readLogicalDiskAligned(Communication& com, device_eventhandler_t report,
|
||||||
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout) override;
|
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout) override;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue