ByteStream: Devirtualize where unnecessary

This commit is contained in:
Connor McLaughlin 2022-08-18 21:16:07 +10:00
parent c66a9f117d
commit 0154a594c9
2 changed files with 86 additions and 97 deletions

View file

@ -33,9 +33,9 @@ class FileByteStream : public ByteStream
public: public:
FileByteStream(FILE* pFile) : m_pFile(pFile) { DebugAssert(m_pFile != nullptr); } FileByteStream(FILE* pFile) : m_pFile(pFile) { DebugAssert(m_pFile != nullptr); }
virtual ~FileByteStream() { fclose(m_pFile); } virtual ~FileByteStream() override { fclose(m_pFile); }
virtual bool ReadByte(u8* pDestByte) override bool ReadByte(u8* pDestByte) override
{ {
if (m_errorState) if (m_errorState)
return false; return false;
@ -49,7 +49,7 @@ public:
return true; return true;
} }
virtual u32 Read(void* pDestination, u32 ByteCount) override u32 Read(void* pDestination, u32 ByteCount) override
{ {
if (m_errorState) if (m_errorState)
return 0; return 0;
@ -61,7 +61,7 @@ public:
return readCount; return readCount;
} }
virtual bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override
{ {
if (m_errorState) if (m_errorState)
return false; return false;
@ -80,7 +80,7 @@ public:
return true; return true;
} }
virtual bool WriteByte(u8 SourceByte) override bool WriteByte(u8 SourceByte) override
{ {
if (m_errorState) if (m_errorState)
return false; return false;
@ -94,7 +94,7 @@ public:
return true; return true;
} }
virtual u32 Write(const void* pSource, u32 ByteCount) override u32 Write(const void* pSource, u32 ByteCount) override
{ {
if (m_errorState) if (m_errorState)
return 0; return 0;
@ -106,7 +106,7 @@ public:
return writeCount; return writeCount;
} }
virtual bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override
{ {
if (m_errorState) if (m_errorState)
return false; return false;
@ -127,7 +127,7 @@ public:
#if defined(_WIN32) #if defined(_WIN32)
virtual bool SeekAbsolute(u64 Offset) override bool SeekAbsolute(u64 Offset) override
{ {
if (m_errorState) if (m_errorState)
return false; return false;
@ -141,7 +141,7 @@ public:
return true; return true;
} }
virtual bool SeekRelative(s64 Offset) override bool SeekRelative(s64 Offset) override
{ {
if (m_errorState) if (m_errorState)
return false; return false;
@ -155,7 +155,7 @@ public:
return true; return true;
} }
virtual bool SeekToEnd() override bool SeekToEnd() override
{ {
if (m_errorState) if (m_errorState)
return false; return false;
@ -169,9 +169,9 @@ public:
return true; return true;
} }
virtual u64 GetPosition() const override { return _ftelli64(m_pFile); } u64 GetPosition() const override { return _ftelli64(m_pFile); }
virtual u64 GetSize() const override u64 GetSize() const override
{ {
s64 OldPos = _ftelli64(m_pFile); s64 OldPos = _ftelli64(m_pFile);
_fseeki64(m_pFile, 0, SEEK_END); _fseeki64(m_pFile, 0, SEEK_END);
@ -182,7 +182,7 @@ public:
#else #else
virtual bool SeekAbsolute(u64 Offset) override bool SeekAbsolute(u64 Offset) override
{ {
if (m_errorState) if (m_errorState)
return false; return false;
@ -196,7 +196,7 @@ public:
return true; return true;
} }
virtual bool SeekRelative(s64 Offset) override bool SeekRelative(s64 Offset) override
{ {
if (m_errorState) if (m_errorState)
return false; return false;
@ -210,7 +210,7 @@ public:
return true; return true;
} }
virtual bool SeekToEnd() override bool SeekToEnd() override
{ {
if (m_errorState) if (m_errorState)
return false; return false;
@ -224,9 +224,9 @@ public:
return true; return true;
} }
virtual u64 GetPosition() const override { return static_cast<u64>(ftello(m_pFile)); } u64 GetPosition() const override { return static_cast<u64>(ftello(m_pFile)); }
virtual u64 GetSize() const override u64 GetSize() const override
{ {
off_t OldPos = ftello(m_pFile); off_t OldPos = ftello(m_pFile);
fseeko(m_pFile, 0, SEEK_END); fseeko(m_pFile, 0, SEEK_END);
@ -237,7 +237,7 @@ public:
#endif #endif
virtual bool Flush() override bool Flush() override
{ {
if (m_errorState) if (m_errorState)
return false; return false;
@ -259,7 +259,7 @@ protected:
FILE* m_pFile; FILE* m_pFile;
}; };
class AtomicUpdatedFileByteStream : public FileByteStream class AtomicUpdatedFileByteStream final : public FileByteStream
{ {
public: public:
AtomicUpdatedFileByteStream(FILE* pFile, std::string originalFileName, std::string temporaryFileName) AtomicUpdatedFileByteStream(FILE* pFile, std::string originalFileName, std::string temporaryFileName)
@ -268,7 +268,7 @@ public:
{ {
} }
virtual ~AtomicUpdatedFileByteStream() ~AtomicUpdatedFileByteStream() override
{ {
if (m_discarded) if (m_discarded)
{ {
@ -304,18 +304,7 @@ public:
// fclose called by FileByteStream destructor // fclose called by FileByteStream destructor
} }
virtual bool Flush() override bool Commit() override
{
if (fflush(m_pFile) != 0)
{
m_errorState = true;
return false;
}
return true;
}
virtual bool Commit() override
{ {
Assert(!m_discarded); Assert(!m_discarded);
if (m_committed) if (m_committed)
@ -364,7 +353,7 @@ public:
return (!m_discarded); return (!m_discarded);
} }
virtual bool Discard() override bool Discard() override
{ {
Assert(!m_committed); Assert(!m_committed);
m_discarded = true; m_discarded = true;

View file

@ -144,51 +144,51 @@ protected:
ByteStream& operator=(const ByteStream&) = delete; ByteStream& operator=(const ByteStream&) = delete;
}; };
class NullByteStream : public ByteStream class NullByteStream final : public ByteStream
{ {
public: public:
NullByteStream(); NullByteStream();
~NullByteStream(); ~NullByteStream() override;
virtual bool ReadByte(u8* pDestByte) override final; bool ReadByte(u8* pDestByte) override;
virtual u32 Read(void* pDestination, u32 ByteCount) override final; u32 Read(void* pDestination, u32 ByteCount) override;
virtual bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override final; bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override;
virtual bool WriteByte(u8 SourceByte) override final; bool WriteByte(u8 SourceByte) override;
virtual u32 Write(const void* pSource, u32 ByteCount) override final; u32 Write(const void* pSource, u32 ByteCount) override;
virtual bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override final; bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override;
virtual bool SeekAbsolute(u64 Offset) override final; bool SeekAbsolute(u64 Offset) override;
virtual bool SeekRelative(s64 Offset) override final; bool SeekRelative(s64 Offset) override;
virtual bool SeekToEnd() override final; bool SeekToEnd() override;
virtual u64 GetSize() const override final; u64 GetSize() const override;
virtual u64 GetPosition() const override final; u64 GetPosition() const override;
virtual bool Flush() override final; bool Flush() override;
virtual bool Commit() override final; bool Commit() override;
virtual bool Discard() override final; bool Discard() override;
}; };
class MemoryByteStream : public ByteStream class MemoryByteStream final : public ByteStream
{ {
public: public:
MemoryByteStream(void* pMemory, u32 MemSize); MemoryByteStream(void* pMemory, u32 MemSize);
virtual ~MemoryByteStream(); ~MemoryByteStream() override;
u8* GetMemoryPointer() const { return m_pMemory; } u8* GetMemoryPointer() const { return m_pMemory; }
u32 GetMemorySize() const { return m_iSize; } u32 GetMemorySize() const { return m_iSize; }
virtual bool ReadByte(u8* pDestByte) override; bool ReadByte(u8* pDestByte) override;
virtual u32 Read(void* pDestination, u32 ByteCount) override; u32 Read(void* pDestination, u32 ByteCount) override;
virtual bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override; bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override;
virtual bool WriteByte(u8 SourceByte) override; bool WriteByte(u8 SourceByte) override;
virtual u32 Write(const void* pSource, u32 ByteCount) override; u32 Write(const void* pSource, u32 ByteCount) override;
virtual bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override; bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override;
virtual bool SeekAbsolute(u64 Offset) override; bool SeekAbsolute(u64 Offset) override;
virtual bool SeekRelative(s64 Offset) override; bool SeekRelative(s64 Offset) override;
virtual bool SeekToEnd() override; bool SeekToEnd() override;
virtual u64 GetSize() const override; u64 GetSize() const override;
virtual u64 GetPosition() const override; u64 GetPosition() const override;
virtual bool Flush() override; bool Flush() override;
virtual bool Commit() override; bool Commit() override;
virtual bool Discard() override; bool Discard() override;
private: private:
u8* m_pMemory; u8* m_pMemory;
@ -196,29 +196,29 @@ private:
u32 m_iSize; u32 m_iSize;
}; };
class ReadOnlyMemoryByteStream : public ByteStream class ReadOnlyMemoryByteStream final : public ByteStream
{ {
public: public:
ReadOnlyMemoryByteStream(const void* pMemory, u32 MemSize); ReadOnlyMemoryByteStream(const void* pMemory, u32 MemSize);
virtual ~ReadOnlyMemoryByteStream(); ~ReadOnlyMemoryByteStream() override;
const u8* GetMemoryPointer() const { return m_pMemory; } const u8* GetMemoryPointer() const { return m_pMemory; }
u32 GetMemorySize() const { return m_iSize; } u32 GetMemorySize() const { return m_iSize; }
virtual bool ReadByte(u8* pDestByte) override; bool ReadByte(u8* pDestByte) override;
virtual u32 Read(void* pDestination, u32 ByteCount) override; u32 Read(void* pDestination, u32 ByteCount) override;
virtual bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override; bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override;
virtual bool WriteByte(u8 SourceByte) override; bool WriteByte(u8 SourceByte) override;
virtual u32 Write(const void* pSource, u32 ByteCount) override; u32 Write(const void* pSource, u32 ByteCount) override;
virtual bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override; bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override;
virtual bool SeekAbsolute(u64 Offset) override; bool SeekAbsolute(u64 Offset) override;
virtual bool SeekRelative(s64 Offset) override; bool SeekRelative(s64 Offset) override;
virtual bool SeekToEnd() override; bool SeekToEnd() override;
virtual u64 GetSize() const override; u64 GetSize() const override;
virtual u64 GetPosition() const override; u64 GetPosition() const override;
virtual bool Flush() override; bool Flush() override;
virtual bool Commit() override; bool Commit() override;
virtual bool Discard() override; bool Discard() override;
private: private:
const u8* m_pMemory; const u8* m_pMemory;
@ -226,11 +226,11 @@ private:
u32 m_iSize; u32 m_iSize;
}; };
class GrowableMemoryByteStream : public ByteStream class GrowableMemoryByteStream final : public ByteStream
{ {
public: public:
GrowableMemoryByteStream(void* pInitialMem, u32 InitialMemSize); GrowableMemoryByteStream(void* pInitialMem, u32 InitialMemSize);
virtual ~GrowableMemoryByteStream(); ~GrowableMemoryByteStream() override;
u8* GetMemoryPointer() const { return m_pMemory; } u8* GetMemoryPointer() const { return m_pMemory; }
u32 GetMemorySize() const { return m_iMemorySize; } u32 GetMemorySize() const { return m_iMemorySize; }
@ -240,20 +240,20 @@ public:
void EnsureSpace(u32 space); void EnsureSpace(u32 space);
void ShrinkToFit(); void ShrinkToFit();
virtual bool ReadByte(u8* pDestByte) override; bool ReadByte(u8* pDestByte) override;
virtual u32 Read(void* pDestination, u32 ByteCount) override; u32 Read(void* pDestination, u32 ByteCount) override;
virtual bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override; bool Read2(void* pDestination, u32 ByteCount, u32* pNumberOfBytesRead /* = nullptr */) override;
virtual bool WriteByte(u8 SourceByte) override; bool WriteByte(u8 SourceByte) override;
virtual u32 Write(const void* pSource, u32 ByteCount) override; u32 Write(const void* pSource, u32 ByteCount) override;
virtual bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override; bool Write2(const void* pSource, u32 ByteCount, u32* pNumberOfBytesWritten /* = nullptr */) override;
virtual bool SeekAbsolute(u64 Offset) override; bool SeekAbsolute(u64 Offset) override;
virtual bool SeekRelative(s64 Offset) override; bool SeekRelative(s64 Offset) override;
virtual bool SeekToEnd() override; bool SeekToEnd() override;
virtual u64 GetSize() const override; u64 GetSize() const override;
virtual u64 GetPosition() const override; u64 GetPosition() const override;
virtual bool Flush() override; bool Flush() override;
virtual bool Commit() override; bool Commit() override;
virtual bool Discard() override; bool Discard() override;
private: private:
void Grow(u32 MinimumGrowth); void Grow(u32 MinimumGrowth);