Skip to content

Commit c55a2b2

Browse files
Merge pull request #20305 from neutrinoceros/io.fits/rfc/no-tobytes
RFC: avoid unnecessary buffer copies in gzip fits codec
1 parent b707665 commit c55a2b2

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

‎astropy/io/fits/hdu/compressed/_codecs.py‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
This module contains the FITS compression algorithms in numcodecs style Codecs.
33
"""
44

5+
import sys
56
from gzip import compress as gzip_compress
67
from gzip import decompress as gzip_decompress
78

@@ -119,9 +120,9 @@ def decode(self, buf):
119120
buf : np.ndarray
120121
The decompressed buffer.
121122
"""
122-
# In principle we should be able to not have .tobytes() here and avoid
123-
# the copy but this does not work correctly in Python 3.11.
124-
cbytes = np.frombuffer(buf, dtype=np.uint8).tobytes()
123+
cbytes = np.frombuffer(buf, dtype=np.uint8)
124+
if sys.version_info < (3, 13):
125+
cbytes = cbytes.tobytes()
125126
dbytes = gzip_decompress(cbytes)
126127
return np.frombuffer(dbytes, dtype=np.uint8)
127128

@@ -140,9 +141,11 @@ def encode(self, buf):
140141
The compressed bytes.
141142
"""
142143
# Data bytes should be stored as big endian in files
143-
# In principle we should be able to not have .tobytes() here and avoid
144-
# the copy but this does not work correctly in Python 3.11.
145-
dbytes = _as_big_endian_array(buf).tobytes()
144+
be_arr = _as_big_endian_array(buf)
145+
if sys.version_info >= (3, 13):
146+
dbytes = np.ascontiguousarray(be_arr)
147+
else:
148+
dbytes = be_arr.tobytes()
146149
return gzip_compress(dbytes)
147150

148151

0 commit comments

Comments
 (0)