From 19675a1a4899f68a4e7afbd45cebc63b544650e4 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 20 Nov 2016 05:27:10 +0100 Subject: optimized RBTree: avoid unnecessary virtual RBTree and RBEntry implementation classes * whenever the implementation class was not exactly RBEntryType, it had to have a virtual destructor since RBTree cared about cleanup and had to delete its members. * Since it does not allocate them, it is consistent to remove RBTree::clear(). The destructor now only checks that subclasses have cleaned up. Implementing cleanup in the subclasses is trivial. * Consequently, RBEntryString no longer has to be virtual. HelpIndex and GotoTables are completely non-virtual now which saves memory (and a bit of cleanup speed). For QRegister, not much changes, though. --- src/rbtree.h | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) (limited to 'src/rbtree.h') diff --git a/src/rbtree.h b/src/rbtree.h index 76b2141..7730fc5 100644 --- a/src/rbtree.h +++ b/src/rbtree.h @@ -34,15 +34,15 @@ public: public: RB_ENTRY(RBEntry) nodes; - inline RBEntry * + inline RBEntryType * next(void) { - return RBTree::Tree_RB_NEXT(this); + return (RBEntryType *)RBTree::Tree_RB_NEXT(this); } - inline RBEntry * + inline RBEntryType * prev(void) { - return RBTree::Tree_RB_PREV(this); + return (RBEntryType *)RBTree::Tree_RB_PREV(this); } }; @@ -66,10 +66,14 @@ public: { RB_INIT(&head); } - virtual ~RBTree() { - clear(); + /* + * Keeping the clean up out of this wrapper class + * means we can avoid declaring EBEntry implementations + * virtual. + */ + g_assert(min() == NULL); } inline RBEntryType * @@ -108,17 +112,6 @@ public: { return (RBEntryType *)RB_MAX(Tree, &head); } - - inline void - clear(void) - { - RBEntryType *cur; - - while ((cur = min())) { - remove(cur); - delete cur; - } - } }; typedef gint (*StringCmpFunc)(const gchar *str1, const gchar *str2); @@ -138,8 +131,6 @@ public: RBEntryStringT(gchar *_key) : key(_key) {} - virtual ~RBEntryStringT() {} - inline gint compare(RBEntryStringT &other) { -- cgit v1.2.3