Here is something I stumbled upon yesterday: each Paragraph object sores at least on copy of a Language object!

I have not quantify the cost of this thing, but it may be useful to backport it to 2.1.x eventually.

Looking at the completion code shows a lot of other weird stuff, like collecting words even when completion is not enabled, or using too many new/delete for my taste, but this will be for later.

JMarc
>From 74ee2d5d1ee326db84cdc98e0822eb8f7323b445 Mon Sep 17 00:00:00 2001
From: Jean-Marc Lasgouttes <[email protected]>
Date: Fri, 21 Mar 2014 12:24:47 +0100
Subject: [PATCH] Do not store Languages in completion work lists, but only
 pointers

In the current code each paragraph contains a map<Language,
WordList*>, which means that it contains a full copy of the language
object. Since these objects contain translation tables nowadays, this
is a very bad idea.

This patch simply replaces the Language key by Language const *.
---
 src/LyX.cpp       |    1 +
 src/Paragraph.cpp |    4 ++--
 src/Text.cpp      |    2 +-
 src/WordList.cpp  |   14 ++++++--------
 src/WordList.h    |    6 +++---
 5 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/src/LyX.cpp b/src/LyX.cpp
index 41c69f7..b784831 100644
--- a/src/LyX.cpp
+++ b/src/LyX.cpp
@@ -33,6 +33,7 @@
 #include "FuncStatus.h"
 #include "HunspellChecker.h"
 #include "KeyMap.h"
+#include "Language.h"
 #include "LaTeXFonts.h"
 #include "LayoutFile.h"
 #include "Lexer.h"
diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp
index d32c0be..5ed510a 100644
--- a/src/Paragraph.cpp
+++ b/src/Paragraph.cpp
@@ -499,7 +499,7 @@ public:
 	TextContainer text_;
 
 	typedef set<docstring> Words;
-	typedef map<Language, Words> LangWordsMap;
+	typedef map<Language const *, Words> LangWordsMap;
 	///
 	LangWordsMap words_;
 	///
@@ -3866,7 +3866,7 @@ void Paragraph::collectWords()
 			if (cit == d->fontlist_.end())
 				return;
 			Language const * lang = cit->font().language();
-			d->words_[*lang].insert(word);
+			d->words_[lang].insert(word);
 		}
 	}
 }
diff --git a/src/Text.cpp b/src/Text.cpp
index c4cac88..491bd7b 100644
--- a/src/Text.cpp
+++ b/src/Text.cpp
@@ -2120,7 +2120,7 @@ bool Text::completionSupported(Cursor const & cur) const
 
 CompletionList const * Text::createCompletionList(Cursor const & cur) const
 {
-	WordList const * list = theWordList(*cur.getFont().language());
+	WordList const * list = theWordList(cur.getFont().language());
 	return new TextCompletionList(cur, list);
 }
 
diff --git a/src/WordList.cpp b/src/WordList.cpp
index 762e1b9..678e1f5 100644
--- a/src/WordList.cpp
+++ b/src/WordList.cpp
@@ -12,15 +12,12 @@
 
 #include "WordList.h"
 
-#include "Language.h"
-
+#include "support/lassert.h"
 #include "support/convert.h"
 #include "support/debug.h"
 #include "support/docstring.h"
 #include "support/weighted_btree.h"
 
-#include "support/lassert.h"
-
 #include <map>
 
 using namespace std;
@@ -28,12 +25,13 @@ using namespace std;
 namespace lyx {
 
 ///
-map<Language, WordList *> theGlobalWordList;
+typedef map<Language const *, WordList *> GlobalWordList;
+GlobalWordList theGlobalWordList;
 
 
-WordList * theWordList(Language const & lang)
+WordList * theWordList(Language const * lang)
 {
-	map<Language, WordList *>::iterator it = theGlobalWordList.find(lang);
+	GlobalWordList::iterator it = theGlobalWordList.find(lang);
 	if (it != theGlobalWordList.end())
 		return it->second;
 	else
@@ -44,7 +42,7 @@ WordList * theWordList(Language const & lang)
 
 void WordList::cleanupWordLists()
 {
-	map<Language, WordList *>::const_iterator it = theGlobalWordList.begin();
+	GlobalWordList::const_iterator it = theGlobalWordList.begin();
 	for (; it != theGlobalWordList.end(); ++it)
 		delete it->second;
 	theGlobalWordList.clear();
diff --git a/src/WordList.h b/src/WordList.h
index b7f88fb..bca50c5 100644
--- a/src/WordList.h
+++ b/src/WordList.h
@@ -14,10 +14,10 @@
 
 #include "support/docstring.h"
 
-#include "Language.h"
-
 namespace lyx {
 
+class Language;
+
 class WordList {
 public:
 	///
@@ -41,7 +41,7 @@ private:
 	Impl * d;
 };
 
-WordList * theWordList(Language const & lang);
+WordList * theWordList(Language const * lang);
 
 } // namespace lyx
 
-- 
1.7.9.5

Reply via email to