From 63ca5d9d980d4d77331251b5c453310d410b0cd2 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sat, 3 Jun 2023 18:04:38 +0200 Subject: [PATCH] Make small bitbuf methods inline Make inline to boost performance. --- r5dev/public/tier1/bitbuf.h | 38 +++++----- r5dev/tier1/bitbuf.cpp | 135 +----------------------------------- 2 files changed, 20 insertions(+), 153 deletions(-) diff --git a/r5dev/public/tier1/bitbuf.h b/r5dev/public/tier1/bitbuf.h index 9aec7195..f43745c5 100644 --- a/r5dev/public/tier1/bitbuf.h +++ b/r5dev/public/tier1/bitbuf.h @@ -108,10 +108,10 @@ class CBitBuffer { public: CBitBuffer(void); - void SetDebugName(const char* pName); - const char* GetDebugName() const; - bool IsOverflowed() const; - void SetOverflowFlag(); + inline void SetDebugName(const char* pName) { m_pDebugName = pName; } + inline const char* GetDebugName() const { return m_pDebugName; } + inline bool IsOverflowed() const { return m_bOverflow; } + inline void SetOverflowFlag() { m_bOverflow = true; } //////////////////////////////////// const char* m_pDebugName; @@ -138,8 +138,8 @@ public: int ReadSBitLong(int numbits); uint32 ReadUBitLong(int numbits); - int ReadByte(); - int ReadChar(); + inline int ReadByte() { return ReadSBitLong(sizeof(unsigned char) << 3); } + inline int ReadChar() { return ReadSBitLong(sizeof(char) << 3); } bool ReadString(char* pStr, int bufLen, bool bLine = false, int* pOutNumChars = nullptr); //////////////////////////////////// @@ -167,8 +167,8 @@ public: bf_write(const char* pDebugName, void* pData, int nBytes, int nMaxBits = -1); // Restart buffer writing. - void Reset(); - void SeekToBit(int bitPos); + inline void Reset() { m_iCurBit = 0; m_bOverflow = false; } + inline void SeekToBit(int bitPos) { m_iCurBit = bitPos; } void StartWriting(void* pData, int nBytes, int iStartBit = 0, int nMaxBits = -1); @@ -187,24 +187,24 @@ public: // Write a list of bits in. bool WriteBits(const void* pIn, int nBits); - - bool WriteBytes(const void* pIn, int nBytes); + inline bool WriteBytes(const void* pIn, int nBytes) { return WriteBits(pIn, nBytes << 3); } // How many bytes are filled in? - int GetNumBytesWritten() const; - int GetNumBitsWritten() const; - int GetMaxNumBits() const; - int GetNumBitsLeft() const; - int GetNumBytesLeft() const; - unsigned char* GetData() const; + inline int GetNumBytesWritten() const { return BitByte(this->m_iCurBit); } + inline int GetNumBitsWritten() const { return this->m_iCurBit; } + inline int GetMaxNumBits() const { return this->m_nDataBits; } + inline int GetNumBitsLeft() const { return this->m_nDataBits - m_iCurBit; } + inline int GetNumBytesLeft() const { return this->GetNumBitsLeft() >> 3; } + inline unsigned char* GetData() const { return this->m_pData; } - const char* GetDebugName() const; - void SetDebugName(const char* pDebugName); + inline const char* GetDebugName() const { return this->m_pDebugName; } + inline void SetDebugName(const char* pDebugName) { m_pDebugName = pDebugName; } // Has the buffer overflowed? bool CheckForOverflow(int nBits); - bool IsOverflowed() const; void SetOverflowFlag(); + + inline bool IsOverflowed() const { return this->m_bOverflow; } private: // The current buffer. unsigned char* m_pData; diff --git a/r5dev/tier1/bitbuf.cpp b/r5dev/tier1/bitbuf.cpp index b3cf7692..dfd39b47 100644 --- a/r5dev/tier1/bitbuf.cpp +++ b/r5dev/tier1/bitbuf.cpp @@ -72,26 +72,6 @@ CBitBuffer::CBitBuffer(void) m_nDataBytes = 0; } -void CBitBuffer::SetDebugName(const char* pName) -{ - m_pDebugName = pName; -} - -const char* CBitBuffer::GetDebugName() const -{ - return m_pDebugName; -} - -bool CBitBuffer::IsOverflowed() const -{ - return m_bOverflow; -} - -void CBitBuffer::SetOverflowFlag() -{ - m_bOverflow = true; -} - // ---------------------------------------------------------------------------------------- // // bf_read // ---------------------------------------------------------------------------------------- // @@ -270,22 +250,6 @@ int CBitRead::ReadSBitLong(int numbits) return (nRet << (32 - numbits)) >> (32 - numbits); } -//----------------------------------------------------------------------------- -// Purpose: reads byte from the buffer -//----------------------------------------------------------------------------- -int CBitRead::ReadByte() -{ - return ReadSBitLong(sizeof(unsigned char) << 3); -} - -//----------------------------------------------------------------------------- -// Purpose: reads character from the buffer -//----------------------------------------------------------------------------- -int CBitRead::ReadChar() -{ - return ReadSBitLong(sizeof(char) << 3); -} - //----------------------------------------------------------------------------- // Purpose: reads a string from the buffer //----------------------------------------------------------------------------- @@ -351,23 +315,6 @@ bf_write::bf_write(void* pData, int nBytes, int nBits) StartWriting(pData, nBytes, 0, nBits); } -//----------------------------------------------------------------------------- -// Purpose: resets buffer writing -//----------------------------------------------------------------------------- -void bf_write::Reset() -{ - m_iCurBit = 0; - m_bOverflow = false; -} - -//----------------------------------------------------------------------------- -// Purpose: seeks to a specific bit -//----------------------------------------------------------------------------- -void bf_write::SeekToBit(int bitPos) -{ - m_iCurBit = bitPos; -} - //----------------------------------------------------------------------------- // Purpose: //----------------------------------------------------------------------------- @@ -650,86 +597,6 @@ bool bf_write::WriteBits(const void* pInData, int nBits) return !IsOverflowed(); } -//----------------------------------------------------------------------------- -// Purpose: writes a list of bytes to the buffer -//----------------------------------------------------------------------------- -bool bf_write::WriteBytes(const void* pBuf, int nBytes) -{ - return WriteBits(pBuf, nBytes << 3); -} - -//----------------------------------------------------------------------------- -// Purpose: writes a bit into the buffer -//----------------------------------------------------------------------------- -bool bf_write::IsOverflowed() const -{ - return this->m_bOverflow; -} - -//----------------------------------------------------------------------------- -// Purpose: returns the number of bytes written to the buffer -//----------------------------------------------------------------------------- -int bf_write::GetNumBytesWritten() const -{ - return BitByte(this->m_iCurBit); -} - -//----------------------------------------------------------------------------- -// Purpose: returns the number of bits written to the buffer -//----------------------------------------------------------------------------- -int bf_write::GetNumBitsWritten() const -{ - return this->m_iCurBit; -} - -//----------------------------------------------------------------------------- -// Purpose: returns the number of bits in the buffer -//----------------------------------------------------------------------------- -int bf_write::GetMaxNumBits() const -{ - return this->m_nDataBits; -} - -//----------------------------------------------------------------------------- -// Purpose: returns the number of bits left in the buffer -//----------------------------------------------------------------------------- -int bf_write::GetNumBitsLeft() const -{ - return this->m_nDataBits - m_iCurBit; -} - -//----------------------------------------------------------------------------- -// Purpose: returns the number of bytes left in the buffer -//----------------------------------------------------------------------------- -int bf_write::GetNumBytesLeft() const -{ - return this->GetNumBitsLeft() >> 3; -} - -//----------------------------------------------------------------------------- -// Purpose: returns the data pointer -//----------------------------------------------------------------------------- -unsigned char* bf_write::GetData() const -{ - return this->m_pData; -} - -//----------------------------------------------------------------------------- -// Purpose: returns the debug name -//----------------------------------------------------------------------------- -const char* bf_write::GetDebugName() const -{ - return this->m_pDebugName; -} - -//----------------------------------------------------------------------------- -// Purpose: sets the debug name -//----------------------------------------------------------------------------- -void bf_write::SetDebugName(const char* pDebugName) -{ - m_pDebugName = pDebugName; -} - //----------------------------------------------------------------------------- // Purpose: checks if we have enough space for the requested number of bits //----------------------------------------------------------------------------- @@ -740,7 +607,7 @@ bool bf_write::CheckForOverflow(int nBits) this->SetOverflowFlag(); } - return this->m_bOverflow; + return IsOverflowed(); } //-----------------------------------------------------------------------------