Skip to content

Commit f0a7b65

Browse files
committed
Refactor PythonPointTable
1 parent 799276a commit f0a7b65

File tree

2 files changed

+22
-40
lines changed

2 files changed

+22
-40
lines changed

pdal/StreamableExecutor.cpp

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,60 +48,46 @@ namespace python
4848
// PythonPointTable
4949

5050
PythonPointTable::PythonPointTable(point_count_t limit, int prefetch) :
51-
StreamPointTable(m_layout, limit), m_limit(limit), m_prefetch(prefetch),
51+
StreamPointTable(m_layout, limit), m_prefetch(prefetch),
5252
m_curArray(nullptr), m_dtype(nullptr)
5353
{}
5454

5555
PythonPointTable::~PythonPointTable()
56-
{
57-
py_destroy();
58-
}
59-
60-
void PythonPointTable::finalize()
61-
{
62-
BasePointTable::finalize();
63-
py_createDescriptor();
64-
m_curArray = py_createArray();
65-
}
66-
67-
void PythonPointTable::py_destroy()
6856
{
6957
auto gil = PyGILState_Ensure();
70-
7158
Py_XDECREF(m_dtype);
7259
Py_XDECREF(m_curArray);
73-
7460
PyGILState_Release(gil);
7561
}
7662

77-
PyArrayObject *PythonPointTable::py_createArray() const
63+
void PythonPointTable::finalize()
7864
{
79-
auto gil = PyGILState_Ensure();
80-
81-
npy_intp size = (npy_intp)m_limit;
82-
Py_INCREF(m_dtype);
83-
PyArrayObject *arr = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, m_dtype,
84-
1, &size, 0, nullptr, NPY_ARRAY_CARRAY, nullptr);
85-
86-
PyGILState_Release(gil);
87-
return arr;
88-
}
65+
BasePointTable::finalize();
8966

90-
void PythonPointTable::py_createDescriptor()
91-
{
67+
// create dtype
9268
auto gil = PyGILState_Ensure();
93-
9469
if (_import_array() < 0)
9570
std::cerr << "Could not import array!\n";
9671
PyObject *dtype_dict = py_buildNumpyDescriptor();
9772
if (PyArray_DescrConverter(dtype_dict, &m_dtype) == NPY_FAIL)
98-
m_dtype = nullptr;
73+
throw pdal_error("Unable to create numpy dtype");
9974
Py_XDECREF(dtype_dict);
75+
PyGILState_Release(gil);
76+
77+
py_createArray();
78+
}
10079

80+
void PythonPointTable::py_createArray()
81+
{
82+
auto gil = PyGILState_Ensure();
83+
npy_intp size = capacity();
84+
Py_INCREF(m_dtype);
85+
m_curArray = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, m_dtype,
86+
1, &size, 0, nullptr, NPY_ARRAY_CARRAY, nullptr);
10187
PyGILState_Release(gil);
10288
}
10389

104-
void PythonPointTable::py_resizeArray(int np)
90+
void PythonPointTable::py_resizeArray(point_count_t np)
10591
{
10692
npy_intp sizes[1];
10793
sizes[0] = np;
@@ -187,7 +173,7 @@ void PythonPointTable::reset()
187173
if (!skip(idx))
188174
np++;
189175

190-
if (np && np != m_limit)
176+
if (np && np != capacity())
191177
py_resizeArray(np);
192178

193179
// This will keep putting arrays on the list until done, whether or not the consumer
@@ -199,7 +185,7 @@ void PythonPointTable::reset()
199185
if (np)
200186
{
201187
m_arrays.push(m_curArray);
202-
m_curArray = py_createArray();
188+
py_createArray();
203189
m_producedCv.notify_one();
204190
}
205191
while (m_arrays.size() > m_prefetch)
@@ -236,7 +222,7 @@ PyArrayObject *PythonPointTable::fetchArray()
236222

237223
char *PythonPointTable::getPoint(PointId idx)
238224
{
239-
return (char *)PyArray_GETPTR1(m_curArray, (npy_intp)idx);
225+
return (char *)PyArray_GETPTR1(m_curArray, idx);
240226
}
241227

242228

pdal/StreamableExecutor.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,10 @@ class PythonPointTable : public StreamPointTable
6868

6969
private:
7070
// All functions starting with py_ call Python things that need the GIL locked.
71-
void py_destroy();
72-
PyArrayObject *py_createArray() const;
73-
void py_createDescriptor();
74-
void py_resizeArray(int np);
71+
void py_createArray();
72+
void py_resizeArray(point_count_t np);
7573
PyObject *py_buildNumpyDescriptor() const;
7674

77-
point_count_t m_limit;
7875
int m_prefetch;
7976
PointLayout m_layout;
8077
PyArrayObject *m_curArray;
@@ -94,7 +91,6 @@ class StreamableExecutor : public PipelineExecutor
9491
PyArrayObject* executeNext();
9592

9693
private:
97-
int m_prefetch;
9894
PythonPointTable m_table;
9995
std::unique_ptr<std::thread> m_thread;
10096
};

0 commit comments

Comments
 (0)