diff options
author | vng <viktor.govako@gmail.com> | 2015-12-04 20:31:13 +0300 |
---|---|---|
committer | Sergey Yershov <yershov@corp.mail.ru> | 2016-03-23 16:02:58 +0300 |
commit | 9434abdd15663e69e985cb21c6c93abdafff8a1a (patch) | |
tree | bcc995336f2dca915c030aa39f82f08a89438365 /coding/writer.hpp | |
parent | bb1ad911558044ecab8fffc0ac86e320af7e6cae (diff) |
Optimized Write function to avoid multiple vector reallocation.
Diffstat (limited to 'coding/writer.hpp')
-rw-r--r-- | coding/writer.hpp | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/coding/writer.hpp b/coding/writer.hpp index 40655aacd2..b68c9e3891 100644 --- a/coding/writer.hpp +++ b/coding/writer.hpp @@ -49,9 +49,21 @@ public: inline void Write(void const * p, size_t size) { - if (m_Pos + size > m_Data.size()) + intptr_t freeSize = m_Data.size() - m_Pos; + if (freeSize < 0) + { m_Data.resize(m_Pos + size); - memcpy(((uint8_t*)m_Data.data()) + m_Pos, p, size); + freeSize = size; + } + + memcpy(&m_Data[m_Pos], p, min(size, static_cast<size_t>(freeSize))); + + if (size > freeSize) + { + uint8_t const * it = reinterpret_cast<uint8_t const *>(p); + m_Data.insert(m_Data.end(), it + freeSize, it + size); + } + m_Pos += size; } |