From 45dce93af73845471ca986731fd31da468a42360 Mon Sep 17 00:00:00 2001 From: Jean-Marc Lasgouttes Date: Fri, 21 Jun 2024 17:02:13 +0200 Subject: [PATCH] redoParagraph: avoid extra cache accesses To this end, CoordCacheBase::add returns true when a change was made. This uses the return value of std::unordered_map::insert. --- src/CoordCache.h | 16 ++++++++++++++-- src/TextMetrics.cpp | 5 +---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/CoordCache.h b/src/CoordCache.h index 666a3e71c7..fbe91e87c6 100644 --- a/src/CoordCache.h +++ b/src/CoordCache.h @@ -75,9 +75,21 @@ public: data_[thing].pos = Point(x, y); } - void add(T const * thing, Dimension const & dim) + // this returns true if a change is done + bool add(T const * thing, Dimension const & dim) { - data_[thing].dim = dim; + Geometry g; + g.dim = dim; + auto const result = data_.insert(std::make_pair(thing, g)); + // did a new entry get inserted? + if (result.second) + return true; + // otherwise, if the existing value is different, update it + else if (result.first->second.dim != dim) { + result.first->second.dim = dim; + return true; + } + return false; } Geometry & geometry(T const * thing) diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp index 0ae75f4355..a63adf5ebb 100644 --- a/src/TextMetrics.cpp +++ b/src/TextMetrics.cpp @@ -605,10 +605,7 @@ bool TextMetrics::redoParagraph(pit_type const pit, bool const align_rows) */ extrawidths[e.inset] = mi.extrawidth; - if (!insetCache.has(e.inset) || insetCache.dim(e.inset) != dim) { - insetCache.add(e.inset, dim); - changed = true; - } + changed |= insetCache.add(e.inset, dim); } // Transform the paragraph into a single row containing all the elements.