# HG changeset patch # User Sebastien Jodogne # Date 1749650065 -7200 # Node ID ecd7fdc5f8d455c0708ef2f610cadf61f34b2fcf # Parent 52f87859fec23d18fe2b5ba7d0df41e1fce37cfa optimization of ChunkedBuffer diff -r 52f87859fec2 -r ecd7fdc5f8d4 OrthancFramework/Sources/ChunkedBuffer.cpp --- 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; }