use LIKELY/UNLIKELY macros for operations that run a lot.
authorCampbell Barton <ideasman42@gmail.com>
Sun, 26 Feb 2012 23:59:08 +0000 (23:59 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sun, 26 Feb 2012 23:59:08 +0000 (23:59 +0000)
source/blender/bmesh/intern/bmesh_iterators_inline.c
source/blender/python/bmesh/bmesh_py_types.c
source/blender/python/bmesh/bmesh_py_types.h
source/blender/python/intern/bpy_rna.h
source/blender/python/intern/bpy_rna_anim.c
source/blender/python/mathutils/mathutils.c

index e4ad53ee46d6056ee68bb164371bcca68f519d4c..37105ea95d53d9bc16e5fd6f319e54a052d96749 100644 (file)
@@ -76,80 +76,90 @@ BM_INLINE int BM_iter_init(BMIter *iter, BMesh *bm, const char itype, void *data
                        iter->step =  bmiter__face_of_mesh_step;
                        break;
                case BM_EDGES_OF_VERT:
-                       if (!data)
+                       if (UNLIKELY(!data)) {
                                return FALSE;
+                       }
 
                        iter->begin = bmiter__edge_of_vert_begin;
                        iter->step =  bmiter__edge_of_vert_step;
                        iter->vdata = data;
                        break;
                case BM_FACES_OF_VERT:
-                       if (!data)
+                       if (UNLIKELY(!data)) {
                                return FALSE;
+                       }
 
                        iter->begin = bmiter__face_of_vert_begin;
                        iter->step =  bmiter__face_of_vert_step;
                        iter->vdata = data;
                        break;
                case BM_LOOPS_OF_VERT:
-                       if (!data)
+                       if (UNLIKELY(!data)) {
                                return FALSE;
+                       }
 
                        iter->begin = bmiter__loop_of_vert_begin;
                        iter->step =  bmiter__loop_of_vert_step;
                        iter->vdata = data;
                        break;
                case BM_VERTS_OF_EDGE:
-                       if (!data)
+                       if (UNLIKELY(!data)) {
                                return FALSE;
+                       }
 
                        iter->begin = bmiter__vert_of_edge_begin;
                        iter->step =  bmiter__vert_of_edge_step;
                        iter->edata = data;
                        break;
                case BM_FACES_OF_EDGE:
-                       if (!data)
+                       if (UNLIKELY(!data)) {
                                return FALSE;
+                       }
 
                        iter->begin = bmiter__face_of_edge_begin;
                        iter->step =  bmiter__face_of_edge_step;
                        iter->edata = data;
                        break;
                case BM_VERTS_OF_FACE:
-                       if (!data)
+                       if (UNLIKELY(!data)) {
                                return FALSE;
+                       }
 
                        iter->begin = bmiter__vert_of_face_begin;
                        iter->step =  bmiter__vert_of_face_step;
                        iter->pdata = data;
                        break;
                case BM_EDGES_OF_FACE:
-                       if (!data)
+                       if (UNLIKELY(!data)) {
                                return FALSE;
+                       }
 
                        iter->begin = bmiter__edge_of_face_begin;
                        iter->step =  bmiter__edge_of_face_step;
                        iter->pdata = data;
                        break;
                case BM_LOOPS_OF_FACE:
-                       if (!data)
+                       if (UNLIKELY(!data)) {
                                return FALSE;
+                       }
 
                        iter->begin = bmiter__loop_of_face_begin;
                        iter->step =  bmiter__loop_of_face_step;
                        iter->pdata = data;
                        break;
                case BM_LOOPS_OF_LOOP:
-                       if (!data)
+                       if (UNLIKELY(!data)) {
                                return FALSE;
+                       }
 
                        iter->begin = bmiter__loops_of_loop_begin;
                        iter->step =  bmiter__loops_of_loop_step;
                        iter->ldata = data;
                        break;
                case BM_LOOPS_OF_EDGE:
-                       if (!data)
+                       if (UNLIKELY(!data)) {
                                return FALSE;
+                       }
 
                        iter->begin = bmiter__loops_of_edge_begin;
                        iter->step =  bmiter__loops_of_edge_step;
@@ -174,7 +184,7 @@ BM_INLINE int BM_iter_init(BMIter *iter, BMesh *bm, const char itype, void *data
  */
 BM_INLINE void *BM_iter_new(BMIter *iter, BMesh *bm, const char itype, void *data)
 {
-       if (BM_iter_init(iter, bm, itype, data)) {
+       if (LIKELY(BM_iter_init(iter, bm, itype, data))) {
                return BM_iter_step(iter);
        }
        else {
index 14acc305e6465f2eec36f6be4b6c4f39c1f1427c..1f65ef2ce113bab59371f13a8c580e751be7fdce 100644 (file)
@@ -244,7 +244,7 @@ PyDoc_STRVAR(bpy_bm_is_valid_doc,
 );
 static PyObject *bpy_bm_is_valid_get(BPy_BMGeneric *self)
 {
-       return PyBool_FromLong(self->bm != NULL);
+       return PyBool_FromLong(BPY_BM_IS_VALID(self));
 }
 
 
@@ -2453,13 +2453,15 @@ PyObject *BPy_BMElem_CreatePyObject(BMesh *bm, BMHeader *ele)
 
 int bpy_bm_generic_valid_check(BPy_BMGeneric *self)
 {
-       if (self->bm) {
+       if (LIKELY(self->bm)) {
                return 0;
        }
-       PyErr_Format(PyExc_ReferenceError,
-                    "BMesh data of type %.200s has been removed",
-                    Py_TYPE(self)->tp_name);
-       return -1;
+       else {
+               PyErr_Format(PyExc_ReferenceError,
+                                        "BMesh data of type %.200s has been removed",
+                                        Py_TYPE(self)->tp_name);
+               return -1;
+       }
 }
 
 void bpy_bm_generic_invalidate(BPy_BMGeneric *self)
@@ -2513,7 +2515,7 @@ void *BPy_BMElem_PySeq_As_Array(BMesh **r_bm, PyObject *seq, Py_ssize_t min, Py_
                                             error_prefix, type->tp_name, Py_TYPE(item)->tp_name);
                                goto err_cleanup;
                        }
-                       else if (item->bm == NULL) {
+                       else if (!BPY_BM_IS_VALID(item)) {
                                PyErr_Format(PyExc_TypeError,
                                             "%s: %d %s has been removed",
                                             error_prefix, i, type->tp_name);
index 6d4444b46625f841dbc1e2969bf230835187b004..d7deb41c4e305e450345e7a3575d20463565dfb9 100644 (file)
@@ -137,8 +137,10 @@ void *BPy_BMElem_PySeq_As_Array(BMesh **r_bm, PyObject *seq, Py_ssize_t min, Py_
                                 const char do_unique_check, const char do_bm_check,
                                 const char *error_prefix);
 
-#define BPY_BM_CHECK_OBJ(obj) if (bpy_bm_generic_valid_check((BPy_BMGeneric *)obj) == -1) { return NULL; } (void)NULL
-#define BPY_BM_CHECK_INT(obj) if (bpy_bm_generic_valid_check((BPy_BMGeneric *)obj) == -1) { return -1; }   (void)NULL
+#define BPY_BM_CHECK_OBJ(obj) if (UNLIKELY(bpy_bm_generic_valid_check((BPy_BMGeneric *)obj) == -1)) { return NULL; } (void)0
+#define BPY_BM_CHECK_INT(obj) if (UNLIKELY(bpy_bm_generic_valid_check((BPy_BMGeneric *)obj) == -1)) { return -1; }   (void)0
+
+#define BPY_BM_IS_VALID(obj) (LIKELY((obj)->bm != NULL))
 
 #define BM_ITER_BPY_BM_SEQ(ele, iter, bpy_bmelemseq)                \
        BM_ITER(ele, iter, (bpy_bmelemseq)->bm, (bpy_bmelemseq)->itype, \
index c9ff034716d3568f8f8cbf1228e005a0d981002e..113367788b66c89b91da5272460ba4d9ddf6fd6a 100644 (file)
@@ -83,14 +83,14 @@ extern PyTypeObject pyrna_func_Type;
 #define BPy_PropertyRNA_Check(v)               (PyObject_TypeCheck(v, &pyrna_prop_Type))
 #define BPy_PropertyRNA_CheckExact(v)  (Py_TYPE(v) == &pyrna_prop_Type)
 
-#define PYRNA_STRUCT_CHECK_OBJ(obj) if(pyrna_struct_validity_check(obj) == -1) { return NULL; }
-#define PYRNA_STRUCT_CHECK_INT(obj) if(pyrna_struct_validity_check(obj) == -1) { return -1; }
+#define PYRNA_STRUCT_CHECK_OBJ(obj) if (UNLIKELY(pyrna_struct_validity_check(obj) == -1)) { return NULL; } (void)0
+#define PYRNA_STRUCT_CHECK_INT(obj) if (UNLIKELY(pyrna_struct_validity_check(obj) == -1)) { return -1;   } (void)0
 
-#define PYRNA_PROP_CHECK_OBJ(obj) if(pyrna_prop_validity_check(obj) == -1) { return NULL; }
-#define PYRNA_PROP_CHECK_INT(obj) if(pyrna_prop_validity_check(obj) == -1) { return -1; }
+#define PYRNA_PROP_CHECK_OBJ(obj) if (UNLIKELY(pyrna_prop_validity_check(obj) == -1)) { return NULL; } (void)0
+#define PYRNA_PROP_CHECK_INT(obj) if (UNLIKELY(pyrna_prop_validity_check(obj) == -1)) { return -1;   } (void)0
 
-#define PYRNA_STRUCT_IS_VALID(pysrna) (((BPy_StructRNA *)(pysrna))->ptr.type != NULL)
-#define PYRNA_PROP_IS_VALID(pysrna) (((BPy_PropertyRNA *)(pysrna))->ptr.type != NULL)
+#define PYRNA_STRUCT_IS_VALID(pysrna) (LIKELY(((BPy_StructRNA *)(pysrna))->ptr.type != NULL))
+#define PYRNA_PROP_IS_VALID(pysrna)   (LIKELY(((BPy_PropertyRNA *)(pysrna))->ptr.type != NULL))
 
 /* 'in_weakreflist' MUST be aligned */
 
index 2371ca7dc50a5f10f63c67a6065b038d40f3cda7..4b4d0ff3535d9bbb8fd5f10fab4bbb0e6768c094 100644 (file)
@@ -31,6 +31,7 @@
 
 #include "MEM_guardedalloc.h"
 
+#include "BLI_utildefines.h"
 #include "BLI_string.h"
 
 #include "DNA_scene_types.h"
index a9a9ba8643c99eb9678d2e55f4f7360d36f42da7..e6be81f88a338a0a007469ff1f3bb53484902dce 100644 (file)
@@ -304,8 +304,9 @@ int Mathutils_RegisterCallback(Mathutils_Callback *cb)
 int _BaseMathObject_ReadCallback(BaseMathObject *self)
 {
        Mathutils_Callback *cb = mathutils_callbacks[self->cb_type];
-       if (cb->get(self, self->cb_subtype) != -1)
+       if (LIKELY(cb->get(self, self->cb_subtype) != -1)) {
                return 0;
+       }
 
        if (!PyErr_Occurred()) {
                PyErr_Format(PyExc_RuntimeError,
@@ -318,8 +319,9 @@ int _BaseMathObject_ReadCallback(BaseMathObject *self)
 int _BaseMathObject_WriteCallback(BaseMathObject *self)
 {
        Mathutils_Callback *cb = mathutils_callbacks[self->cb_type];
-       if (cb->set(self, self->cb_subtype) != -1)
+       if (LIKELY(cb->set(self, self->cb_subtype) != -1)) {
                return 0;
+       }
 
        if (!PyErr_Occurred()) {
                PyErr_Format(PyExc_RuntimeError,
@@ -332,8 +334,9 @@ int _BaseMathObject_WriteCallback(BaseMathObject *self)
 int _BaseMathObject_ReadIndexCallback(BaseMathObject *self, int index)
 {
        Mathutils_Callback *cb = mathutils_callbacks[self->cb_type];
-       if (cb->get_index(self, self->cb_subtype, index) != -1)
+       if (LIKELY(cb->get_index(self, self->cb_subtype, index) != -1)) {
                return 0;
+       }
 
        if (!PyErr_Occurred()) {
                PyErr_Format(PyExc_RuntimeError,
@@ -346,8 +349,9 @@ int _BaseMathObject_ReadIndexCallback(BaseMathObject *self, int index)
 int _BaseMathObject_WriteIndexCallback(BaseMathObject *self, int index)
 {
        Mathutils_Callback *cb = mathutils_callbacks[self->cb_type];
-       if (cb->set_index(self, self->cb_subtype, index) != -1)
+       if (LIKELY(cb->set_index(self, self->cb_subtype, index) != -1)) {
                return 0;
+       }
 
        if (!PyErr_Occurred()) {
                PyErr_Format(PyExc_RuntimeError,