From 37c56eec076d74cc7708ff14c66ee960d7f53e2a Mon Sep 17 00:00:00 2001 From: Luke Street Date: Sat, 22 May 2021 22:25:34 -0400 Subject: [PATCH] FileReader: Cache fileSize on open for performance --- include/athena/FileReader.hpp | 4 ++-- src/athena/FileReader.cpp | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/athena/FileReader.hpp b/include/athena/FileReader.hpp index dd6044e..feefc09 100644 --- a/include/athena/FileReader.hpp +++ b/include/athena/FileReader.hpp @@ -40,8 +40,7 @@ public: void open(); void close(); - bool isOpen() const { return m_fileHandle != 0; } - bool save(); + bool isOpen() const { return m_fileHandle != nullptr; } void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current) override; atUint64 position() const override; atUint64 length() const override; @@ -64,6 +63,7 @@ protected: std::string m_filename; #endif HandleType m_fileHandle; + atUint64 m_fileSize; std::unique_ptr m_cacheData; atInt32 m_blockSize; atInt32 m_curBlock; diff --git a/src/athena/FileReader.cpp b/src/athena/FileReader.cpp index 164dd8d..b230168 100644 --- a/src/athena/FileReader.cpp +++ b/src/athena/FileReader.cpp @@ -38,6 +38,8 @@ void FileReader::open() { return; } + m_fileSize = utility::fileSize(m_filename); + // reset error m_hasError = false; } @@ -112,7 +114,7 @@ atUint64 FileReader::length() const { return 0; } - return utility::fileSize(m_filename); + return m_fileSize; } atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len) { @@ -126,11 +128,10 @@ atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len) { if (m_blockSize <= 0) return fread(buf, 1, len, m_fileHandle); else { - atUint64 fs = utility::fileSize(m_filename); - if (m_offset >= fs) + if (m_offset >= m_fileSize) return 0; - if (m_offset + len >= fs) - len = fs - m_offset; + if (m_offset + len >= m_fileSize) + len = m_fileSize - m_offset; size_t block = m_offset / m_blockSize; atUint64 cacheOffset = m_offset % m_blockSize;