[multimedia/kwave] libkwave: fixed performance bottleneck in sample storage subsystem

Thomas Eschenbacher <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 3b5224c05490283cf1bf06c66f717efd1fdd86b5 by Thomas Eschenbacher.
Committed on 24/07/2026 at 19:14.
Pushed by eschenbacher into branch 'master'.

fixed performance bottleneck in sample storage subsystem

M  +23   -15   libkwave/SampleArray.cpp
M  +1    -1    libkwave/Stripe.cpp

https://invent.kde.org/multimedia/kwave/-/commit/3b5224c05490283cf1bf06c66f717efd1fdd86b5

diff --git a/libkwave/SampleArray.cpp b/libkwave/SampleArray.cpp
index 31e68751..1ac1bb2b 100644
--- a/libkwave/SampleArray.cpp
+++ b/libkwave/SampleArray.cpp
@@ -83,16 +83,21 @@ const sample_t & Kwave::SampleArray::operator [] (unsigned int index) const
 //***************************************************************************
 bool Kwave::SampleArray::resize(unsigned int size)
 {
+    const unsigned int current_size = this->size();
     if (!m_storage) return false;
-    if (size == m_storage->m_size) return true;
+    if (size == current_size) return true;
 
-    m_storage->resize(size);
-    if (size && (m_storage->m_size > size)) {
-        qWarning("Kwave::SampleArray::resize(): shrinking from %u to %u "
-                 "failed, keeping old memory", m_storage->m_size, size);
-        return true;
+    if (size == 0) {
+        m_storage = new(std::nothrow) SampleStorage;
+    } else {
+        m_storage->resize(size);
+        if (size && (this->size() > size)) {
+            qWarning("Kwave::SampleArray::resize(): shrinking from %u to %u "
+                    "failed, keeping old memory", current_size, size);
+            return true;
+        }
     }
-    return (m_storage->m_size == size);
+    return (this->size() == size);
 }
 
 //***************************************************************************
@@ -103,20 +108,22 @@ unsigned int Kwave::SampleArray::size() const
 
 //***************************************************************************
 Kwave::SampleArray::SampleStorage::SampleStorage()
-    :QSharedData()
+    :QSharedData(),
+     m_size(0),
+     m_data(nullptr)
 {
-    m_size     = 0;
-    m_data     = nullptr;
 }
 
 //***************************************************************************
 Kwave::SampleArray::SampleStorage::SampleStorage(const SampleStorage &other)
-    :QSharedData(other)
+    :QSharedData(other),
+     m_size(0),
+     m_data(nullptr)
 {
-    m_size     = 0;
-    m_data     = nullptr;
-
     if (other.m_size) {
+        // qDebug("SampleStorage - DEEP COPY");
+        // print_backtrace();
+
         m_data = static_cast<sample_t *>(
             ::malloc(other.m_size * sizeof(sample_t))
         );
@@ -130,7 +137,8 @@ Kwave::SampleArray::SampleStorage::SampleStorage(const SampleStorage &other)
 //***************************************************************************
 Kwave::SampleArray::SampleStorage::~SampleStorage()
 {
-    if (m_data) ::free(m_data);
+    ::free(m_data);
+    m_data = nullptr;
 }
 
 //***************************************************************************
diff --git a/libkwave/Stripe.cpp b/libkwave/Stripe.cpp
index 5e1d9c8f..807a0ec3 100644
--- a/libkwave/Stripe.cpp
+++ b/libkwave/Stripe.cpp
@@ -41,7 +41,7 @@ Kwave::Stripe::Stripe(Stripe &&other)
     :m_lock(), m_start(other.m_start), m_data(other.m_data)
 {
     other.m_start = 0;
-    other.m_data.resize(0);
+    other.resize(0);
 }
 
 //***************************************************************************
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.