aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/undo.h
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2016-02-10 15:08:46 +0100
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2016-02-10 15:46:17 +0100
commit1da5bdeb986657c5cfd83d495d15b7f2308d3b5b (patch)
tree258c5297dea9b2296a7ff231c73dd101ff1ab18d /src/undo.h
parentb21d294b8168e20282f31026cbc2e50a3bcd0222 (diff)
downloadsciteco-1da5bdeb986657c5cfd83d495d15b7f2308d3b5b.tar.gz
avoid unnecessary undo token allocations in batch mode: greatly speeds up batch mode
* by using variadic templates, UndoStack::push() is now responsible for allocating undo tokens. This is avoided in batch mode. * The old UndoStack::push(UndoToken *) method has been made private to avoid confusion around UndoStack's API. The old UndoStack::push() no longer needs to handle !undo.enabled, but at least asserts on it. * C++11 support is now required, so variadic templates can be used. This could have also been done using manual undo.enabled checks; or using multiple versions of the template with different numbers of template arguments. The latter could be done if we one day have to support a non-C++11 compiler. However since we're depending on GCC 4.4, variadic template use should be OK. Clang supports it since v2.9. * Sometimes, undo token pushing passed ownership of some memory to the undo token. The old behaviour was relied on to reclaim the memory even in batch mode -- the undo token was always deleted. To avoid leaks or repeated manual undo.enabled checking, another method UndoStack::push_own() had to be introduced that makes sure that an undo token is always created. In batch mode (!undo.enabled), this will however create the object on the stack which is much cheaper than using `new`. * Having to know which kind of undo token is to be pushed (taking ownership or not) is inconvenient. It may be better to add static methods to the UndoToken classes that can take care of reclaiming memory. * Benchmarking certain SciTECO scripts have shown 50% (!!!) speed increases at the highest possible optimization level (-O3 -mtune=native -march=native).
Diffstat (limited to 'src/undo.h')
-rw-r--r--src/undo.h48
1 files changed, 44 insertions, 4 deletions
diff --git a/src/undo.h b/src/undo.h
index 325aee9..1e92b16 100644
--- a/src/undo.h
+++ b/src/undo.h
@@ -175,6 +175,8 @@ extern class UndoStack {
*/
gsize memory_usage;
+ void push(UndoToken *token);
+
public:
bool enabled;
@@ -194,13 +196,50 @@ public:
void set_memory_limit(gsize new_limit = 0);
- void push(UndoToken *token);
+ /**
+ * Allocate and push undo token.
+ *
+ * This does nothing if undo is disabled and should
+ * not be used when ownership of some data is to be
+ * passed to the undo token.
+ */
+ template <class TokenType, typename... Params>
+ inline void
+ push(Params && ... params)
+ {
+ if (enabled)
+ push(new TokenType(params...));
+ }
+
+ /**
+ * Allocate and push undo token, passing ownership.
+ *
+ * This creates and deletes the undo token cheaply
+ * if undo is disabled, so that data whose ownership
+ * is passed to the undo token is correctly reclaimed.
+ *
+ * @bug We must know which version of push to call
+ * depending on the token type. This could be hidden
+ * if UndoTokens had static push methods that take care
+ * of reclaiming memory.
+ */
+ template <class TokenType, typename... Params>
+ inline void
+ push_own(Params && ... params)
+ {
+ if (enabled) {
+ push(new TokenType(params...));
+ } else {
+ /* ensures that all memory is reclaimed */
+ TokenType dummy(params...);
+ }
+ }
template <typename Type>
inline Type &
push_var(Type &variable, Type value)
{
- push(new UndoTokenVariable<Type>(variable, value));
+ push<UndoTokenVariable<Type>>(variable, value);
return variable;
}
@@ -214,7 +253,7 @@ public:
inline gchar *&
push_str(gchar *&variable, gchar *str)
{
- push(new UndoTokenString(variable, str));
+ push<UndoTokenString>(variable, str);
return variable;
}
inline gchar *&
@@ -227,7 +266,8 @@ public:
inline Type *&
push_obj(Type *&variable, Type *obj)
{
- push(new UndoTokenObject<Type>(variable, obj));
+ /* pass ownership of original object */
+ push_own<UndoTokenObject<Type>>(variable, obj);
return variable;
}