kuuko pushed a commit to branch master.

commit 21ec3e18cf85c4716faa1d8fac2036aec17b33e6
Author: Kai Huuhko <[email protected]>
Date:   Sat Mar 30 15:32:10 2013 +0000

    Change the class lookup code to C only using an Eina Hash table.
---
 efl/eo/efl.eo.pyx  | 42 +++++++++++++++++++++++++-----------------
 include/efl.eo.pxd |  4 ++--
 include/efl.pxd    |  8 ++++++++
 3 files changed, 35 insertions(+), 19 deletions(-)

diff --git a/efl/eo/efl.eo.pyx b/efl/eo/efl.eo.pyx
index 1c80c01..dace687 100644
--- a/efl/eo/efl.eo.pyx
+++ b/efl/eo/efl.eo.pyx
@@ -15,10 +15,12 @@
 # You should have received a copy of the GNU Lesser General Public License
 # along with this Python-EFL.  If not, see <http://www.gnu.org/licenses/>.
 
-from cpython cimport PyObject, Py_INCREF, Py_DECREF#, PyMem_Malloc, PyMem_Free
+from cpython cimport PyObject, Py_INCREF, Py_DECREF
 from libc.stdlib cimport malloc, free
 from libc.string cimport memcpy, strdup
-from efl cimport Eina_Bool, const_Eina_List, eina_list_append, const_void
+from efl cimport Eina_Bool, const_Eina_List, eina_list_append, const_void, \
+    Eina_Hash, eina_hash_string_superfast_new, eina_hash_add, eina_hash_del, \
+    eina_hash_find
 from efl.c_eo cimport Eo as cEo
 from efl.c_eo cimport eo_init, eo_shutdown, eo_del, eo_unref, eo_wref_add, 
eo_add, Eo_Class
 from efl.c_eo cimport eo_do, eo_class_name_get, eo_class_get
@@ -208,22 +210,27 @@ cdef void _METHOD_DEPRECATED(object self, char *message):
 
 
 """
-Object mapping is a dictionary into which object type names can be
+Object mapping is an Eina Hash table into which object type names can be
 registered. These can be used to find a bindings class for an object using
-the object_from_instance function.
-"""
-cdef dict object_mapping = dict()
+the object_from_instance function."""
+cdef Eina_Hash *object_mapping = eina_hash_string_superfast_new(NULL)
+
+
+cdef void _object_mapping_register(char *name, object cls) except *:
+    cdef void *value
 
+    #print("REGISTER: %s => %s" % (name, cls))
 
-cdef void _object_mapping_register(str name, object cls) except *:
-#    print("REGISTER: %s => %s" % (name, cls))
-    if name in object_mapping:
+    value = eina_hash_find(object_mapping, name)
+
+    if value == NULL:
+        eina_hash_add(object_mapping, name, <PyObject *>cls)
+    else:
         raise ValueError("Object type name '%s' already registered." % name)
-    object_mapping[name] = cls
 
 
-cdef void _object_mapping_unregister(str name):
-    object_mapping.pop(name)
+cdef void _object_mapping_unregister(char *name):
+    eina_hash_del(object_mapping, name, NULL)
 
 
 cdef object object_from_instance(cEo *obj):
@@ -232,27 +239,28 @@ cdef object object_from_instance(cEo *obj):
         void *data
         Eo o
         const_char *cls_name
-        object cls
+        type cls
 
     if obj == NULL:
         return None
 
     eo_do(obj, eo_base_data_get("python-eo", &data))
     if data != NULL:
-#         print("Found: %s" % Eo.__repr__(<Eo>data))
+        #print("Found: %s" % Eo.__repr__(<Eo>data))
         return <Eo>data
 
     cls_name = eo_class_name_get(eo_class_get(obj))
     if cls_name == NULL:
         raise ValueError("Eo object %#x does not have a type!" % <long>obj)
-#     print("Class name: %s" % cls_name)
+    #print("Class name: %s" % cls_name)
+
+    cls = <type>eina_hash_find(object_mapping, cls_name)
 
-    cls = object_mapping.get(cls_name, None)
     if cls is None:
         raise ValueError("Eo object %#x of type %s does not have a mapping!" %
                          (<long>obj, cls_name))
 
-#     print "MAPPING OBJECT:", cls_name, "=>", cls
+    #print "MAPPING OBJECT:", cls_name, "=>", cls
     o = cls.__new__(cls)
     o._set_obj(obj)
     return o
diff --git a/include/efl.eo.pxd b/include/efl.eo.pxd
index 41dbc9a..75f7ee8 100644
--- a/include/efl.eo.pxd
+++ b/include/efl.eo.pxd
@@ -32,8 +32,8 @@ cdef class Eo(object):
 cdef int PY_REFCOUNT(object o)
 
 cdef object object_from_instance(cEo *obj)
-cdef void _object_mapping_register(str name, object cls) except *
-cdef void _object_mapping_unregister(str name)
+cdef void _object_mapping_register(char *name, object cls) except *
+cdef void _object_mapping_unregister(char *name)
 
 cdef unicode _touni(char* s)
 cdef char* _fruni(s)
diff --git a/include/efl.pxd b/include/efl.pxd
index 12deb4d..6b112f2 100644
--- a/include/efl.pxd
+++ b/include/efl.pxd
@@ -76,6 +76,8 @@ cdef extern from "Eina.h":
         void      *accounting
     ctypedef Eina_List const_Eina_List "const Eina_List"
 
+    ctypedef struct Eina_Hash
+
     ctypedef struct Eina_Hash_Tuple:
         void *key
         void *data
@@ -91,6 +93,7 @@ cdef extern from "Eina.h":
     # Other typedefs
     #
     ctypedef int (*Eina_Compare_Cb)(const_void *data1, const_void *data2)
+    ctypedef void (*Eina_Free_Cb)(void *data)
 
     ####################################################################
     # Functions
@@ -144,3 +147,8 @@ cdef extern from "Eina.h":
     Eina_List *eina_list_prev(Eina_List *list)
     void *eina_list_data_get(Eina_List *list)
     unsigned int eina_list_count(Eina_List *list)
+
+    Eina_Hash *eina_hash_string_superfast_new(Eina_Free_Cb data_free_cb)
+    Eina_Bool  eina_hash_add(Eina_Hash *hash, const_void *key, const_void 
*data)
+    Eina_Bool eina_hash_del(Eina_Hash  *hash, const_void *key, const_void 
*data)
+    void *eina_hash_find(Eina_Hash *hash, const_void *key)

-- 

------------------------------------------------------------------------------
Own the Future-Intel(R) Level Up Game Demo Contest 2013
Rise to greatness in Intel's independent game demo contest. Compete 
for recognition, cash, and the chance to get your game on Steam. 
$5K grand prize plus 10 genre and skill prizes. Submit your demo 
by 6/6/13. http://altfarm.mediaplex.com/ad/ck/12124-176961-30367-2

Reply via email to