Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pitrou committed Nov 13, 2023
1 parent aa9f8ec commit f5796e4
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 15 deletions.
5 changes: 2 additions & 3 deletions python/pyarrow/_csv.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ from collections.abc import Mapping

from pyarrow.includes.common cimport *
from pyarrow.includes.libarrow cimport *
from pyarrow.includes.libarrow_python cimport (MakeInvalidRowHandler,
PyInvalidRowCallback)
from pyarrow.includes.libarrow_python cimport *
from pyarrow.lib cimport (check_status, Field, MemoryPool, Schema,
RecordBatchReader, ensure_type,
maybe_unbox_memory_pool, get_input_stream,
Expand Down Expand Up @@ -1251,7 +1250,7 @@ def read_csv(input_file, read_options=None, parse_options=None,
CCSVParseOptions c_parse_options
CCSVConvertOptions c_convert_options
CIOContext io_context
shared_ptr[CCSVReader] reader
SharedPtrNoGIL[CCSVReader] reader
shared_ptr[CTable] table

_get_reader(input_file, read_options, &stream)
Expand Down
8 changes: 4 additions & 4 deletions python/pyarrow/_dataset.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ cdef CFileSource _make_file_source(object file, FileSystem filesystem=*)
cdef class DatasetFactory(_Weakrefable):

cdef:
shared_ptr[CDatasetFactory] wrapped
SharedPtrNoGIL[CDatasetFactory] wrapped
CDatasetFactory* factory

cdef init(self, const shared_ptr[CDatasetFactory]& sp)
Expand All @@ -45,7 +45,7 @@ cdef class DatasetFactory(_Weakrefable):
cdef class Dataset(_Weakrefable):

cdef:
shared_ptr[CDataset] wrapped
SharedPtrNoGIL[CDataset] wrapped
CDataset* dataset
public dict _scan_options

Expand All @@ -59,7 +59,7 @@ cdef class Dataset(_Weakrefable):

cdef class Scanner(_Weakrefable):
cdef:
shared_ptr[CScanner] wrapped
SharedPtrNoGIL[CScanner] wrapped
CScanner* scanner

cdef void init(self, const shared_ptr[CScanner]& sp)
Expand Down Expand Up @@ -122,7 +122,7 @@ cdef class FileWriteOptions(_Weakrefable):
cdef class Fragment(_Weakrefable):

cdef:
shared_ptr[CFragment] wrapped
SharedPtrNoGIL[CFragment] wrapped
CFragment* fragment

cdef void init(self, const shared_ptr[CFragment]& sp)
Expand Down
4 changes: 2 additions & 2 deletions python/pyarrow/_dataset.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3227,7 +3227,7 @@ cdef class RecordBatchIterator(_Weakrefable):
object iterator_owner
# Iterator is a non-POD type and Cython uses offsetof, leading
# to a compiler warning unless wrapped like so
shared_ptr[CRecordBatchIterator] iterator
SharedPtrNoGIL[CRecordBatchIterator] iterator

def __init__(self):
_forbid_instantiation(self.__class__, subclasses_instead=False)
Expand Down Expand Up @@ -3273,7 +3273,7 @@ cdef class TaggedRecordBatchIterator(_Weakrefable):
"""An iterator over a sequence of record batches with fragments."""
cdef:
object iterator_owner
shared_ptr[CTaggedRecordBatchIterator] iterator
SharedPtrNoGIL[CTaggedRecordBatchIterator] iterator

def __init__(self):
_forbid_instantiation(self.__class__, subclasses_instead=False)
Expand Down
6 changes: 3 additions & 3 deletions python/pyarrow/_parquet.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import warnings
from cython.operator cimport dereference as deref
from pyarrow.includes.common cimport *
from pyarrow.includes.libarrow cimport *
from pyarrow.includes.libarrow_python cimport *
from pyarrow.lib cimport (_Weakrefable, Buffer, Schema,
check_status,
MemoryPool, maybe_unbox_memory_pool,
Expand Down Expand Up @@ -1165,7 +1166,7 @@ cdef class ParquetReader(_Weakrefable):
cdef:
object source
CMemoryPool* pool
unique_ptr[FileReader] reader
UniquePtrNoGIL[FileReader] reader
FileMetaData _metadata
shared_ptr[CRandomAccessFile] rd_handle

Expand Down Expand Up @@ -1334,7 +1335,7 @@ cdef class ParquetReader(_Weakrefable):
vector[int] c_row_groups
vector[int] c_column_indices
shared_ptr[CRecordBatch] record_batch
unique_ptr[CRecordBatchReader] recordbatchreader
UniquePtrNoGIL[CRecordBatchReader] recordbatchreader

self.set_batch_size(batch_size)

Expand Down Expand Up @@ -1366,7 +1367,6 @@ cdef class ParquetReader(_Weakrefable):
check_status(
recordbatchreader.get().ReadNext(&record_batch)
)

if record_batch.get() == NULL:
break

Expand Down
7 changes: 7 additions & 0 deletions python/pyarrow/includes/libarrow_python.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ cdef extern from "arrow/python/common.h" namespace "arrow::py":
void RestorePyError(const CStatus& status) except *


cdef extern from "arrow/python/common.h" namespace "arrow::py" nogil:
cdef cppclass SharedPtrNoGIL[T](shared_ptr[T]):
pass
cdef cppclass UniquePtrNoGIL[T,DELETER=*](unique_ptr[T,DELETER]):
pass


cdef extern from "arrow/python/inference.h" namespace "arrow::py":
c_bool IsPyBool(object o)
c_bool IsPyInt(object o)
Expand Down
2 changes: 1 addition & 1 deletion python/pyarrow/ipc.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ cdef _wrap_record_batch_with_metadata(CRecordBatchWithMetadata c):

cdef class _RecordBatchFileReader(_Weakrefable):
cdef:
shared_ptr[CRecordBatchFileReader] reader
SharedPtrNoGIL[CRecordBatchFileReader] reader
shared_ptr[CRandomAccessFile] file
CIpcReadOptions options

Expand Down
4 changes: 2 additions & 2 deletions python/pyarrow/lib.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,12 @@ cdef class CompressedOutputStream(NativeFile):

cdef class _CRecordBatchWriter(_Weakrefable):
cdef:
shared_ptr[CRecordBatchWriter] writer
SharedPtrNoGIL[CRecordBatchWriter] writer


cdef class RecordBatchReader(_Weakrefable):
cdef:
shared_ptr[CRecordBatchReader] reader
SharedPtrNoGIL[CRecordBatchReader] reader


cdef class Codec(_Weakrefable):
Expand Down
44 changes: 44 additions & 0 deletions python/pyarrow/src/arrow/python/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,50 @@ class ARROW_PYTHON_EXPORT OwnedRefNoGIL : public OwnedRef {
}
};

template <typename T>
class SharedPtrNoGIL : public std::shared_ptr<T> {
using Base = std::shared_ptr<T>;
public:
template <typename... Args>
SharedPtrNoGIL(Args&&... args)
: Base(std::forward<Args>(args)...) {}

~SharedPtrNoGIL() {
if (Py_IsInitialized() && PyGILState_Check()) {
PyReleaseGIL release;
Base::reset();
}
}

template <typename V>
SharedPtrNoGIL& operator=(V&& v) {
Base::operator=(std::forward<V>(v));
return *this;
}
};

template <typename T>
class UniquePtrNoGIL : public std::unique_ptr<T> {
using Base = std::unique_ptr<T>;
public:
template <typename... Args>
UniquePtrNoGIL(Args&&... args)
: Base(std::forward<Args>(args)...) {}

~UniquePtrNoGIL() {
if (Py_IsInitialized() && PyGILState_Check()) {
PyReleaseGIL release;
Base::reset();
}
}

template <typename V>
UniquePtrNoGIL& operator=(V&& v) {
Base::operator=(std::forward<V>(v));
return *this;
}
};

template <typename Fn>
struct BoundFunction;

Expand Down

0 comments on commit f5796e4

Please sign in to comment.