Skip to content

Commit

Permalink
Merge pull request #21 from hobu/issue-16
Browse files Browse the repository at this point in the history
Limit memory consumption by limiting buffer creation
  • Loading branch information
abellgithub authored Nov 18, 2020
2 parents 25a304d + ca36bb1 commit 0bcecc6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
14 changes: 12 additions & 2 deletions epf/BufferCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,36 @@ namespace epf
// If we have a buffer in the cache, return it. Otherwise create a new one and return that.
DataVecPtr BufferCache::fetch()
{
std::lock_guard<std::mutex> lock(m_mutex);
std::unique_lock<std::mutex> lock(m_mutex);

m_cv.wait(lock, [this](){ return m_buffers.size() || m_count < MaxBuffers; });
if (m_buffers.size())
{
DataVecPtr buf(std::move(m_buffers.back()));
m_buffers.pop_back();
return buf;
}

// m_count tracks the number of created buffers. We only create MaxBuffers buffers.
// If we've created that many, we wait until one is available.
m_count++;
return DataVecPtr(new DataVec(BufSize));
}

// Put a buffer back in the cache.
void BufferCache::replace(DataVecPtr&& buf)
{
std::lock_guard<std::mutex> lock(m_mutex);
std::unique_lock<std::mutex> lock(m_mutex);

//ABELL - Fix this.
buf->resize(BufSize);
m_buffers.push_back(std::move(buf));

if (m_count == MaxBuffers)
{
lock.unlock();
m_cv.notify_one();
}
}

} // namespace epf
Expand Down
12 changes: 10 additions & 2 deletions epf/BufferCache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#pragma once

#include <deque>
#include <mutex>
#include <condition_variable>

#include "EpfTypes.hpp"

Expand All @@ -26,11 +28,17 @@ namespace epf
class BufferCache
{
public:
std::deque<DataVecPtr> m_buffers;
std::mutex m_mutex;
BufferCache() : m_count(0)
{}

DataVecPtr fetch();
void replace(DataVecPtr&& buf);

private:
std::deque<DataVecPtr> m_buffers;
std::mutex m_mutex;
std::condition_variable m_cv;
int m_count;
};

} // namespace epf
Expand Down
1 change: 1 addition & 0 deletions epf/EpfTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ using DataVecPtr = std::unique_ptr<DataVec>;
using Totals = std::unordered_map<VoxelKey, size_t>;
constexpr int MaxPointsPerNode = 100000;
constexpr int BufSize = 4096 * 10;
constexpr int MaxBuffers = 1000;

struct Error : public std::runtime_error
{
Expand Down

0 comments on commit 0bcecc6

Please sign in to comment.