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 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<atUint8[]> m_cacheData;
atInt32 m_blockSize;
atInt32 m_curBlock;

View File

@ -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;