From 5afacb144d2b4e3f00af5b45341aafea22c5ac42 Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Mon, 31 Dec 2018 18:27:34 +0100 Subject: [PATCH] Separate LabelCache from RefCache We only assign references to label _string_. Which inset this corresponds to will be decided in context. --- src/Buffer.cpp | 51 +++++++++++++++++++------------------------------- 1 file changed, 19 insertions(+), 32 deletions(-) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 54a4dff5a7..f1de2bafe4 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -148,7 +148,9 @@ struct LabelInfo { bool active; }; -typedef vector RefCache; +typedef vector LabelCache; + +typedef map RefCache; } // namespace @@ -318,9 +320,11 @@ public: /// was missing). bool preview_error_; - /// Cache the label insets, their associated refs (with positions), - /// and whether the insets are active. + /// Cache the references associated to a label and their positions + /// in the buffer. mutable RefCache ref_cache_; + /// Cache the label insets and their activity status. + mutable LabelCache label_cache_; /// our Text that should be wrapped in an InsetText InsetText * inset; @@ -3904,33 +3908,14 @@ Buffer::References & Buffer::getReferenceCache(docstring const & label) if (d->parent()) return const_cast(masterBuffer())->getReferenceCache(label); - // In what follows, we look whether we find an active label - // with the given string in the cache. If so, return its - // references cache. If we only find an inactive one, return - // that (the last we find, coincidentally). If we find nothing, - // return an empty references cache. - static LabelInfo linfo; - linfo.inset = nullptr; - linfo.references = References(); - linfo.active = false; - bool have_inactive = false; - for (auto & rc : d->ref_cache_) { - if (rc.label == label) { - if (rc.active) - return rc.references; - else { - linfo = rc; - have_inactive = true; - } - } - } + RefCache::iterator it = d->ref_cache_.find(label); + if (it != d->ref_cache_.end()) + return it->second; - if (!have_inactive) - // We found nothing, so insert the empty one to the cache - // for further processing - d->ref_cache_.push_back(linfo); - - return linfo.references; + static References const dummy_refs = References(); + it = d->ref_cache_.insert( + make_pair(label, dummy_refs)).first; + return it->second; } @@ -3954,14 +3939,14 @@ void Buffer::setInsetLabel(docstring const & label, InsetLabel const * il, linfo.label = label; linfo.inset = il; linfo.active = active; - masterBuffer()->d->ref_cache_.push_back(linfo); + masterBuffer()->d->label_cache_.push_back(linfo); } InsetLabel const * Buffer::insetLabel(docstring const & label, bool const active) const { - for (auto & rc : masterBuffer()->d->ref_cache_) { + for (auto & rc : masterBuffer()->d->label_cache_) { if (rc.label == label && (rc.active || !active)) return rc.inset; } @@ -3980,8 +3965,10 @@ bool Buffer::activeLabel(docstring const & label) const void Buffer::clearReferenceCache() const { - if (!d->parent()) + if (!d->parent()) { d->ref_cache_.clear(); + d->label_cache_.clear(); + } }