From 1882623d96d9aa35dba784907c584bb1783d536c Mon Sep 17 00:00:00 2001 From: Michael Niksa Date: Fri, 8 Jan 2021 11:09:57 -0800 Subject: [PATCH] use PMR pool for the vector in the bitmap so we're not alloc/freeing over and over when we know we're about to just rebuild a vector of more runs. optional will destroy the internal vector. --- src/inc/til/bitmap.h | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/inc/til/bitmap.h b/src/inc/til/bitmap.h index f992fe765b6..f83583eea01 100644 --- a/src/inc/til/bitmap.h +++ b/src/inc/til/bitmap.h @@ -185,15 +185,15 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned" return const_iterator(_bits, _sz, _sz.area()); } - const std::vector& runs() const + const gsl::span runs() const { // If we don't have cached runs, rebuild. if (!_runs.has_value()) { - _runs.emplace(begin(), end()); + _runs.emplace(begin(), end(), &_getPool()); } - // Return a reference to the runs. + // Return the runs. return _runs.value(); } @@ -454,7 +454,20 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned" til::rectangle _rc; dynamic_bitset<> _bits; - mutable std::optional> _runs; + // Memory pooling to save alloc/free work to the OS. + // The optional below will destroy the internal vector when it is reset. + // But that will do a whole free to the OS when we know we're probably going to rebuild + // it very shortly for the new set of runs. We could make a pair bool/vector + // or continue to use the optional with a PMR vector inside. + // It's static because we only need one pool to manage memory + // for all vectors of rectangles no matter which bitmap instance is making them. + static std::pmr::unsynchronized_pool_resource& _getPool() + { + static std::pmr::unsynchronized_pool_resource pool; + return pool; + } + + mutable std::optional> _runs; #ifdef UNIT_TESTING friend class ::BitmapTests;