Mercurial > hg > orthanc
changeset 6165:ecd7fdc5f8d4
optimization of ChunkedBuffer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 11 Jun 2025 15:54:25 +0200 |
parents | 52f87859fec2 |
children | dbe67b2c2d9c 20e44205c260 dfcac2e0d784 |
files | OrthancFramework/Sources/ChunkedBuffer.cpp |
diffstat | 1 files changed, 47 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/OrthancFramework/Sources/ChunkedBuffer.cpp Wed Jun 11 15:23:12 2025 +0200 +++ b/OrthancFramework/Sources/ChunkedBuffer.cpp Wed Jun 11 15:54:25 2025 +0200 @@ -184,31 +184,58 @@ { FlushPendingBuffer(); - try + if (chunks_.empty()) + { + if (numBytes_ != 0) + { + throw OrthancException(ErrorCode_InternalError); + } + + result.clear(); + } + else if (chunks_.size() == 1) { - result.resize(numBytes_); + // Avoid reallocating a buffer if there is a single chunk + assert(chunks_.front() != NULL); + if (chunks_.front()->size() != numBytes_) + { + throw OrthancException(ErrorCode_InternalError); + } + else + { + chunks_.front()->swap(result); + delete chunks_.front(); + } } - catch (...) + else { - throw OrthancException(ErrorCode_NotEnoughMemory); + try + { + result.resize(numBytes_); + } + catch (...) + { + throw OrthancException(ErrorCode_NotEnoughMemory); + } + + size_t pos = 0; + for (Chunks::iterator it = chunks_.begin(); + it != chunks_.end(); ++it) + { + assert(*it != NULL); + + size_t s = (*it)->size(); + if (s != 0) + { + memcpy(&result[pos], (*it)->c_str(), s); + pos += s; + } + + delete *it; + } } - size_t pos = 0; - for (Chunks::iterator it = chunks_.begin(); - it != chunks_.end(); ++it) - { - assert(*it != NULL); - - size_t s = (*it)->size(); - if (s != 0) - { - memcpy(&result[pos], (*it)->c_str(), s); - pos += s; - } - - delete *it; - } - + // Reset the data structure chunks_.clear(); numBytes_ = 0; }