[LyX/master] Do not update statisitics if buffer has not changed

Jean-Marc Lasgouttes lasgouttes at lyx.org
Wed Jul 24 20:38:50 UTC 2024


commit 0d50a8417f059f7bffc86b562817042564970c05
Author: Jean-Marc Lasgouttes <lasgouttes at lyx.org>
Date:   Wed Jul 24 22:19:32 2024 +0200

    Do not update statisitics if buffer has not changed
    
    Rely on the newly-introduced Buffer::id() to skip statistics
    computation if the id is the same as last time. This will reduce the
    annoyance of updates triggering at random times.
    
    Take this occasion to clean code up:
    
    - add 'skip' parameter (true by default) to Statistics::update to indicate
      that the insets that do not produce output should be skipped.
    
    - use a trailing underscrore for private members
---
 src/Statistics.cpp | 21 +++++++++++++++------
 src/Statistics.h   | 25 ++++++++++++++-----------
 2 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/src/Statistics.cpp b/src/Statistics.cpp
index eabae8f399..b0e109b584 100644
--- a/src/Statistics.cpp
+++ b/src/Statistics.cpp
@@ -13,11 +13,13 @@
 
 #include "Statistics.h"
 
+#include "Buffer.h"
 #include "Paragraph.h"
 #include "Text.h"
 #include "Cursor.h"
 
 #include "support/lassert.h"
+#include "support/debug.h"
 #include "support/lstrings.h"
 #include "support/textutils.h"
 
@@ -27,10 +29,17 @@ namespace lyx {
 using namespace support;
 
 
-void Statistics::update(CursorData const & cur)
+void Statistics::update(CursorData const & cur, bool skip)
 {
+	// early exit if the buffer has not changed since last time
+	if (stats_id_ == cur.buffer()->id())
+               return;
+
 	// reset counts
 	*this = Statistics();
+	skip_no_output_ = skip;
+	stats_id_ = cur.buffer()->id();
+
 	if (cur.selection()) {
 		if (cur.inMathed())
 			return;
@@ -91,15 +100,15 @@ void Statistics::update(Paragraph const & par, pos_type from, pos_type to)
 		// Stuff that we skip
 		if (par.isDeleted(pos))
 			continue;
-		if (ins && skip_no_output && !ins->producesOutput())
+		if (ins && skip_no_output_ && !ins->producesOutput())
 			continue;
 
 		// words
 		if (par.isWordSeparator(pos))
-			inword = false;
-		else if (!inword) {
+			inword_ = false;
+		else if (!inword_) {
 			++word_count;
-			inword = true;
+			inword_ = true;
 		}
 
 		if (ins)
@@ -112,7 +121,7 @@ void Statistics::update(Paragraph const & par, pos_type from, pos_type to)
 				++blank_count;
 		}
 	}
-	inword = false;
+	inword_ = false;
 }
 
 
diff --git a/src/Statistics.h b/src/Statistics.h
index 7439de06cf..e91e9fc340 100644
--- a/src/Statistics.h
+++ b/src/Statistics.h
@@ -25,22 +25,21 @@ class Paragraph;
 // Class used to compute letters/words statistics on buffer or selection
 class Statistics {
 public:
+	/// Count characters in the whole document, or in the selection if
+	/// there is one. This is the main entry point.
+	void update(CursorData const & cur, bool skip = true);
+
+	/// Helper: count chars and words in this string
+	void update(docstring const & s);
+	/// Helper: count chars and words in the paragraphs of \c text
+	void update(Text const & text);
+
 	// Number of words
 	int word_count = 0;
 	// Number of non blank characters
 	int char_count = 0;
 	// Number of blank characters
 	int blank_count = 0;
-	// Indicate whether parts that are not output should be counted.
-	bool skip_no_output = true;
-
-	/// Count characters in the whole document, or in the selection if
-	/// there is one. This is the main entry point.
-	void update(CursorData const & cur);
-	///  Count chars and words in this string
-	void update(docstring const & s);
-	/// Count chars and words in the paragraphs of \c text
-	void update(Text const & text);
 
 private:
 
@@ -55,8 +54,12 @@ private:
 	 */
 	void update(Paragraph const & par, pos_type from = 0, pos_type to = -1);
 
+	// Indicate whether parts that produce no output should be counted.
+	bool skip_no_output_;
 	// Used in the code to track status
-	bool inword = false;
+	bool inword_ = false;
+	// The buffer id at last statistics computation.
+	int stats_id_ = -1;
 };
 
 }


More information about the lyx-cvs mailing list