changeset 97:22494cc28c9f

* Reduce number of allocations and deallocations by keeping a spare buffer around (we were generally deallocating and then immediately allocating again, so it's much better not to have to bother as very large allocations can tie up the system)
author Chris Cannam
date Thu, 04 May 2006 20:17:28 +0000
parents 1aebdc68ec6d
children 604bd4ee3ed4
files base/MatrixFile.cpp base/MatrixFile.h
diffstat 2 files changed, 16 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/base/MatrixFile.cpp	Thu May 04 16:03:02 2006 +0000
+++ b/base/MatrixFile.cpp	Thu May 04 20:17:28 2006 +0000
@@ -38,9 +38,10 @@
     m_width(0),
     m_height(0),
     m_headerSize(2 * sizeof(size_t)),
-    m_defaultCacheWidth(256),
+    m_defaultCacheWidth(512),
     m_prevX(0),
-    m_requestToken(-1)
+    m_requestToken(-1),
+    m_spareData(0)
 {
     m_cache.data = 0;
 
@@ -90,10 +91,6 @@
     }
 
     m_fileName = fileName;
-    
-    //!!! why isn't this signal being delivered?
-    connect(&m_readThread, SIGNAL(cancelled(int)), 
-            this, SLOT(requestCancelled(int)));
 
     m_readThread.start();
 
@@ -118,8 +115,9 @@
     m_readThread.finish();
     m_readThread.wait();
 
-    if (requestData) delete[] requestData;
-    if (m_cache.data) delete[] m_cache.data;
+    if (requestData) free(requestData);
+    if (m_cache.data) free(m_cache.data);
+    if (m_spareData) free(m_spareData);
 
     if (m_fd >= 0) {
         if (::close(m_fd) < 0) {
@@ -408,7 +406,10 @@
                 
                 std::cerr << "actual: " << m_cache.x << ", " << m_cache.width << std::endl;
 
-                if (m_cache.data) delete[] m_cache.data;
+                if (m_cache.data) {
+                    if (m_spareData) free(m_spareData);
+                    m_spareData = (char *)m_cache.data;
+                }
                 m_cache.data = (float *)request.data;
 
                 m_readThread.done(m_requestToken);
@@ -427,7 +428,8 @@
             usleep(10000);
         }
 
-        delete[] ((float *)request.data);
+        if (m_spareData) free(m_spareData);
+        m_spareData = request.data;
         m_readThread.done(m_requestToken);
 
         m_requestToken = -1;
@@ -437,8 +439,9 @@
     request.mutex = &m_fdMutex;
     request.start = m_headerSize + rx * m_height * sizeof(float);
     request.size = rw * m_height * sizeof(float);
-    request.data = (char *)(new float[rw * m_height]);
+    request.data = (char *)realloc(m_spareData, rw * m_height * sizeof(float));
     MUNLOCK(request.data, rw * m_height * sizeof(float));
+    m_spareData = 0;
 
     m_requestingX = rx;
     m_requestingWidth = rw;
@@ -450,18 +453,6 @@
     m_requestToken = token;
 }
 
-void
-MatrixFile::requestCancelled(int token)
-{
-    std::cerr << "MatrixFile::requestCancelled(" << token << ")" << std::endl;
-
-    FileReadThread::Request request;
-    if (m_readThread.getRequest(token, request)) {
-        delete[] ((float *)request.data);
-        m_readThread.done(token);
-    }
-}
-
 bool
 MatrixFile::seekTo(size_t x, size_t y)
 {
--- a/base/MatrixFile.h	Thu May 04 16:03:02 2006 +0000
+++ b/base/MatrixFile.h	Thu May 04 20:17:28 2006 +0000
@@ -45,9 +45,6 @@
     void setValueAt(size_t x, size_t y, float value);
     void setColumnAt(size_t x, float *values);
 
-protected slots:
-    void requestCancelled(int token);
-
 protected:
     int     m_fd;
     Mode    m_mode;
@@ -75,8 +72,10 @@
 
     FileReadThread m_readThread;
     int m_requestToken;
+
     size_t m_requestingX;
     size_t m_requestingWidth;
+    char *m_spareData;
 
     static std::map<QString, int> m_refcount;
     static QMutex m_refcountMutex;