Add disabled code to use a cache for cso reading.

It doesn't seem like the cache is worth it, unfortunately.
This commit is contained in:
Unknown W. Brackets 2015-04-15 10:27:59 -07:00
parent 427fa039ba
commit 7b1214849a
2 changed files with 48 additions and 6 deletions

View File

@ -154,6 +154,9 @@ bool CsoFileReader::InitializeBuffers() {
void CsoFileReader::Close() { void CsoFileReader::Close() {
m_filename.Empty(); m_filename.Empty();
#if CSO_USE_CHUNKSCACHE
m_cache.Clear();
#endif
if (m_src) { if (m_src) {
fclose(m_src); fclose(m_src);
@ -193,18 +196,37 @@ int CsoFileReader::ReadSync(void* pBuffer, uint sector, uint count) {
int bytes = 0; int bytes = 0;
while (remaining > 0) { while (remaining > 0) {
int readBytes = ReadFromFrame(dest + bytes, pos + bytes, remaining); int readBytes;
if (readBytes == 0) {
// We hit EOF. #if CSO_USE_CHUNKSCACHE
break; // Try first to read from the cache.
readBytes = m_cache.Read(dest + bytes, pos + bytes, remaining);
#else
readBytes = -1;
#endif
if (readBytes < 0) {
readBytes = ReadFromFrame(dest + bytes, pos + bytes, remaining);
if (readBytes == 0) {
// We hit EOF.
break;
}
#if CSO_USE_CHUNKSCACHE
// Add the bytes into the cache. We need to allocate a buffer for it.
void *cached = malloc(readBytes);
memcpy(cached, dest + bytes, readBytes);
m_cache.Take(cached, pos + bytes, readBytes, readBytes);
#endif
} }
bytes += readBytes; bytes += readBytes;
remaining -= readBytes; remaining -= readBytes;
} }
return bytes; return bytes;
} }
int CsoFileReader::ReadFromFrame(u8 *dest, u64 pos, u64 maxBytes) { int CsoFileReader::ReadFromFrame(u8 *dest, u64 pos, int maxBytes) {
if (pos >= m_totalSize) { if (pos >= m_totalSize) {
// Can't read anything passed the end. // Can't read anything passed the end.
return 0; return 0;

View File

@ -15,11 +15,23 @@
#pragma once #pragma once
// Based on testing, the overhead of using this cache is high.
//
// The test was done with CSO files using a block size of 16KB.
// Cache hit rates were observed in the range of 25%.
// Cache overhead added 35% to the overall read time.
//
// For this reason, it's currently disabled.
#define CSO_USE_CHUNKSCACHE 0
#include "AsyncFileReader.h" #include "AsyncFileReader.h"
#include "ChunksCache.h"
struct CsoHeader; struct CsoHeader;
typedef struct z_stream_s z_stream; typedef struct z_stream_s z_stream;
static const uint CSO_CHUNKCACHE_SIZE_MB = 200;
class CsoFileReader : public AsyncFileReader class CsoFileReader : public AsyncFileReader
{ {
DeclareNoncopyableObject(CsoFileReader); DeclareNoncopyableObject(CsoFileReader);
@ -31,6 +43,9 @@ public:
m_totalSize(0), m_totalSize(0),
m_src(0), m_src(0),
m_z_stream(0), m_z_stream(0),
#if CSO_USE_CHUNKSCACHE
m_cache(CSO_CHUNKCACHE_SIZE_MB),
#endif
m_bytesRead(0) { m_bytesRead(0) {
m_blocksize = 2048; m_blocksize = 2048;
}; };
@ -59,7 +74,7 @@ private:
static bool ValidateHeader(const CsoHeader& hdr); static bool ValidateHeader(const CsoHeader& hdr);
bool ReadFileHeader(); bool ReadFileHeader();
bool InitializeBuffers(); bool InitializeBuffers();
int ReadFromFrame(u8 *dest, u64 pos, u64 maxBytes); int ReadFromFrame(u8 *dest, u64 pos, int maxBytes);
bool DecompressFrame(u32 frame, u32 readBufferSize); bool DecompressFrame(u32 frame, u32 readBufferSize);
u32 m_frameSize; u32 m_frameSize;
@ -73,6 +88,11 @@ private:
// The actual source cso file handle. // The actual source cso file handle.
FILE* m_src; FILE* m_src;
z_stream* m_z_stream; z_stream* m_z_stream;
#if CSO_USE_CHUNKSCACHE
ChunksCache m_cache;
#endif
// The result of a read is stored here between BeginRead() and FinishRead(). // The result of a read is stored here between BeginRead() and FinishRead().
int m_bytesRead; int m_bytesRead;
}; };