1
- # cython: profile=False
1
+ # cython: profile=True
2
2
3
3
from cpython cimport PyObject, Py_INCREF, PyList_Check, PyTuple_Check
4
4
@@ -32,6 +32,9 @@ PyDateTime_IMPORT
32
32
cdef extern from " Python.h" :
33
33
int PySlice_Check(object )
34
34
35
+ cdef size_t _INIT_VEC_CAP = 32
36
+ cdef size_t _USE_GIL = 100000
37
+
35
38
def list_to_object_array (list obj ):
36
39
'''
37
40
Convert list to object ndarray. Seriously can't believe I had to write this
@@ -50,8 +53,6 @@ def list_to_object_array(list obj):
50
53
return arr
51
54
52
55
53
- cdef size_t _INIT_VEC_CAP = 32
54
-
55
56
cdef class Vector:
56
57
57
58
cdef:
@@ -109,7 +110,7 @@ cdef class Int64Vector(Vector):
109
110
self .ao.resize(self .m)
110
111
self .data = < int64_t* > self .ao.data
111
112
112
- cdef inline void append (self , int64_t x) nogil:
113
+ cdef inline void append_nogil (self , int64_t x) nogil:
113
114
114
115
if self .needs_resize():
115
116
with gil:
@@ -118,6 +119,14 @@ cdef class Int64Vector(Vector):
118
119
self .data[self .n] = x
119
120
self .n += 1
120
121
122
+ cdef inline void append(self , int64_t x):
123
+
124
+ if self .needs_resize():
125
+ self .resize()
126
+
127
+ self .data[self .n] = x
128
+ self .n += 1
129
+
121
130
cdef class Float64Vector(Vector):
122
131
123
132
cdef:
@@ -364,7 +373,22 @@ cdef class Int64HashTable(HashTable):
364
373
365
374
labels = np.empty(n, dtype = np.int64)
366
375
367
- with nogil:
376
+ if n > _USE_GIL:
377
+ with nogil:
378
+ for i in range (n):
379
+ val = values[i]
380
+ k = kh_get_int64(self .table, val)
381
+ if k != self .table.n_buckets:
382
+ idx = self .table.vals[k]
383
+ labels[i] = idx
384
+ else :
385
+ k = kh_put_int64(self .table, val, & ret)
386
+ self .table.vals[k] = count
387
+ uniques.append_nogil(val)
388
+ labels[i] = count
389
+ count += 1
390
+
391
+ else :
368
392
for i in range (n):
369
393
val = values[i]
370
394
k = kh_get_int64(self .table, val)
@@ -409,7 +433,7 @@ cdef class Int64HashTable(HashTable):
409
433
else :
410
434
k = kh_put_int64(self .table, val, & ret)
411
435
self .table.vals[k] = count
412
- uniques.append (val)
436
+ uniques.append_nogil (val)
413
437
labels[i] = count
414
438
count += 1
415
439
@@ -433,7 +457,7 @@ cdef class Int64HashTable(HashTable):
433
457
k = kh_get_int64(self .table, val)
434
458
if k == self .table.n_buckets:
435
459
kh_put_int64(self .table, val, & ret)
436
- uniques.append (val)
460
+ uniques.append_nogil (val)
437
461
438
462
result = uniques.to_array()
439
463
0 commit comments