Skip to content

Commit 1ab9983

Browse files
committed
Resize buffers without copying
1 parent 9069553 commit 1ab9983

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

numcodecs/blosc.pyx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ from deprecated import deprecated
1010

1111

1212
from cpython.buffer cimport PyBuffer_IsContiguous
13-
from cpython.bytes cimport PyBytes_FromStringAndSize, PyBytes_AS_STRING
13+
from cpython.bytes cimport (
14+
PyBytes_AS_STRING,
15+
PyBytes_FromStringAndSize,
16+
_PyBytes_Resize,
17+
)
1418
from cpython.memoryview cimport PyMemoryView_GET_BUFFER
19+
from cpython.object cimport PyObject
1520

1621

1722
from .compat import ensure_contiguous_ndarray
@@ -271,6 +276,7 @@ def compress(source, char* cname, int clevel, int shuffle=SHUFFLE,
271276
size_t nbytes, itemsize
272277
int cbytes
273278
bytes dest
279+
PyObject* dest_objptr
274280
char* dest_ptr
275281

276282
# check valid cname early
@@ -350,7 +356,8 @@ def compress(source, char* cname, int clevel, int shuffle=SHUFFLE,
350356
raise RuntimeError('error during blosc compression: %d' % cbytes)
351357

352358
# resize after compression
353-
dest = dest[:cbytes]
359+
dest_objptr = <PyObject*>dest
360+
_PyBytes_Resize(&dest_objptr, cbytes)
354361

355362
return dest
356363

numcodecs/lz4.pyx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
from libc.stdint cimport uint8_t, uint32_t
99

1010
from cpython.buffer cimport PyBuffer_IsContiguous
11-
from cpython.bytes cimport PyBytes_FromStringAndSize, PyBytes_AS_STRING
11+
from cpython.bytes cimport (
12+
PyBytes_AS_STRING,
13+
PyBytes_FromStringAndSize,
14+
_PyBytes_Resize,
15+
)
1216
from cpython.memoryview cimport PyMemoryView_GET_BUFFER
17+
from cpython.object cimport PyObject
1318

1419
from ._utils cimport store_le32, load_le32
1520
from .compat import ensure_contiguous_ndarray
@@ -69,6 +74,7 @@ def compress(source, int acceleration=DEFAULT_ACCELERATION):
6974
const Py_buffer* source_pb
7075
const char* source_ptr
7176
bytes dest
77+
PyObject* dest_objptr
7278
char* dest_ptr
7379
char* dest_start
7480
int source_size, dest_size, compressed_size
@@ -108,7 +114,8 @@ def compress(source, int acceleration=DEFAULT_ACCELERATION):
108114

109115
# resize after compression
110116
compressed_size += sizeof(uint32_t)
111-
dest = dest[:compressed_size]
117+
dest_objptr = <PyObject*>dest
118+
_PyBytes_Resize(&dest_objptr, compressed_size)
112119

113120
return dest
114121

numcodecs/zstd.pyx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66

77

88
from cpython.buffer cimport PyBuffer_IsContiguous
9-
from cpython.bytes cimport PyBytes_FromStringAndSize, PyBytes_AS_STRING
9+
from cpython.bytes cimport (
10+
PyBytes_AS_STRING,
11+
PyBytes_FromStringAndSize,
12+
_PyBytes_Resize,
13+
)
1014
from cpython.memoryview cimport PyMemoryView_GET_BUFFER
15+
from cpython.object cimport PyObject
1116

1217
from .compat import ensure_contiguous_ndarray
1318
from .abc import Codec
@@ -95,6 +100,7 @@ def compress(source, int level=DEFAULT_CLEVEL, bint checksum=False):
95100
const char* source_ptr
96101
size_t source_size, dest_size, compressed_size
97102
bytes dest
103+
PyObject* dest_objptr
98104
char* dest_ptr
99105

100106
# check level
@@ -145,7 +151,8 @@ def compress(source, int level=DEFAULT_CLEVEL, bint checksum=False):
145151
raise RuntimeError('Zstd compression error: %s' % error)
146152

147153
# resize after compression
148-
dest = dest[:compressed_size]
154+
dest_objptr = <PyObject*>dest
155+
_PyBytes_Resize(&dest_objptr, compressed_size)
149156

150157
return dest
151158

0 commit comments

Comments
 (0)