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.
This commit is contained in:
Jean-Marc Lasgouttes 2024-06-21 17:02:13 +02:00
parent 608929a857
commit 45dce93af7
2 changed files with 15 additions and 6 deletions

View File

@ -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)

View File

@ -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.