Changeset - 29bd9e9a44b0
[Not reviewed]
default
0 2 0
Hasan Yavuz ÖZDERYA - 8 years ago 2017-11-07 06:11:21
hy@ozderya.net
cache bounding rectangle instead of re-calculating at every reading

improves performance significantly for big buffers
2 files changed with 36 insertions and 23 deletions:
0 comments (0 inline, 0 general)
src/framebuffer.cpp
Show inline comments
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 
  Copyright © 2017 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
@@ -25,7 +25,8 @@ FrameBuffer::FrameBuffer(size_t size)
 
    data = new double[_size]();
 
    headIndex = 0;
 

	
 
    _boundingRect.setCoords(0, 0, size, 0);
 
    brInvalid = false;
 
    _brCache.setCoords(0, 0, size, 0);
 
}
 

	
 
FrameBuffer::~FrameBuffer()
 
@@ -63,8 +64,8 @@ void FrameBuffer::resize(size_t size)
 
    headIndex = 0;
 
    _size = size;
 

	
 
    // update the bounding rectangle
 
    _boundingRect.setRight(_size);
 
    // invalidate bounding rectangle
 
    brInvalid = true;
 
}
 

	
 
void FrameBuffer::addSamples(double* samples, size_t size)
 
@@ -113,7 +114,30 @@ void FrameBuffer::addSamples(double* sam
 
        headIndex = 0;
 
    }
 

	
 
    // update bounding rectangle
 
    // invalidate cache
 
    brInvalid = true;
 
}
 

	
 
void FrameBuffer::clear()
 
{
 
    for (size_t i=0; i < _size; i++) data[i] = 0.;
 

	
 
    _brCache.setCoords(0, 0, _size, 0);
 
}
 

	
 
size_t FrameBuffer::size() const
 
{
 
    return _size;
 
}
 

	
 
QRectF FrameBuffer::boundingRect() const
 
{
 
    if (brInvalid) updateBoundingRect();
 
    return _brCache;
 
}
 

	
 
void FrameBuffer::updateBoundingRect() const
 
{
 
    double minValue = data[0];
 
    double maxValue = data[0];
 
    for (size_t i = 0; i < _size; i++)
 
@@ -127,23 +151,10 @@ void FrameBuffer::addSamples(double* sam
 
            minValue = data[i];
 
        }
 
    }
 
    _boundingRect.setTop(minValue);
 
    _boundingRect.setBottom(maxValue);
 
}
 

	
 
void FrameBuffer::clear()
 
{
 
    for (size_t i=0; i < _size; i++) data[i] = 0.;
 
}
 
    _brCache.setTop(minValue);
 
    _brCache.setBottom(maxValue);
 

	
 
size_t FrameBuffer::size() const
 
{
 
    return _size;
 
}
 

	
 
QRectF FrameBuffer::boundingRect() const
 
{
 
    return _boundingRect;
 
    brInvalid = false;
 
}
 

	
 
double FrameBuffer::sample(size_t i) const
src/framebuffer.h
Show inline comments
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 
  Copyright © 2017 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
@@ -43,7 +43,9 @@ private:
 
    double* data;
 
    size_t headIndex; // indicates the actual `0` index of the ring buffer
 

	
 
    QRectF _boundingRect;
 
    mutable bool brInvalid; ///< Indicates that bounding rectangle needs to be re-calculated
 
    mutable QRectF _brCache; ///< Cache for boundingRect()
 
    void updateBoundingRect() const; ///< Updates bounding rectangle cache
 
};
 

	
 
#endif // FRAMEBUFFER_H
0 comments (0 inline, 0 general)