summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMarc Jeanmougin <marc@jeanmougin.fr>2018-09-12 16:02:42 +0000
committerMarc Jeanmougin <marc@jeanmougin.fr>2018-09-12 16:02:42 +0000
commit26f470dfa8cd5424f3e63b45955fddd3d2860f15 (patch)
treec29f9f17eedf8de1c8f1ad08dc43356a28ce92b6 /src
parentFix preferences crash (diff)
downloadinkscape-26f470dfa8cd5424f3e63b45955fddd3d2860f15.tar.gz
inkscape-26f470dfa8cd5424f3e63b45955fddd3d2860f15.zip
More caching in prefs
Diffstat (limited to 'src')
-rw-r--r--src/preferences.cpp33
-rw-r--r--src/preferences.h22
2 files changed, 46 insertions, 9 deletions
diff --git a/src/preferences.cpp b/src/preferences.cpp
index c32d6082d..419ea449d 100644
--- a/src/preferences.cpp
+++ b/src/preferences.cpp
@@ -509,7 +509,7 @@ void Preferences::mergeStyle(Glib::ustring const &pref_path, SPCSSAttr *style)
*/
void Preferences::remove(Glib::ustring const &pref_path)
{
- auto it = cachedRawValue.find(pref_path);
+ auto it = cachedRawValue.find(pref_path.c_str());
if (it != cachedRawValue.end()) cachedRawValue.erase(it);
Inkscape::XML::Node *node = _getNode(pref_path, false);
@@ -754,7 +754,7 @@ Inkscape::XML::Node *Preferences::_getNode(Glib::ustring const &pref_key, bool c
void Preferences::_getRawValue(Glib::ustring const &path, gchar const *&result)
{
// will return empty string if `path` was not in the cache yet
- auto& cacheref = cachedRawValue[path];
+ auto& cacheref = cachedRawValue[path.c_str()];
if (!cacheref.empty()) {
if (cacheref == RAWCACHE_CODE_NULL) {
@@ -799,7 +799,7 @@ void Preferences::_setRawValue(Glib::ustring const &path, Glib::ustring const &v
// set the attribute
Inkscape::XML::Node *node = _getNode(node_key, true);
node->setAttribute(attr_key.c_str(), value.c_str());
- cachedRawValue[path] = RAWCACHE_CODE_VALUE + value;
+ cachedRawValue[path.c_str()] = RAWCACHE_CODE_VALUE + value;
}
// The _extract* methods are where the actual work is done - they define how preferences are stored
@@ -807,30 +807,41 @@ void Preferences::_setRawValue(Glib::ustring const &path, Glib::ustring const &v
bool Preferences::_extractBool(Entry const &v)
{
+ if (v.cached_bool) return v.value_bool;
+ v.cached_bool = true;
gchar const *s = static_cast<gchar const *>(v._value);
if ( !s[0] || !strcmp(s, "0") || !strcmp(s, "false") ) {
return false;
} else {
+ v.value_bool = true;
return true;
}
}
int Preferences::_extractInt(Entry const &v)
{
+ if (v.cached_int) return v.value_int;
+ v.cached_int = true;
gchar const *s = static_cast<gchar const *>(v._value);
if ( !strcmp(s, "true") ) {
+ v.value_int = 1;
return true;
} else if ( !strcmp(s, "false") ) {
+ v.value_int = 0;
return false;
} else {
- return atoi(s);
+ v.value_int = atoi(s);
+ return v.value_int;
}
}
double Preferences::_extractDouble(Entry const &v)
{
+ if (v.cached_double) return v.value_double;
+ v.cached_double = true;
gchar const *s = static_cast<gchar const *>(v._value);
- return g_ascii_strtod(s, nullptr);
+ v.value_double = g_ascii_strtod(s, nullptr);
+ return v.value_double;
}
double Preferences::_extractDouble(Entry const &v, Glib::ustring const &requested_unit)
@@ -852,6 +863,9 @@ Glib::ustring Preferences::_extractString(Entry const &v)
Glib::ustring Preferences::_extractUnit(Entry const &v)
{
+ if (v.cached_unit) return v.value_unit;
+ v.cached_unit = true;
+ v.value_unit = "";
gchar const *str = static_cast<gchar const *>(v._value);
gchar const *e;
g_ascii_strtod(str, (char **) &e);
@@ -863,12 +877,15 @@ Glib::ustring Preferences::_extractUnit(Entry const &v)
/* Unitless */
return "";
} else {
- return Glib::ustring(e);
+ v.value_unit = Glib::ustring(e);
+ return v.value_unit;
}
}
guint32 Preferences::_extractColor(Entry const &v)
{
+ if (v.cached_color) return v.value_color;
+ v.cached_color = true;
gchar const *s = static_cast<gchar const *>(v._value);
std::istringstream hr(s);
guint32 color;
@@ -878,13 +895,17 @@ guint32 Preferences::_extractColor(Entry const &v)
} else {
hr >> color;
}
+ v.value_color = color;
return color;
}
SPCSSAttr *Preferences::_extractStyle(Entry const &v)
{
+ if (v.cached_style) return v.value_style;
+ v.cached_style = true;
SPCSSAttr *style = sp_repr_css_attr_new();
sp_repr_css_attr_add_from_string(style, static_cast<gchar const*>(v._value));
+ v.value_style = style;
return style;
}
diff --git a/src/preferences.h b/src/preferences.h
index ac6bf91a1..c5b08c07c 100644
--- a/src/preferences.h
+++ b/src/preferences.h
@@ -17,6 +17,7 @@
#include <cfloat>
#include <glibmm/ustring.h>
#include <map>
+#include <unordered_map>
#include <utility>
#include <vector>
@@ -123,7 +124,11 @@ public:
friend class Preferences; // Preferences class has to access _value
public:
~Entry() = default;
- Entry() : _pref_path(""), _value(nullptr) {} // needed to enable use in maps
+ Entry() : _pref_path(""), _value(nullptr),
+ cached_bool(false), cached_int(false), cached_double(false), cached_unit(false), cached_color(false), cached_style(false) {
+
+
+ } // needed to enable use in maps
Entry(Entry const &other) = default;
/**
@@ -228,10 +233,21 @@ public:
*/
Glib::ustring getEntryName() const;
private:
- Entry(Glib::ustring path, void const *v) : _pref_path(std::move(path)), _value(v) {}
+ Entry(Glib::ustring path, void const *v) : _pref_path(std::move(path)), _value(v),
+ cached_bool(false), cached_int(false), cached_double(false), cached_unit(false), cached_color(false), cached_style(false) {}
Glib::ustring _pref_path;
void const *_value;
+
+ mutable bool value_bool;
+ mutable int value_int;
+ mutable double value_double;
+ mutable Glib::ustring value_unit;
+ mutable guint32 value_color;
+ mutable SPCSSAttr* value_style;
+
+ mutable bool cached_bool, cached_int, cached_double, cached_unit, cached_color, cached_style;
+
};
// utility methods
@@ -552,7 +568,7 @@ private:
ErrorReporter* _errorHandler; ///< Pointer to object reporting errors.
bool _writable; ///< Will the preferences be saved at exit?
bool _hasError; ///< Indication that some error has occurred;
- std::map<Glib::ustring, Glib::ustring> cachedRawValue;
+ std::unordered_map<std::string, Glib::ustring> cachedRawValue;
/// Wrapper class for XML node observers
class PrefNodeObserver;