Skip to content

Speeding up SZ3 Python API Compress and Decompress functions - #99

Merged
ayzk merged 2 commits into
szcompressor:sz3.3_developfrom
blt-sl:sz3.3_develop
Jul 9, 2025
Merged

ayzk merged 2 commits into
szcompressor:sz3.3_developfrom
blt-sl:sz3.3_develop

Conversation

@blt-sl

@blt-sl blt-sl commented Jul 9, 2025

Copy link
Copy Markdown

The Python API functions currently has a bottleneck in how it transfers data from the SZ3 C buffer to a Python array. Currently in both the compress and decompress functions, the C buffer is indexed and then send into a np.array block. This can lead to some serious bottle necks. For example, I compressed 2 GB of data with SZ3 and the compress step took me 4.5 seconds, with 1 second dedicated to line 106 of the decompress functions below, and the decompress took me 17 seconds, with over 12 of those seconds dedicated to line 80.

This method of transferring data back to Python is inefficient and can be sped up by using Numpy's from buffer function. I have added in the required lines to speed up the compress and decompress Python functions. It now takes me 3.5 seconds to compress and around 2 seconds to compress 2 GB of data.

I recommend integrating the following changes in to ensure the Python API is able to take better advantage of SZ3's speed.

blt-sl added 2 commits July 9, 2025 08:48
…m C++ in both the compression and decompression functions. This results in a massive speed up of SZ3's Python API.
@ayzk
ayzk requested a review from Copilot July 9, 2025 18:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR replaces element-wise array creation in the Python API with np.frombuffer to eliminate Python-level indexing bottlenecks and reduce memory-copy time for both compression and decompression.

  • Use np.frombuffer on the C buffer for fast bulk data transfer.
  • Preserve original behavior by copying the buffer before freeing it.
  • Free the C buffer after converting to a NumPy array.
Comments suppressed due to low confidence (1)

tools/pysz/pysz.py:89

  • Add unit tests for the new frombuffer path to verify correct handling of different dtypes and shapes, ensuring no off-by-one or alignment issues are introduced.
        data_dec = np.frombuffer((ctypes.c_ubyte * buffer_size).from_address(buffer_ptr.value),

Comment thread tools/pysz/pysz.py
# Use np.frombuffer for fast memory copy
# Cast the ctypes pointer to void pointer, then use addressof to get memory address
buffer_ptr = ctypes.cast(data_dec_c, ctypes.c_void_p)
buffer_size = total_elements * np.dtype(original_dtype).itemsize

Copilot AI Jul 9, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting total_elements (a NumPy scalar) to a Python int before using it in the ctypes array declaration will avoid type errors. e.g. buffer_size = int(total_elements) * np.dtype(original_dtype).itemsize.

Suggested change
buffer_size = total_elements * np.dtype(original_dtype).itemsize
buffer_size = int(total_elements) * np.dtype(original_dtype).itemsize

Copilot uses AI. Check for mistakes.
Comment thread tools/pysz/pysz.py
Comment on lines +83 to 99
# Use np.frombuffer for fast memory copy
# Cast the ctypes pointer to void pointer, then use addressof to get memory address
buffer_ptr = ctypes.cast(data_dec_c, ctypes.c_void_p)
buffer_size = total_elements * np.dtype(original_dtype).itemsize

# Create numpy array directly from memory buffer
data_dec = np.frombuffer((ctypes.c_ubyte * buffer_size).from_address(buffer_ptr.value),
dtype=original_dtype,
count=total_elements
).reshape(original_shape)

# Make a copy since we're going to free the original memory
data_dec = data_dec.copy()

self.sz.free_buf(data_dec_c)
return data_dec

Copilot AI Jul 9, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The buffer-to-NumPy conversion logic is duplicated in both compress and decompress. Consider extracting this into a helper function to reduce code duplication and make future adjustments easier.

Suggested change
# Use np.frombuffer for fast memory copy
# Cast the ctypes pointer to void pointer, then use addressof to get memory address
buffer_ptr = ctypes.cast(data_dec_c, ctypes.c_void_p)
buffer_size = total_elements * np.dtype(original_dtype).itemsize
# Create numpy array directly from memory buffer
data_dec = np.frombuffer((ctypes.c_ubyte * buffer_size).from_address(buffer_ptr.value),
dtype=original_dtype,
count=total_elements
).reshape(original_shape)
# Make a copy since we're going to free the original memory
data_dec = data_dec.copy()
self.sz.free_buf(data_dec_c)
return data_dec
# Convert buffer to NumPy array using helper function
data_dec = self._buffer_to_numpy_array(data_dec_c, original_dtype, original_shape)
# Make a copy since we're going to free the original memory
data_dec = data_dec.copy()
self.sz.free_buf(data_dec_c)
return data_dec
def _buffer_to_numpy_array(self, buffer_ptr, dtype, shape):
"""
Convert a memory buffer to a NumPy array.
:param buffer_ptr: ctypes pointer to the buffer
:param dtype: NumPy data type of the array
:param shape: Shape of the resulting NumPy array
:return: NumPy array
"""
total_elements = np.prod(shape)
buffer_size = total_elements * np.dtype(dtype).itemsize
# Create numpy array directly from memory buffer
numpy_array = np.frombuffer((ctypes.c_ubyte * buffer_size).from_address(buffer_ptr.value),
dtype=dtype,
count=total_elements
).reshape(shape)
return numpy_array

Copilot uses AI. Check for mistakes.
@ayzk
ayzk merged commit e981348 into szcompressor:sz3.3_develop Jul 9, 2025
@ayzk

ayzk commented Jul 9, 2025

Copy link
Copy Markdown
Collaborator

@blt-sl Thanks for this update!

ayzk added a commit that referenced this pull request Aug 6, 2025
* Algorithmic updates

* replace pointwise iterator by blockwise iterator

* replace pointwise iterator by blockwise iterator

* Merge test units (#95)

* update python support for Windows

* update config to support save and load to ini

* update error handling

* update conf

* Speeding up SZ3 Python API Compress and Decompress functions (#99)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants