changeset 1371:fad8f533ca13

Reduce amount of copying in FFTModel
author Chris Cannam
date Thu, 26 Jan 2017 14:11:20 +0000
parents 54fabf5aceb8
children 54366398e636
files data/model/FFTModel.cpp data/model/FFTModel.h
diffstat 2 files changed, 14 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/data/model/FFTModel.cpp	Fri Jan 20 18:05:41 2017 +0000
+++ b/data/model/FFTModel.cpp	Thu Jan 26 14:11:20 2017 +0000
@@ -44,8 +44,13 @@
     m_fftSize(fftSize),
     m_windower(windowType, windowSize),
     m_fft(fftSize),
+    m_cacheWriteIndex(0),
     m_cacheSize(3)
 {
+    while (m_cached.size() < m_cacheSize) {
+        m_cached.push_back({ -1, cvec(m_fftSize / 2 + 1) });
+    }
+    
     if (m_windowSize > m_fftSize) {
         cerr << "ERROR: FFTModel::FFTModel: window size (" << m_windowSize
              << ") must be at least FFT size (" << m_fftSize << ")" << endl;
@@ -296,7 +301,7 @@
     return data;
 }
 
-FFTModel::cvec
+const FFTModel::cvec &
 FFTModel::getFFTColumn(int n) const
 {
     // The small cache (i.e. the m_cached deque) is for cases where
@@ -319,16 +324,14 @@
     m_windower.cut(samples.data());
     breakfastquay::v_fftshift(samples.data(), m_fftSize);
 
-    cvec col(m_fftSize/2 + 1);
+    cvec &col = m_cached[m_cacheWriteIndex].col;
     
     m_fft.forwardInterleaved(samples.data(),
                              reinterpret_cast<float *>(col.data()));
 
-    SavedColumn sc { n, col };
-    if (m_cached.size() >= m_cacheSize) {
-        m_cached.pop_front();
-    }
-    m_cached.push_back(sc);
+    m_cached[m_cacheWriteIndex].n = n;
+
+    m_cacheWriteIndex = (m_cacheWriteIndex + 1) % m_cacheSize;
 
     return col;
 }
--- a/data/model/FFTModel.h	Fri Jan 20 18:05:41 2017 +0000
+++ b/data/model/FFTModel.h	Thu Jan 26 14:11:20 2017 +0000
@@ -27,7 +27,6 @@
 #include <set>
 #include <vector>
 #include <complex>
-#include <deque>
 
 /**
  * An implementation of DenseThreeDimensionalModel that makes FFT data
@@ -172,7 +171,7 @@
     typedef std::vector<std::complex<float>,
                         breakfastquay::StlAllocator<std::complex<float>>> cvec;
     
-    cvec getFFTColumn(int column) const;
+    const cvec &getFFTColumn(int column) const; // returns ref for immediate use only
     fvec getSourceSamples(int column) const;
     fvec getSourceData(std::pair<sv_frame_t, sv_frame_t>) const;
     fvec getSourceDataUncached(std::pair<sv_frame_t, sv_frame_t>) const;
@@ -182,12 +181,13 @@
         fvec data;
     };
     mutable SavedSourceData m_savedData;
-    
+
     struct SavedColumn {
         int n;
         cvec col;
     };
-    mutable std::deque<SavedColumn> m_cached;
+    mutable std::vector<SavedColumn> m_cached;
+    mutable size_t m_cacheWriteIndex;
     size_t m_cacheSize;
 };