Additional shortcut to QgsAttributes creation from Python

Avoid constructing a bunch of default QVariant objects which
we will just replace immediately
This commit is contained in:
Nyall Dawson 2024-05-22 12:19:13 +10:00
parent 280cd84652
commit 6185675c93
3 changed files with 21 additions and 9 deletions

View File

@ -70,9 +70,12 @@ typedef QVector<QVariant> QgsAttributes;
return 1;
}
QgsAttributes *qv = new QgsAttributes;
Py_ssize_t listSize = PyList_GET_SIZE( sipPy );
qv->resize( listSize );
// Initialize attributes to null. This has two motivations:
// 1. It speeds up the QVector construction, as otherwise we are creating n default QVariant objects (default QVariant constructor is not free!)
// 2. It lets us shortcut in the loop below when a Py_None is encountered in the list
const QVariant nullVariant( QVariant::Int );
QgsAttributes *qv = new QgsAttributes( listSize, nullVariant );
QVariant *outData = qv->data();
for ( Py_ssize_t i = 0; i < listSize; ++i )
@ -80,7 +83,8 @@ typedef QVector<QVariant> QgsAttributes;
PyObject *obj = PyList_GET_ITEM( sipPy, i );
if ( obj == Py_None )
{
*outData++ = QVariant( QVariant::Int );
// outData was already initialized to null values
*outData++;
}
else if ( PyBool_Check( obj ) )
{

View File

@ -70,9 +70,12 @@ typedef QVector<QVariant> QgsAttributes;
return 1;
}
QgsAttributes *qv = new QgsAttributes;
SIP_SSIZE_T listSize = PyList_GET_SIZE( sipPy );
qv->resize( listSize );
// Initialize attributes to null. This has two motivations:
// 1. It speeds up the QVector construction, as otherwise we are creating n default QVariant objects (default QVariant constructor is not free!)
// 2. It lets us shortcut in the loop below when a Py_None is encountered in the list
const QVariant nullVariant( QVariant::Int );
QgsAttributes *qv = new QgsAttributes( listSize, nullVariant );
QVariant *outData = qv->data();
for ( SIP_SSIZE_T i = 0; i < listSize; ++i )
@ -80,7 +83,8 @@ typedef QVector<QVariant> QgsAttributes;
PyObject *obj = PyList_GET_ITEM( sipPy, i );
if ( obj == Py_None )
{
*outData++ = QVariant( QVariant::Int );
// outData was already initialized to null values
*outData++;
}
else if ( PyBool_Check( obj ) )
{

View File

@ -190,9 +190,12 @@ typedef QVector<QVariant> QgsAttributes;
return 1;
}
QgsAttributes *qv = new QgsAttributes;
SIP_SSIZE_T listSize = PyList_GET_SIZE( sipPy );
qv->resize( listSize );
// Initialize attributes to null. This has two motivations:
// 1. It speeds up the QVector construction, as otherwise we are creating n default QVariant objects (default QVariant constructor is not free!)
// 2. It lets us shortcut in the loop below when a Py_None is encountered in the list
const QVariant nullVariant( QVariant::Int );
QgsAttributes *qv = new QgsAttributes( listSize, nullVariant );
QVariant *outData = qv->data();
for ( SIP_SSIZE_T i = 0; i < listSize; ++i )
@ -200,7 +203,8 @@ typedef QVector<QVariant> QgsAttributes;
PyObject *obj = PyList_GET_ITEM( sipPy, i );
if ( obj == Py_None )
{
*outData++ = QVariant( QVariant::Int );
// outData was already initialized to null values
*outData++;
}
else if ( PyBool_Check( obj ) )
{