]> git.lyx.org Git - lyx.git/commitdiff
redoParagraph: avoid extra cache accesses
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Fri, 21 Jun 2024 15:02:13 +0000 (17:02 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Fri, 21 Jun 2024 15:02:13 +0000 (17:02 +0200)
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
src/TextMetrics.cpp

index 666a3e71c7341a89b8af0744fe667300327ec314..fbe91e87c673fc6409cea027295dc2b7e40461f7 100644 (file)
@@ -75,9 +75,21 @@ public:
                data_[thing].pos = Point(x, y);
        }
 
-       void add(T const * thing, Dimension const & dim)
-       {
-               data_[thing].dim = dim;
+       // this returns true if a change is done
+       bool add(T const * thing, Dimension const & 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)
index 0ae75f4355d05a7c8f547a926835c4b23b015908..a63adf5ebb642d7e743d2958fb1f4b5d20d761e8 100644 (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.