FileReader: Cache fileSize on open for performance

This commit is contained in:
Luke Street 2021-05-22 22:25:34 -04:00
parent a4074e1a57
commit 37c56eec07
2 changed files with 8 additions and 7 deletions

View File

@ -40,8 +40,7 @@ public:
void open(); void open();
void close(); void close();
bool isOpen() const { return m_fileHandle != 0; } bool isOpen() const { return m_fileHandle != nullptr; }
bool save();
void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current) override; void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current) override;
atUint64 position() const override; atUint64 position() const override;
atUint64 length() const override; atUint64 length() const override;
@ -64,6 +63,7 @@ protected:
std::string m_filename; std::string m_filename;
#endif #endif
HandleType m_fileHandle; HandleType m_fileHandle;
atUint64 m_fileSize;
std::unique_ptr<atUint8[]> m_cacheData; std::unique_ptr<atUint8[]> m_cacheData;
atInt32 m_blockSize; atInt32 m_blockSize;
atInt32 m_curBlock; atInt32 m_curBlock;

View File

@ -38,6 +38,8 @@ void FileReader::open() {
return; return;
} }
m_fileSize = utility::fileSize(m_filename);
// reset error // reset error
m_hasError = false; m_hasError = false;
} }
@ -112,7 +114,7 @@ atUint64 FileReader::length() const {
return 0; return 0;
} }
return utility::fileSize(m_filename); return m_fileSize;
} }
atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len) { atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len) {
@ -126,11 +128,10 @@ atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len) {
if (m_blockSize <= 0) if (m_blockSize <= 0)
return fread(buf, 1, len, m_fileHandle); return fread(buf, 1, len, m_fileHandle);
else { else {
atUint64 fs = utility::fileSize(m_filename); if (m_offset >= m_fileSize)
if (m_offset >= fs)
return 0; return 0;
if (m_offset + len >= fs) if (m_offset + len >= m_fileSize)
len = fs - m_offset; len = m_fileSize - m_offset;
size_t block = m_offset / m_blockSize; size_t block = m_offset / m_blockSize;
atUint64 cacheOffset = m_offset % m_blockSize; atUint64 cacheOffset = m_offset % m_blockSize;