[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [51488] trunk/blender: small optimization for BLI_heap(), give some speedup in decimeter.

Campbell Barton ideasman42 at gmail.com
Mon Oct 22 05:26:01 CEST 2012


Revision: 51488
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=51488
Author:   campbellbarton
Date:     2012-10-22 03:25:53 +0000 (Mon, 22 Oct 2012)
Log Message:
-----------
small optimization for BLI_heap(), give some speedup in decimeter.
- use unsigned ints only (where mixing signed/unsigned)
- turn heap_swap into an inline function, add SWAP_TVAL macro to swap values using a temp value as storage.
- added type checking SHIFT3/4 macros

also style cleanup for CTR_Map

Modified Paths:
--------------
    trunk/blender/intern/container/CTR_HashedPtr.h
    trunk/blender/intern/container/CTR_Map.h
    trunk/blender/source/blender/blenlib/BLI_heap.h
    trunk/blender/source/blender/blenlib/BLI_utildefines.h
    trunk/blender/source/blender/blenlib/intern/BLI_heap.c
    trunk/blender/source/blender/modifiers/intern/MOD_decimate.c

Modified: trunk/blender/intern/container/CTR_HashedPtr.h
===================================================================
--- trunk/blender/intern/container/CTR_HashedPtr.h	2012-10-22 02:39:26 UTC (rev 51487)
+++ trunk/blender/intern/container/CTR_HashedPtr.h	2012-10-22 03:25:53 UTC (rev 51488)
@@ -43,12 +43,19 @@
 
 class CTR_HashedPtr
 {
-	void* m_valptr;
+	void *m_valptr;
 public:
-	CTR_HashedPtr(void* val) : m_valptr(val) {};
-	unsigned int hash() const { return CTR_Hash(m_valptr);};
-	inline friend bool operator ==(const CTR_HashedPtr & rhs, const CTR_HashedPtr & lhs) { return rhs.m_valptr == lhs.m_valptr;};
-	void *getValue() const { return m_valptr; }
+	CTR_HashedPtr(void *val) : m_valptr(val) {
+	};
+	unsigned int hash() const {
+		return CTR_Hash(m_valptr);
+	};
+	inline friend bool operator ==(const CTR_HashedPtr & rhs, const CTR_HashedPtr & lhs) {
+		return rhs.m_valptr == lhs.m_valptr;
+	};
+	void *getValue() const {
+		return m_valptr;
+	}
 };
 
 #endif  /* __CTR_HASHEDPTR_H__ */

Modified: trunk/blender/intern/container/CTR_Map.h
===================================================================
--- trunk/blender/intern/container/CTR_Map.h	2012-10-22 02:39:26 UTC (rev 51487)
+++ trunk/blender/intern/container/CTR_Map.h	2012-10-22 03:25:53 UTC (rev 51488)
@@ -37,13 +37,14 @@
 private:
 	struct Entry {
 		Entry (Entry *next, Key key, Value value) :
-		    m_next(next),
-		    m_key(key),
-		    m_value(value) {}
+			m_next(next),
+			m_key(key),
+			m_value(value) {
+		}
 
 		Entry *m_next;
-		Key    m_key;
-		Value  m_value;
+		Key m_key;
+		Value m_value;
 	};
 
 public:
@@ -62,18 +63,18 @@
 		for (int i = 0; i < m_num_buckets; ++i) {
 			m_buckets[i] = 0;
 
-			for (Entry *entry = map.m_buckets[i]; entry; entry=entry->m_next)
+			for (Entry *entry = map.m_buckets[i]; entry; entry = entry->m_next) {
 				insert(entry->m_key, entry->m_value);
+			}
 		}
 	}
 
-	int size() {
-		int count=0;
-		for (int i=0;i<m_num_buckets;i++)
-		{
-			Entry* bucket = m_buckets[i];
-			while(bucket)
-			{
+	int size()
+	{
+		int count = 0;
+		for (int i = 0; i < m_num_buckets; i++) {
+			Entry *bucket = m_buckets[i];
+			while (bucket) {
 				bucket = bucket->m_next;
 				count++;
 			}
@@ -81,15 +82,13 @@
 		return count;
 	}
 
-	Value* at(int index) {
-		int count=0;
-		for (int i=0;i<m_num_buckets;i++)
-		{
-			Entry* bucket = m_buckets[i];
-			while(bucket)
-			{
-				if (count==index)
-				{
+	Value *at(int index)
+	{
+		int count = 0;
+		for (int i = 0; i < m_num_buckets; i++) {
+			Entry *bucket = m_buckets[i];
+			while (bucket) {
+				if (count == index) {
 					return &bucket->m_value;
 				}
 				bucket = bucket->m_next;
@@ -99,15 +98,13 @@
 		return 0;
 	}
 
-	Key* getKey(int index) {
-		int count=0;
-		for (int i=0;i<m_num_buckets;i++)
-		{
-			Entry* bucket = m_buckets[i];
-			while(bucket)
-			{
-				if (count==index)
-				{
+	Key *getKey(int index)
+	{
+		int count = 0;
+		for (int i = 0; i < m_num_buckets; i++) {
+			Entry *bucket = m_buckets[i];
+			while (bucket) {
+				if (count == index) {
 					return &bucket->m_key;
 				}
 				bucket = bucket->m_next;
@@ -117,7 +114,8 @@
 		return 0;
 	}
 
-	void clear() {
+	void clear()
+	{
 		for (int i = 0; i < m_num_buckets; ++i) {
 			Entry *entry_ptr = m_buckets[i];
 
@@ -130,12 +128,14 @@
 		}
 	}
 
-	~CTR_Map() {
+	~CTR_Map()
+	{
 		clear();
-		delete [] m_buckets;
+		delete[] m_buckets;
 	}
 
-	void insert(const Key& key, const Value& value) {
+	void insert(const Key& key, const Value& value)
+	{
 		Entry *entry_ptr = m_buckets[key.hash() % m_num_buckets];
 		while ((entry_ptr != 0) && !(key == entry_ptr->m_key)) {
 			entry_ptr = entry_ptr->m_next;
@@ -150,7 +150,8 @@
 		}
 	}
 
-	void remove(const Key& key) {
+	void remove(const Key& key)
+	{
 		Entry **entry_ptr = &m_buckets[key.hash() % m_num_buckets];
 		while ((*entry_ptr != 0) && !(key == (*entry_ptr)->m_key)) {
 			entry_ptr = &(*entry_ptr)->m_next;
@@ -163,7 +164,8 @@
 		}
 	}
 
-	Value *operator[](Key key) {
+	Value *operator[](Key key)
+	{
 		Entry *bucket = m_buckets[key.hash() % m_num_buckets];
 		while ((bucket != 0) && !(key == bucket->m_key)) {
 			bucket = bucket->m_next;

Modified: trunk/blender/source/blender/blenlib/BLI_heap.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_heap.h	2012-10-22 02:39:26 UTC (rev 51487)
+++ trunk/blender/source/blender/blenlib/BLI_heap.h	2012-10-22 03:25:53 UTC (rev 51488)
@@ -57,7 +57,7 @@
 int             BLI_heap_is_empty(Heap *heap);
 
 /* Return the size of the heap. */
-int             BLI_heap_size(Heap *heap);
+unsigned int    BLI_heap_size(Heap *heap);
 
 /* Return the top node of the heap. This is the node with the lowest value. */
 HeapNode       *BLI_heap_top(Heap *heap);

Modified: trunk/blender/source/blender/blenlib/BLI_utildefines.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_utildefines.h	2012-10-22 02:39:26 UTC (rev 51487)
+++ trunk/blender/source/blender/blenlib/BLI_utildefines.h	2012-10-22 03:25:53 UTC (rev 51488)
@@ -41,34 +41,6 @@
 #endif
 
 
-#define ELEM(a, b, c)           ((a) == (b) || (a) == (c))
-#define ELEM3(a, b, c, d)       (ELEM(a, b, c) || (a) == (d) )
-#define ELEM4(a, b, c, d, e)    (ELEM(a, b, c) || ELEM(a, d, e) )
-#define ELEM5(a, b, c, d, e, f) (ELEM(a, b, c) || ELEM3(a, d, e, f) )
-#define ELEM6(a, b, c, d, e, f, g)      (ELEM(a, b, c) || ELEM4(a, d, e, f, g) )
-#define ELEM7(a, b, c, d, e, f, g, h)   (ELEM3(a, b, c, d) || ELEM4(a, e, f, g, h) )
-#define ELEM8(a, b, c, d, e, f, g, h, i)        (ELEM4(a, b, c, d, e) || ELEM4(a, f, g, h, i) )
-#define ELEM9(a, b, c, d, e, f, g, h, i, j)        (ELEM4(a, b, c, d, e) || ELEM5(a, f, g, h, i, j) )
-#define ELEM10(a, b, c, d, e, f, g, h, i, j, k)        (ELEM4(a, b, c, d, e) || ELEM6(a, f, g, h, i, j, k) )
-#define ELEM11(a, b, c, d, e, f, g, h, i, j, k, l)        (ELEM4(a, b, c, d, e) || ELEM7(a, f, g, h, i, j, k, l) )
-
-/* shift around elements */
-#define SHIFT3(type, a, b, c)  {                                              \
-		type tmp;                                                             \
-		tmp = a;                                                              \
-		a = c;                                                                \
-		c = b;                                                                \
-		b = tmp;                                                              \
-} (void)0
-#define SHIFT4(type, a, b, c, d)  {                                           \
-		type tmp;                                                             \
-		tmp = a;                                                              \
-		a = d;                                                                \
-		d = c;                                                                \
-		c = b;                                                                \
-		b = tmp;                                                              \
-} (void)0
-
 /* min/max */
 #define MIN2(x, y)               ( (x) < (y) ? (x) : (y) )
 #define MIN3(x, y, z)             MIN2(MIN2((x), (y)), (z) )
@@ -116,22 +88,28 @@
 /* Causes warning:
  * incompatible types when assigning to type 'Foo' from type 'Bar'
  * ... the compiler optimizes away the temp var */
-#ifndef CHECK_TYPE
 #ifdef __GNUC__
 #define CHECK_TYPE(var, type)  {  \
 	__typeof(var) *__tmp;         \
 	__tmp = (type *)NULL;         \
 	(void)__tmp;                  \
 } (void)0
+
+#define CHECK_TYPE_PAIR(var_a, var_b)  {  \
+	__typeof(var_a) *__tmp;               \
+	__tmp = (__typeof(var_b) *)NULL;      \
+	(void)__tmp;                          \
+} (void)0
 #else
-#define CHECK_TYPE(var, type)
+#  define CHECK_TYPE(var, type)
+#  define CHECK_TYPE_PAIR(var_a, var_b)
 #endif
-#endif
 
 /* can be used in simple macros */
 #define CHECK_TYPE_INLINE(val, type) \
 	((void)(((type *)0) != (val)))
 
+
 #ifndef SWAP
 #  define SWAP(type, a, b)  {  \
 	type sw_ap;                \
@@ -143,6 +121,53 @@
 } (void)0
 #endif
 
+/* swap with a temp value */
+#define SWAP_TVAL(tval, a, b)  {  \
+	CHECK_TYPE_PAIR(tval, a);     \
+	CHECK_TYPE_PAIR(tval, b);     \
+	(tval) = (a);                 \
+	(a) = (b);                    \
+	(b) = (tval);                 \
+} (void)0
+
+
+#define ELEM(a, b, c)           ((a) == (b) || (a) == (c))
+#define ELEM3(a, b, c, d)       (ELEM(a, b, c) || (a) == (d) )
+#define ELEM4(a, b, c, d, e)    (ELEM(a, b, c) || ELEM(a, d, e) )
+#define ELEM5(a, b, c, d, e, f) (ELEM(a, b, c) || ELEM3(a, d, e, f) )
+#define ELEM6(a, b, c, d, e, f, g)      (ELEM(a, b, c) || ELEM4(a, d, e, f, g) )
+#define ELEM7(a, b, c, d, e, f, g, h)   (ELEM3(a, b, c, d) || ELEM4(a, e, f, g, h) )
+#define ELEM8(a, b, c, d, e, f, g, h, i)        (ELEM4(a, b, c, d, e) || ELEM4(a, f, g, h, i) )
+#define ELEM9(a, b, c, d, e, f, g, h, i, j)        (ELEM4(a, b, c, d, e) || ELEM5(a, f, g, h, i, j) )
+#define ELEM10(a, b, c, d, e, f, g, h, i, j, k)        (ELEM4(a, b, c, d, e) || ELEM6(a, f, g, h, i, j, k) )
+#define ELEM11(a, b, c, d, e, f, g, h, i, j, k, l)        (ELEM4(a, b, c, d, e) || ELEM7(a, f, g, h, i, j, k, l) )
+
+/* shift around elements */
+#define SHIFT3(type, a, b, c)  {                                              \
+	type tmp;                                                                 \
+	CHECK_TYPE(a, type);                                                      \
+	CHECK_TYPE(b, type);                                                      \
+	CHECK_TYPE(c, type);                                                      \
+	tmp = a;                                                                  \
+	a = c;                                                                    \
+	c = b;                                                                    \
+	b = tmp;                                                                  \
+} (void)0
+
+#define SHIFT4(type, a, b, c, d)  {                                           \
+	type tmp;                                                                 \
+	CHECK_TYPE(a, type);                                                      \
+	CHECK_TYPE(b, type);                                                      \
+	CHECK_TYPE(c, type);                                                      \
+	CHECK_TYPE(d, type);                                                      \
+	tmp = a;                                                                  \

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list