[Liblas-commits] hg-main-tree: refactor cache system for const-correctness

liblas-commits at liblas.org liblas-commits at liblas.org
Wed Mar 23 13:43:06 EDT 2011


details:   http://hg.libpc.orghg-main-tree/rev/a8049357f648
changeset: 407:a8049357f648
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Wed Mar 23 10:30:31 2011 -0700
description:
refactor cache system for const-correctness

diffstat:

 include/libpc/filters/CacheFilter.hpp |  24 +++++++++++++++++-------
 src/filters/CacheFilter.cpp           |  34 ++++++++++++++++++++++++++++++++++
 src/filters/CacheFilterIterator.cpp   |  28 +++++++++++-----------------
 3 files changed, 62 insertions(+), 24 deletions(-)

diffs (176 lines):

diff -r 0f1af82d2829 -r a8049357f648 include/libpc/filters/CacheFilter.hpp
--- a/include/libpc/filters/CacheFilter.hpp	Wed Mar 23 09:59:16 2011 -0700
+++ b/include/libpc/filters/CacheFilter.hpp	Wed Mar 23 10:30:31 2011 -0700
@@ -44,8 +44,6 @@
     
 namespace filters {
 
-class CacheFilterIterator;
-
 // This is just a very simple MRU filter -- future versions will be smarter.
 // This cache has the following constraints:
 //   - only one block is cached
@@ -53,14 +51,19 @@
 // If more than one point is read, the cache is skipped.
 class LIBPC_DLL CacheFilter : public Filter
 {
-    friend class CacheFilterIterator;
-
 public:
     CacheFilter(const Stage& prevStage, boost::uint32_t numBlocks, boost::uint32_t blockSize);
     ~CacheFilter();
 
     const std::string& getName() const;
 
+    boost::uint32_t getCacheBlockSize() const;
+
+    // this is const only because the m_cache itself is mutable
+    void addToCache(boost::uint64_t pointIndex, const PointBuffer& data) const;
+
+    const PointBuffer* lookupInCache(boost::uint64_t pointIndex) const;
+
     // clear cache (but leave cache params unchanged)
     void resetCache();
 
@@ -73,6 +76,8 @@
     // num points this filter read from the previous stage
     boost::uint64_t getNumPointsRead() const;
 
+    void updateStats(boost::uint64_t numRead, boost::uint64_t numRequested) const;
+
     void getCacheStats(boost::uint64_t& numCacheLookupMisses,
                        boost::uint64_t& numCacheLookupHits,
                        boost::uint64_t& numCacheInsertMisses,
@@ -81,10 +86,15 @@
     Iterator* createIterator() const;
 
 private:
-    boost::uint64_t m_numPointsRequested;
-    boost::uint64_t m_numPointsRead;
+    // these are mutable to allow const-ness for updating stats
+    // BUG: need to make thread-safe
+    mutable boost::uint64_t m_numPointsRequested;
+    mutable boost::uint64_t m_numPointsRead;
 
-    PointBufferCache* m_cache;
+    // these is mutable to allow const-ness for updating stats
+    // BUG: need to make thread-safe
+    mutable PointBufferCache* m_cache;
+
     boost::uint32_t m_maxCacheBlocks;
     boost::uint32_t m_cacheBlockSize;
 
diff -r 0f1af82d2829 -r a8049357f648 src/filters/CacheFilter.cpp
--- a/src/filters/CacheFilter.cpp	Wed Mar 23 09:59:16 2011 -0700
+++ b/src/filters/CacheFilter.cpp	Wed Mar 23 10:30:31 2011 -0700
@@ -68,6 +68,33 @@
 }
 
 
+boost::uint32_t CacheFilter::getCacheBlockSize() const
+{
+    return m_cacheBlockSize;
+}
+
+
+void CacheFilter::addToCache(boost::uint64_t pointIndex, const PointBuffer& data) const
+{
+    PointBuffer* block = new PointBuffer(data.getSchemaLayout(), m_cacheBlockSize);
+    block->copyPointsFast(0, 0, data, m_cacheBlockSize);
+    
+    m_cache->insert(pointIndex, block);
+
+    return;
+}
+
+
+const PointBuffer* CacheFilter::lookupInCache(boost::uint64_t pointIndex) const
+{
+    const boost::uint64_t blockNum = pointIndex / m_cacheBlockSize;
+
+    const PointBuffer* block = m_cache->lookup(blockNum);
+
+    return block;
+}
+
+
 void CacheFilter::getCacheStats(boost::uint64_t& numCacheLookupMisses,
                                 boost::uint64_t& numCacheLookupHits,
                                 boost::uint64_t& numCacheInsertMisses,
@@ -107,6 +134,13 @@
 }
 
 
+void CacheFilter::updateStats(boost::uint64_t numRead, boost::uint64_t numRequested) const
+{
+    m_numPointsRead += numRead;
+    m_numPointsRequested += numRequested;
+}
+
+
 libpc::Iterator* CacheFilter::createIterator() const
 {
     return new CacheFilterIterator(*this);
diff -r 0f1af82d2829 -r a8049357f648 src/filters/CacheFilterIterator.cpp
--- a/src/filters/CacheFilterIterator.cpp	Wed Mar 23 09:59:16 2011 -0700
+++ b/src/filters/CacheFilterIterator.cpp	Wed Mar 23 10:30:31 2011 -0700
@@ -64,7 +64,7 @@
 
 boost::uint32_t CacheFilterIterator::readImpl(PointBuffer& data)
 {
-    CacheFilter& filter = const_cast<CacheFilter&>(m_filter);       // BUG BUG BUG
+    const boost::uint32_t cacheBlockSize = m_filter.getCacheBlockSize();
 
     const boost::uint64_t currentPointIndex = getIndex();
 
@@ -78,40 +78,34 @@
         // if they asked for a full block and we got a full block,
         // and the block we got is properly aligned and not already cached,
         // then let's cache it!
-        if (data.getCapacity() == filter.m_cacheBlockSize && numRead == filter.m_cacheBlockSize && 
-            (currentPointIndex % filter.m_cacheBlockSize == 0) &&
-            filter.m_cache->lookup(currentPointIndex) == NULL)
+        const bool isCacheable = (data.getCapacity() == cacheBlockSize) && 
+                                 (numRead == cacheBlockSize) && 
+                                 (currentPointIndex % cacheBlockSize == 0);
+        if (isCacheable && (m_filter.lookupInCache(currentPointIndex) == NULL))
         {
-            PointBuffer* block = new PointBuffer(data.getSchemaLayout(), filter.m_cacheBlockSize);
-            block->copyPointsFast(0, 0, data, filter.m_cacheBlockSize);
-            filter.m_cache->insert(currentPointIndex, block);
+            m_filter.addToCache(currentPointIndex, data);
         }
 
-        filter.m_numPointsRead += numRead;
-        filter.m_numPointsRequested += data.getCapacity();
+        m_filter.updateStats(numRead, data.getCapacity());
 
         return numRead;
     }
 
     // they asked for just one point -- first, check Mister Cache
-    const boost::uint64_t blockNum = currentPointIndex / filter.m_cacheBlockSize;
-    PointBuffer* block = filter.m_cache->lookup(blockNum);
+    const PointBuffer* block = m_filter.lookupInCache(currentPointIndex);
     if (block != NULL)
     {
         // A hit! A palpable hit!
-        data.copyPointFast(0,  currentPointIndex % filter.m_cacheBlockSize, *block);
+        data.copyPointFast(0,  currentPointIndex % cacheBlockSize, *block);
         
-        filter.m_numPointsRead += 0;
-        filter.m_numPointsRequested += 1;
+        m_filter.updateStats(0, 1);
 
-        getPrevIterator().skip(1);
         return 1;
     }
 
     // Not in the cache, so do a normal read :-(
     const boost::uint32_t numRead = getPrevIterator().read(data);
-    filter.m_numPointsRead += numRead;
-    filter.m_numPointsRequested += numRead;
+    m_filter.updateStats(numRead, numRead);
 
     return numRead;
 }


More information about the Liblas-commits mailing list