[LyX/master] redoParagraph: avoid extra cache accesses

Jean-Marc Lasgouttes lasgouttes at lyx.org
Fri Jun 21 15:04:06 UTC 2024


commit 45dce93af73845471ca986731fd31da468a42360
Author: Jean-Marc Lasgouttes <lasgouttes at lyx.org>
Date:   Fri Jun 21 17:02:13 2024 +0200

    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    | 18 +++++++++++++++---
 src/TextMetrics.cpp |  5 +----
 2 files changed, 16 insertions(+), 7 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)
-	{
-		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)
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.


More information about the lyx-cvs mailing list