| [ Web Proxy ] |
| Viewing: https://arrow.apache.org/docs/python/generated/../../cpp/api/../../python/memory.html | [Back] [Original] |
This section will introduce you to the major concepts in PyArrows memory management and IO systems:
Buffers
Memory pools
File-like and stream-like objects
The Buffer object wraps the C++ arrow::Buffer type
which is the primary tool for memory management in Apache Arrow in C++. It permits
higher-level array classes to safely interact with memory which they may or may
not own. arrow::Buffer can be zero-copy sliced to permit Buffers to cheaply
reference other Buffers, while preserving memory lifetime and clean
parent-child relationships.
There are many implementations of arrow::Buffer, but they all provide a
standard interface: a data pointer and length. This is similar to Pythons
built-in buffer protocol and memoryview objects.
A Buffer can be created from any Python object implementing
the buffer protocol by calling the py_buffer() function. Lets consider
a bytes object:
>>> import pyarrow as pa
>>> data = b'abcdefghijklmnopqrstuvwxyz'
>>> buf = pa.py_buffer(data)
>>> buf
<pyarrow.Buffer address=... size=26 is_cpu=True is_mutable=False>
>>> buf.size
26
Creating a Buffer in this way does not allocate any memory; it is a zero-copy
view on the memory exported from the data bytes object.
External memory, under the form of a raw pointer and size, can also be
referenced using the foreign_buffer() function.
Buffers can be used in circumstances where a Python buffer or memoryview is required, and such conversions are zero-copy:
>>> memoryview(buf)
<memory at ...>
The Buffers to_pybytes() method converts the Buffers data to a
Python bytestring (thus making a copy of the data):
>>> buf.to_pybytes()
b'abcdefghijklmnopqrstuvwxyz'
All memory allocations and deallocations (like malloc and free in C)
are tracked in an instance of MemoryPool. This means that we can
then precisely track amount of memory that has been allocated:
>>> pa.total_allocated_bytes()
0
Lets allocate a resizable Buffer from the default pool:
>>> buf = pa.allocate_buffer(1024, resizable=True)
>>> pa.total_allocated_bytes()
1024
>>> buf.resize(2048)
>>> pa.total_allocated_bytes()
2048
The default allocator requests memory in a minimum increment of 64 bytes. If the buffer is garbage-collected, all of the memory is freed:
>>> buf = None
>>> pa.total_allocated_bytes()
0
Besides the default built-in memory pool, there may be additional memory pools to choose from (such as jemalloc) depending on how Arrow was built. One can get the backend name for a memory pool:
>>> pa.default_memory_pool().backend_name
'mimalloc'
See also
See also
On-GPU buffers using Arrows optional CUDA integration.
The Arrow C++ libraries have several abstract interfaces for different kinds of IO objects:
Read-only streams
Read-only files supporting random access
Write-only streams
Write-only files supporting random access
File supporting reads, writes, and random access
In the interest of making these objects behave more like Pythons built-in
file objects, we have defined a NativeFile base class
which implements the same API as regular Python file objects.
NativeFile has some important features which make it
preferable to using Python files with PyArrow where possible:
Other Arrow classes can access the internal C++ IO objects natively, and do not need to acquire the Python GIL
Native C++ IO may be able to do zero-copy IO, such as with memory maps
There are several kinds of NativeFile options available:
OSFile, a native file that uses your operating systems
file descriptors
MemoryMappedFile, for reading (zero-copy) and writing with
memory maps
BufferReader, for reading Buffer objects
as a file
BufferOutputStream, for writing data in-memory, producing a
Buffer at the end
FixedSizeBufferWriter, for writing data into an already
allocated Buffer
HdfsFile, for reading and writing data to the Hadoop Filesystem
PythonFile, for interfacing with Python file objects in C++
CompressedInputStream and
CompressedOutputStream, for on-the-fly compression or
decompression to/from another stream
There are also high-level APIs to make instantiating common kinds of streams easier.
The input_stream() function allows creating a readable
NativeFile from various kinds of sources.
If passed a Buffer or a memoryview object, a
BufferReader will be returned:
>>> buf = memoryview(b"some data") >>> stream = pa.input_stream(buf) >>> stream.read(4) b'some'
If passed a string or file path, it will open the given file on disk
for reading, creating a OSFile. Optionally, the file
can be compressed: if its filename ends with a recognized extension
such as .gz, its contents will automatically be decompressed on
reading.
>>> import gzip
>>> with gzip.open('example.gz', 'wb') as f:
... f.write(b'some data\n' * 3)
30
>>> stream = pa.input_stream('example.gz')
>>> stream.read()
b'some data\nsome data\nsome data\n'
If passed a Python file object, it will wrapped in a PythonFile
such that the Arrow C++ libraries can read data from it (at the expense
of a slight overhead).
output_stream() is the equivalent function for output streams
and allows creating a writable NativeFile. It has the same
features as explained above for input_stream(), such as being
able to write to buffers or do on-the-fly compression.
>>> with pa.output_stream('example1.dat') as stream:
... stream.write(b'some data')
9
>>> with open('example1.dat', 'rb') as f:
... f.read()
b'some data'
PyArrow includes two ways to interact with data on disk: standard operating system-level file APIs, and memory-mapped files. In regular Python we can write:
>>> with open('example2.dat', 'wb') as f:
... f.write(b'some example data')
17
Using pyarrows OSFile class, you can write:
>>> with pa.OSFile('example3.dat', 'wb') as f:
... f.write(b'some example data')
17
For reading files, you can use OSFile or
MemoryMappedFile. The difference between these is that
OSFile allocates new memory on each read, like Python file
objects. In reads from memory maps, the library constructs a buffer referencing
the mapped memory without any memory allocation or copying:
>>> file_obj = pa.OSFile('example2.dat')
>>> mmap = pa.memory_map('example3.dat')
>>> file_obj.read(4)
b'some'
>>> mmap.read(4)
b'some'
The read method implements the standard Python file read API. To read
into Arrow Buffer objects, use read_buffer:
>>> mmap.seek(0)
0
>>> buf = mmap.read_buffer(4)
>>> buf
<pyarrow.Buffer address=... size=4 is_cpu=True is_mutable=False>
>>> buf.to_pybytes()
b'some'
Many tools in PyArrow, particular the Apache Parquet interface and the file and
stream messaging tools, are more efficient when used with these NativeFile
types than with normal Python file objects.
To assist with serialization and deserialization of in-memory data, we have file interfaces that can read and write to Arrow Buffers.
>>> writer = pa.BufferOutputStream()
>>> writer.write(b'hello, friends')
14
>>> buf = writer.getvalue()
>>> buf
<pyarrow.Buffer address=... size=14 is_cpu=True is_mutable=True>
>>> buf.size
14
>>> reader = pa.BufferReader(buf)
>>> reader.seek(7)
7
>>> reader.read(7)
b'friends'
These have similar semantics to Pythons built-in io.BytesIO.
| Web Proxy Viewer | New URL | Original Page |