From 7b0b5e3d7f31bf8d3eeba66241026fc65eede5e2 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 6 Apr 2020 00:57:20 +0800 Subject: [PATCH] Avoid inefficient behavior Since the class needs to be copy-constructible, there may be many copies of an instance. So instead of writing to the device on every destructor call, only flush buffer on the last destructor call. --- src/base/utils/io.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/base/utils/io.cpp b/src/base/utils/io.cpp index 499b55a1e..08b0f73c7 100644 --- a/src/base/utils/io.cpp +++ b/src/base/utils/io.cpp @@ -41,9 +41,11 @@ Utils::IO::FileDeviceOutputIterator::FileDeviceOutputIterator(QFileDevice &devic Utils::IO::FileDeviceOutputIterator::~FileDeviceOutputIterator() { - if (m_device->error() == QFileDevice::NoError) - m_device->write(*m_buffer); - m_buffer->clear(); + if (m_buffer.use_count() == 1) { + if (m_device->error() == QFileDevice::NoError) + m_device->write(*m_buffer); + m_buffer->clear(); + } } Utils::IO::FileDeviceOutputIterator &Utils::IO::FileDeviceOutputIterator::operator=(const char c)