@@ -397,36 +397,32 @@ AK_TPS_Resolve(AK_TypeParserState previous, AK_TypeParserState new) {
397
397
PyArray_Descr *
398
398
AK_TPS_ToDtype (AK_TypeParserState state ) {
399
399
PyArray_Descr * dtype = NULL ;
400
- PyArray_Descr * dtype_final ;
401
400
402
401
switch (state ) {
403
402
case TPS_UNKNOWN :
404
- dtype = PyArray_DescrFromType (NPY_UNICODE );
403
+ dtype = PyArray_DescrNewFromType (NPY_UNICODE );
405
404
break ;
406
405
case TPS_EMPTY : // all empty defaults to string
407
- dtype = PyArray_DescrFromType (NPY_UNICODE );
406
+ dtype = PyArray_DescrNewFromType (NPY_UNICODE );
408
407
break ;
409
408
case TPS_STRING :
410
- dtype = PyArray_DescrFromType (NPY_UNICODE );
409
+ dtype = PyArray_DescrNewFromType (NPY_UNICODE );
411
410
break ;
412
411
case TPS_BOOL :
413
- dtype = PyArray_DescrFromType (NPY_BOOL );
412
+ dtype = PyArray_DescrNewFromType (NPY_BOOL );
414
413
break ;
415
414
case TPS_INT :
416
- dtype = PyArray_DescrFromType (NPY_INT64 );
415
+ dtype = PyArray_DescrNewFromType (NPY_INT64 );
417
416
break ;
418
417
case TPS_FLOAT :
419
- dtype = PyArray_DescrFromType (NPY_FLOAT64 );
418
+ dtype = PyArray_DescrNewFromType (NPY_FLOAT64 );
420
419
break ;
421
420
case TPS_COMPLEX :
422
- dtype = PyArray_DescrFromType (NPY_COMPLEX128 );
421
+ dtype = PyArray_DescrNewFromType (NPY_COMPLEX128 );
423
422
break ;
424
423
}
425
424
if (dtype == NULL ) return NULL ; // assume error is set by PyArray_DescrFromType
426
- // get a fresh instance as we might mutate
427
- dtype_final = PyArray_DescrNew (dtype );
428
- Py_DECREF (dtype );
429
- return dtype_final ;
425
+ return dtype ;
430
426
}
431
427
432
428
//------------------------------------------------------------------------------
@@ -1297,7 +1293,7 @@ AK_CPL_Free(AK_CodePointLine* cpl)
1297
1293
static inline int
1298
1294
AK_CPL_resize_buffer (AK_CodePointLine * cpl , Py_ssize_t count )
1299
1295
{
1300
- if (( cpl -> buffer_count + count ) >= cpl -> buffer_capacity ) {
1296
+ if (AK_UNLIKELY (( cpl -> buffer_count + count ) >= cpl -> buffer_capacity ) ) {
1301
1297
// realloc
1302
1298
cpl -> buffer_capacity *= 2 ; // needs to be max of this or element_length
1303
1299
cpl -> buffer = PyMem_Realloc (cpl -> buffer ,
@@ -1313,7 +1309,7 @@ static inline int
1313
1309
AK_CPL_resize_offsets (AK_CodePointLine * cpl )
1314
1310
{
1315
1311
// increment by at most one, so only need to check if equal
1316
- if (cpl -> offsets_count == cpl -> offsets_capacity ) {
1312
+ if (AK_UNLIKELY ( cpl -> offsets_count == cpl -> offsets_capacity ) ) {
1317
1313
// realloc
1318
1314
cpl -> offsets_capacity *= 2 ;
1319
1315
cpl -> offsets = PyMem_Realloc (cpl -> offsets ,
@@ -1923,29 +1919,21 @@ AK_CPL_to_array_bytes(AK_CodePointLine* cpl, PyArray_Descr* dtype)
1923
1919
return array ;
1924
1920
}
1925
1921
1926
- // If we cannot direclty convert bytes to values, create a bytes array and then use PyArray_CastToType to use numpy to interpet it as a new a array. This forces
1922
+ // If we cannot direclty convert bytes to values, create a bytes array and then use PyArray_CastToType to use numpy to interpet it as a new a array.
1927
1923
static inline PyObject *
1928
1924
AK_CPL_to_array_via_cast (AK_CodePointLine * cpl , PyArray_Descr * dtype )
1929
1925
{
1930
- // we cannot use this dtype in array construction as it will mutate a global
1931
- PyArray_Descr * dtype_bytes_proto = PyArray_DescrFromType (NPY_STRING );
1932
- if (dtype_bytes_proto == NULL ) {
1933
- Py_DECREF (dtype );
1934
- return NULL ;
1935
- }
1936
- PyArray_Descr * dtype_bytes = PyArray_DescrNew (dtype_bytes_proto );
1937
- Py_DECREF (dtype_bytes_proto );
1926
+ PyArray_Descr * dtype_bytes = PyArray_DescrNewFromType (NPY_STRING );
1938
1927
if (dtype_bytes == NULL ) {
1939
1928
Py_DECREF (dtype );
1940
1929
return NULL ;
1941
1930
}
1942
1931
PyObject * array_bytes = AK_CPL_to_array_bytes (cpl , dtype_bytes );
1943
1932
if (array_bytes == NULL ) {
1944
1933
Py_DECREF (dtype );
1945
- Py_DECREF ( dtype_bytes ); // was not stolen if array creation failed
1934
+ // dtype_bytes stolen even if array creation failed
1946
1935
return NULL ;
1947
1936
}
1948
-
1949
1937
PyObject * array = PyArray_CastToType ((PyArrayObject * )array_bytes , dtype , 0 );
1950
1938
Py_DECREF (array_bytes );
1951
1939
if (array == NULL ) {
@@ -2096,15 +2084,15 @@ AK_CPG_resize(AK_CodePointGrid* cpg, Py_ssize_t line)
2096
2084
Py_ssize_t lines_count = cpg -> lines_count ;
2097
2085
if (line < lines_count ) return 0 ; // most common scenario
2098
2086
2099
- if (line >= cpg -> lines_capacity ) {
2087
+ if (AK_UNLIKELY ( line >= cpg -> lines_capacity ) ) {
2100
2088
cpg -> lines_capacity *= 2 ;
2101
2089
// NOTE: we assume this only copies the pointers, not the data in the CPLs
2102
2090
cpg -> lines = PyMem_Realloc (cpg -> lines ,
2103
2091
sizeof (AK_CodePointLine * ) * cpg -> lines_capacity );
2104
2092
if (cpg -> lines == NULL ) return -1 ;
2105
2093
}
2106
2094
// Create the new CPL; first check if we need to set type_parse by calling into the dtypes function. For now we assume sequential growth, so should only check if equal
2107
- if (line >= lines_count ) {
2095
+ if (AK_UNLIKELY ( line >= lines_count ) ) {
2108
2096
// determine if we need to parse types
2109
2097
bool type_parse = false;
2110
2098
if (cpg -> dtypes == NULL ) {
@@ -3199,29 +3187,20 @@ dtype_from_element(PyObject *Py_UNUSED(m), PyObject *arg)
3199
3187
{
3200
3188
// -------------------------------------------------------------------------
3201
3189
// 1. Handle fast, exact type checks first.
3202
-
3203
- // None
3204
3190
if (arg == Py_None ) {
3205
3191
return (PyObject * )PyArray_DescrFromType (NPY_OBJECT );
3206
3192
}
3207
-
3208
- // Float
3209
3193
if (PyFloat_CheckExact (arg )) {
3210
- return (PyObject * )PyArray_DescrFromType (NPY_DOUBLE );
3194
+ return (PyObject * )PyArray_DescrFromType (NPY_FLOAT64 );
3211
3195
}
3212
-
3213
- // Integers
3214
3196
if (PyLong_CheckExact (arg )) {
3215
- return (PyObject * )PyArray_DescrFromType (NPY_LONG );
3197
+ return (PyObject * )PyArray_DescrFromType (NPY_INT64 );
3216
3198
}
3217
-
3218
- // Bool
3219
3199
if (PyBool_Check (arg )) {
3220
3200
return (PyObject * )PyArray_DescrFromType (NPY_BOOL );
3221
3201
}
3222
3202
3223
3203
PyObject * dtype = NULL ;
3224
-
3225
3204
// String
3226
3205
if (PyUnicode_CheckExact (arg )) {
3227
3206
PyArray_Descr * descr = PyArray_DescrFromType (NPY_UNICODE );
@@ -3230,27 +3209,23 @@ dtype_from_element(PyObject *Py_UNUSED(m), PyObject *arg)
3230
3209
Py_DECREF (descr );
3231
3210
return dtype ;
3232
3211
}
3233
-
3234
3212
// Bytes
3235
3213
if (PyBytes_CheckExact (arg )) {
3236
3214
PyArray_Descr * descr = PyArray_DescrFromType (NPY_STRING );
3237
3215
if (descr == NULL ) return NULL ;
3238
-
3239
3216
dtype = (PyObject * )PyArray_DescrFromObject (arg , descr );
3240
3217
Py_DECREF (descr );
3241
3218
return dtype ;
3242
3219
}
3243
3220
3244
3221
// -------------------------------------------------------------------------
3245
3222
// 2. Construct dtype (slightly more complicated)
3246
-
3247
3223
// Already known
3248
3224
dtype = PyObject_GetAttrString (arg , "dtype" );
3249
3225
if (dtype ) {
3250
3226
return dtype ;
3251
3227
}
3252
3228
PyErr_Clear ();
3253
-
3254
3229
// -------------------------------------------------------------------------
3255
3230
// 3. Handles everything else.
3256
3231
return (PyObject * )PyArray_DescrFromType (NPY_OBJECT );
@@ -3956,4 +3931,5 @@ PyInit__arraykit(void)
3956
3931
return NULL ;
3957
3932
}
3958
3933
return m ;
3959
- }
3934
+ }
3935
+
0 commit comments