[LyX/2.4.x] Do not run updateMacros if the buffer has not changed

Jean-Marc Lasgouttes lasgouttes at lyx.org
Fri Aug 30 09:11:34 UTC 2024


commit e2cf6731c0ca3a3d4edec19a24754a8ddcbd3284
Author: Jean-Marc Lasgouttes <lasgouttes at lyx.org>
Date:   Sat Jul 20 22:31:34 2024 +0200

    Do not run updateMacros if the buffer has not changed
    
    Each buffer now has an id which is increased when it is marked dirty
    (or when one of its relatives is marked dirty).
    
    This can be a big win since updateMacros is very expensive.
    
    (cherry picked from commit 999fb37ebb1aecfda9889fa6a2b2fbb15133f2aa)
    (cherry picked from commit fe1a3b57df2cd6adf1bd4752d05b1aae40a4e5b7)
---
 src/Buffer.cpp | 27 +++++++++++++++++++++++++++
 src/Buffer.h   |  6 ++++++
 src/Undo.cpp   |  6 ++++--
 status.24x     |  3 +++
 4 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/src/Buffer.cpp b/src/Buffer.cpp
index ef7367ca23..b32dff040e 100644
--- a/src/Buffer.cpp
+++ b/src/Buffer.cpp
@@ -253,6 +253,11 @@ public:
 	///
 	Undo undo_;
 
+	/// This is increased every time the buffer or one of its relatives is marked dirty
+	int id_ = 0;
+	/// The buffer id at last updateMacros invokation
+	int update_macros_id_ = -1;
+
 	/// A cache for the bibfiles (including bibfiles of loaded child
 	/// documents), needed for appropriate update of natbib labels.
 	mutable docstring_list bibfiles_cache_;
@@ -808,6 +813,20 @@ Undo const & Buffer::undo() const
 }
 
 
+int Buffer::id() const
+{
+	return d->id_;
+}
+
+
+void Buffer::updateId()
+{
+	++d->id_;
+	for(Buffer * b : allRelatives())
+		++(b->d->id_);
+}
+
+
 void Buffer::setChild(DocIterator const & dit, Buffer * child)
 {
 	d->children_positions[child] = dit;
@@ -3322,6 +3341,9 @@ void Buffer::markDirty()
 
 	for (auto & depit : d->dep_clean)
 		depit.second = false;
+
+	// Update the buffer and its relatives' ids.
+	updateId();
 }
 
 
@@ -3916,6 +3938,11 @@ void Buffer::updateMacros() const
 	if (d->macro_lock)
 		return;
 
+	// early exit if the buffer has not changed since last time
+	if (d->gui_ && d->update_macros_id_ == d->id_)
+		return;
+	d->update_macros_id_ = d->id_;
+
 	LYXERR(Debug::MACROS, "updateMacro of " << d->filename.onlyFileName());
 
 	// start with empty table
diff --git a/src/Buffer.h b/src/Buffer.h
index ee37098055..824e10c890 100644
--- a/src/Buffer.h
+++ b/src/Buffer.h
@@ -660,6 +660,12 @@ public:
 	///
 	Undo const & undo() const;
 
+	/// poor man versioning of the buffer (and its relatives).
+	int id() const;
+	/// change the id of this buffer and its relatives (indicating
+	/// something has changed). This is currently used by updateMacros().
+	void updateId();
+
 	/// This function is called when the buffer is changed.
 	void changed(bool update_metrics) const;
 	///
diff --git a/src/Undo.cpp b/src/Undo.cpp
index 4e970df5a0..ef149631a3 100644
--- a/src/Undo.cpp
+++ b/src/Undo.cpp
@@ -512,9 +512,11 @@ void Undo::Private::doUndoRedoAction(CursorData & cur, UndoElementStack & stack,
 
 	if (!undo.cur_before.empty())
 		cur = undo.cur_before;
-	if (undo.lyx_clean)
+	if (undo.lyx_clean) {
 		buffer_.markClean();
-	else
+		// since we have changed the buffer, update its id.
+		buffer_.updateId();
+	} else
 		buffer_.markDirty();
 	// Now that we're done with undo, we pop it off the stack.
 	stack.pop();
diff --git a/status.24x b/status.24x
index c826469b85..5ad6c0e865 100644
--- a/status.24x
+++ b/status.24x
@@ -54,6 +54,9 @@ What's new
 
 - fix crash when using Save As... with previews enabled (bug 13091).
 
+- Speedup interactive use by avoiding expensive math macros bookkeeping
+  when possible.
+
 
 * DOCUMENTATION AND LOCALIZATION
 


More information about the lyx-cvs mailing list