@@ -42,6 +42,7 @@ typedef struct TableElement{
4242# define LOAD 0.9
4343# define SCAN 16
4444
45+ const static size_t UCS4_SIZE = sizeof (Py_UCS4 );
4546
4647typedef enum KeysArrayType {
4748 KAT_LIST = 0 , // must be falsy
@@ -790,8 +791,8 @@ lookup_hash_unicode(
790791 Py_ssize_t table_pos = hash & mask ;
791792
792793 PyArrayObject * a = (PyArrayObject * )self -> keys ;
793- // REVIEW: is this a new descr reference?
794- Py_ssize_t dt_size = PyArray_DESCR ( a ) -> elsize / sizeof ( Py_UCS4 ) ;
794+ Py_ssize_t dt_size = PyArray_DESCR ( a ) -> elsize / UCS4_SIZE ;
795+ Py_ssize_t cmp_bytes = Py_MIN ( key_size , dt_size ) * UCS4_SIZE ;
795796
796797 Py_hash_t h = 0 ;
797798 Py_UCS4 * p_start = NULL ;
@@ -808,7 +809,7 @@ lookup_hash_unicode(
808809 }
809810 p_start = (Py_UCS4 * )PyArray_GETPTR1 (a , table [table_pos ].keys_pos );
810811 // memcmp returns 0 on match
811- if (!memcmp (p_start , key , Py_MIN ( key_size , dt_size ) )) {
812+ if (!memcmp (p_start , key , cmp_bytes )) {
812813 return table_pos ;
813814 }
814815 table_pos ++ ;
@@ -833,6 +834,7 @@ lookup_hash_string(
833834
834835 PyArrayObject * a = (PyArrayObject * )self -> keys ;
835836 Py_ssize_t dt_size = PyArray_DESCR (a )-> elsize / sizeof (char );
837+ Py_ssize_t cmp_bytes = Py_MIN (key_size , dt_size );
836838
837839 Py_hash_t h = 0 ;
838840 char * p_start = NULL ;
@@ -849,7 +851,7 @@ lookup_hash_string(
849851 }
850852 p_start = (char * )PyArray_GETPTR1 (a , table [table_pos ].keys_pos );
851853 // memcmp returns 0 on match
852- if (!memcmp (p_start , key , Py_MIN ( key_size , dt_size ) )) {
854+ if (!memcmp (p_start , key , cmp_bytes )) {
853855 return table_pos ;
854856 }
855857 table_pos ++ ;
@@ -1110,7 +1112,7 @@ lookup_unicode(FAMObject *self, PyObject* key) {
11101112 return -1 ;
11111113 }
11121114 PyArrayObject * a = (PyArrayObject * )self -> keys ;
1113- Py_ssize_t dt_size = PyArray_DESCR (a )-> elsize / sizeof ( Py_UCS4 ) ;
1115+ Py_ssize_t dt_size = PyArray_DESCR (a )-> elsize / UCS4_SIZE ;
11141116 // if the key_size is greater than the dtype size of the array, we know there cannot be a match
11151117 Py_ssize_t k_size = PyUnicode_GetLength (key );
11161118 if (k_size > dt_size ) {
@@ -1435,8 +1437,8 @@ copy_to_new(PyTypeObject *cls, FAMObject *self, FAMObject *new)
14351437 new -> key_buffer = NULL ;
14361438 if (new -> keys_array_type == KAT_UNICODE ) {
14371439 PyArrayObject * a = (PyArrayObject * )new -> keys ;
1438- Py_ssize_t dt_size = PyArray_DESCR (a )-> elsize / sizeof ( Py_UCS4 ) ;
1439- new -> key_buffer = (Py_UCS4 * )PyMem_Malloc ((dt_size + 1 ) * sizeof ( Py_UCS4 ) );
1440+ Py_ssize_t dt_size = PyArray_DESCR (a )-> elsize / UCS4_SIZE ;
1441+ new -> key_buffer = (Py_UCS4 * )PyMem_Malloc ((dt_size + 1 ) * UCS4_SIZE );
14401442 }
14411443
14421444 Py_ssize_t table_size_alloc = new -> table_size + SCAN - 1 ;
@@ -1922,8 +1924,8 @@ fam_init(PyObject *self, PyObject *args, PyObject *kwargs)
19221924 break ;
19231925 case KAT_UNICODE : {
19241926 // Over allocate buffer by 1 so there is room for null at end. This buffer is only used in lookup();
1925- Py_ssize_t dt_size = PyArray_DESCR (a )-> elsize / sizeof ( Py_UCS4 ) ;
1926- fam -> key_buffer = (Py_UCS4 * )PyMem_Malloc ((dt_size + 1 ) * sizeof ( Py_UCS4 ) );
1927+ Py_ssize_t dt_size = PyArray_DESCR (a )-> elsize / UCS4_SIZE ;
1928+ fam -> key_buffer = (Py_UCS4 * )PyMem_Malloc ((dt_size + 1 ) * UCS4_SIZE );
19271929 INSERT_FLEXIBLE (Py_UCS4 , insert_unicode , ucs4_get_end_p );
19281930 break ;
19291931 }
0 commit comments