pyfastcdc 0.3.0__tar.gz → 0.3.2b1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyfastcdc-0.3.0/pyfastcdc.egg-info → pyfastcdc-0.3.2b1}/PKG-INFO +4 -3
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/README.md +2 -2
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc/__init__.py +1 -1
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc/__init__.pyi +10 -2
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc/cy/fastcdc.pyx +80 -7
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc/py/fastcdc.py +38 -3
- pyfastcdc-0.3.2b1/pyfastcdc/utils.py +86 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1/pyfastcdc.egg-info}/PKG-INFO +4 -3
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc.egg-info/SOURCES.txt +1 -0
- pyfastcdc-0.3.2b1/pyproject.toml +8 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/setup.py +5 -1
- pyfastcdc-0.3.2b1/tests/test_threading.py +54 -0
- pyfastcdc-0.3.0/pyfastcdc/utils.py +0 -65
- pyfastcdc-0.3.0/pyproject.toml +0 -3
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/LICENSE +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/MANIFEST.in +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc/common.py +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc/cy/__init__.py +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc/cy/chunk.pxd +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc/cy/chunk.pyx +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc/cy/constants.pxd +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc/cy/constants.pyx +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc/py/__init__.py +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc/py/chunk.py +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc/py/constants.py +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc/py.typed +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc.egg-info/dependency_links.txt +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc.egg-info/requires.txt +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/pyfastcdc.egg-info/top_level.txt +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/requirements.dev.txt +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/requirements.txt +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/setup.cfg +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/tests/test_arguments.py +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/tests/test_correctness.py +0 -0
- {pyfastcdc-0.3.0 → pyfastcdc-0.3.2b1}/tests/test_utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pyfastcdc
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.2b1
|
|
4
4
|
Summary: A high-performance FastCDC 2020 implementation written in Python + Cython
|
|
5
5
|
Author: Fallen_Breath
|
|
6
6
|
License: MIT
|
|
@@ -15,6 +15,7 @@ Classifier: Programming Language :: Python :: 3.11
|
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.12
|
|
16
16
|
Classifier: Programming Language :: Python :: 3.13
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
18
19
|
Requires-Python: >=3.6
|
|
19
20
|
Description-Content-Type: text/markdown
|
|
20
21
|
License-File: LICENSE
|
|
@@ -44,7 +45,7 @@ Dynamic: summary
|
|
|
44
45
|
|
|
45
46
|
A high-performance FastCDC 2020 implementation written in Python + [Cython](https://github.com/cython/cython)
|
|
46
47
|
|
|
47
|
-
Supports Python 3.6+. Provides prebuilt wheels for Python 3.
|
|
48
|
+
Supports Python 3.6+. Provides prebuilt wheels for Python 3.9+
|
|
48
49
|
|
|
49
50
|
Its core algorithm implementation is a direct port of the v2020 module from [nlfiedler/fastcdc-rs](https://github.com/nlfiedler/fastcdc-rs),
|
|
50
51
|
which means that the output of PyFastCDC completely matches the output of nlfiedler/fastcdc-rs
|
|
@@ -67,7 +68,7 @@ which is significantly slower (about 0.5% or less in memory chunking speed)
|
|
|
67
68
|
|
|
68
69
|
<details>
|
|
69
70
|
|
|
70
|
-
<summary>I only want to use the Cython
|
|
71
|
+
<summary>I only want to use the Cython implementation, not the slow pure-Python one</summary>
|
|
71
72
|
|
|
72
73
|
You can set the environment variable `PYFASTCDC_REQUIRE_CYTHON=true` or `PYFASTCDC_REQUIRE_CYTHON=1` for the `pip` installation command
|
|
73
74
|
to disable the pure-Python fallback on extension compilation error and make the installation fail hard.
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
A high-performance FastCDC 2020 implementation written in Python + [Cython](https://github.com/cython/cython)
|
|
8
8
|
|
|
9
|
-
Supports Python 3.6+. Provides prebuilt wheels for Python 3.
|
|
9
|
+
Supports Python 3.6+. Provides prebuilt wheels for Python 3.9+
|
|
10
10
|
|
|
11
11
|
Its core algorithm implementation is a direct port of the v2020 module from [nlfiedler/fastcdc-rs](https://github.com/nlfiedler/fastcdc-rs),
|
|
12
12
|
which means that the output of PyFastCDC completely matches the output of nlfiedler/fastcdc-rs
|
|
@@ -29,7 +29,7 @@ which is significantly slower (about 0.5% or less in memory chunking speed)
|
|
|
29
29
|
|
|
30
30
|
<details>
|
|
31
31
|
|
|
32
|
-
<summary>I only want to use the Cython
|
|
32
|
+
<summary>I only want to use the Cython implementation, not the slow pure-Python one</summary>
|
|
33
33
|
|
|
34
34
|
You can set the environment variable `PYFASTCDC_REQUIRE_CYTHON=true` or `PYFASTCDC_REQUIRE_CYTHON=1` for the `pip` installation command
|
|
35
35
|
to disable the pure-Python fallback on extension compilation error and make the installation fail hard.
|
|
@@ -30,6 +30,8 @@ class FastCDC:
|
|
|
30
30
|
"""
|
|
31
31
|
The FastCDC 2020 chunker implementation
|
|
32
32
|
|
|
33
|
+
The object is not concurrent-proof, avoid using it concurrently
|
|
34
|
+
|
|
33
35
|
Paper: https://ieeexplore.ieee.org/document/9055082
|
|
34
36
|
"""
|
|
35
37
|
|
|
@@ -80,6 +82,12 @@ class FastCDC:
|
|
|
80
82
|
"""
|
|
81
83
|
Cut the given file with FastCDC algorithm
|
|
82
84
|
|
|
85
|
+
.. note::
|
|
86
|
+
|
|
87
|
+
If references to chunk ``data`` are kept, the parts of the file they belong to stay mapped in memory.
|
|
88
|
+
On Python < 3.13 or on Windows, each of these mapped parts also holds a duplicated file descriptor (handle),
|
|
89
|
+
which counts towards the open file limit of the process
|
|
90
|
+
|
|
83
91
|
:param file_path: Path to the file to be processed. It should be a readable regular file
|
|
84
92
|
:return: An iterator that yields ``Chunk`` objects
|
|
85
93
|
"""
|
|
@@ -140,8 +148,8 @@ class Chunk:
|
|
|
140
148
|
|
|
141
149
|
.. note::
|
|
142
150
|
|
|
143
|
-
For chunks generated from ``
|
|
144
|
-
|
|
151
|
+
For chunks generated from ``cut_file()``, this memory view is pointed to a read-only mmap of the input file.
|
|
152
|
+
It stays valid as long as it's referenced, and the mapped memory it belongs to won't be released until then
|
|
145
153
|
"""
|
|
146
154
|
|
|
147
155
|
gear_hash: int
|
|
@@ -12,7 +12,7 @@ from pyfastcdc.cy.chunk cimport Chunk
|
|
|
12
12
|
from pyfastcdc.cy.constants cimport GEAR, GEAR_LS, MASKS
|
|
13
13
|
from pyfastcdc.utils import ReadintoFunc
|
|
14
14
|
|
|
15
|
-
cdef struct _Config:
|
|
15
|
+
cdef struct _Config 'pyfastcdc_cut_config':
|
|
16
16
|
uint32_t avg_size
|
|
17
17
|
uint32_t min_size
|
|
18
18
|
uint32_t max_size
|
|
@@ -117,15 +117,30 @@ cdef class FastCDC:
|
|
|
117
117
|
return self.config.max_size
|
|
118
118
|
|
|
119
119
|
|
|
120
|
-
cdef struct _CutResult:
|
|
120
|
+
cdef struct _CutResult 'pyfastcdc_cut_result':
|
|
121
121
|
uint64_t gear_hash
|
|
122
122
|
uint64_t cut_offset
|
|
123
123
|
|
|
124
124
|
|
|
125
|
+
# Align _cut_gear to 64 bytes to stabilize its loops' offsets within instruction cache lines
|
|
126
|
+
cdef extern from *:
|
|
127
|
+
"""
|
|
128
|
+
#if defined(__linux__) && defined(__x86_64__) && defined(__GNUC__) && !defined(__clang__)
|
|
129
|
+
#include <stdint.h>
|
|
130
|
+
|
|
131
|
+
struct pyfastcdc_cut_config;
|
|
132
|
+
struct pyfastcdc_cut_result;
|
|
133
|
+
static struct pyfastcdc_cut_result pyfastcdc_cut_gear(
|
|
134
|
+
const struct pyfastcdc_cut_config *, const uint8_t *, uint64_t
|
|
135
|
+
) __attribute__((aligned(64)));
|
|
136
|
+
#endif
|
|
137
|
+
"""
|
|
138
|
+
|
|
139
|
+
|
|
125
140
|
@cython.boundscheck(False)
|
|
126
141
|
@cython.wraparound(False)
|
|
127
142
|
@cython.cdivision(True)
|
|
128
|
-
cdef _CutResult _cut_gear(const _Config* config, const uint8_t* buf, uint64_t buf_len) noexcept nogil:
|
|
143
|
+
cdef _CutResult _cut_gear 'pyfastcdc_cut_gear'(const _Config* config, const uint8_t* buf, uint64_t buf_len) noexcept nogil:
|
|
129
144
|
cdef uint64_t remaining = buf_len
|
|
130
145
|
if remaining <= config.min_size:
|
|
131
146
|
return _CutResult(0, remaining)
|
|
@@ -207,12 +222,70 @@ cdef class BufferChunker:
|
|
|
207
222
|
return self
|
|
208
223
|
|
|
209
224
|
|
|
210
|
-
cdef class FileMmapChunker
|
|
211
|
-
cdef object
|
|
225
|
+
cdef class FileMmapChunker:
|
|
226
|
+
cdef object fastcdc
|
|
227
|
+
cdef const _Config * config
|
|
228
|
+
cdef object file_mapper
|
|
229
|
+
cdef uint64_t file_size
|
|
230
|
+
cdef uint64_t offset
|
|
231
|
+
|
|
232
|
+
cdef memoryview window
|
|
233
|
+
cdef const uint8_t[:] window_view
|
|
234
|
+
cdef uint64_t window_start
|
|
235
|
+
cdef uint64_t window_end
|
|
212
236
|
|
|
213
237
|
def __init__(self, fastcdc: FastCDC, file_path: Union[str, bytes, Path]):
|
|
214
|
-
self.
|
|
215
|
-
|
|
238
|
+
self.fastcdc = fastcdc # keep ref
|
|
239
|
+
self.config = &fastcdc.config
|
|
240
|
+
self.file_mapper = utils.create_file_mapper(file_path, fastcdc.config.max_size)
|
|
241
|
+
self.file_size = self.file_mapper.file_size
|
|
242
|
+
self.offset = 0
|
|
243
|
+
|
|
244
|
+
self.window = None
|
|
245
|
+
self.window_view = None
|
|
246
|
+
self.window_start = 0
|
|
247
|
+
self.window_end = 0
|
|
248
|
+
|
|
249
|
+
def __next__(self) -> Chunk:
|
|
250
|
+
if self.offset >= self.file_size:
|
|
251
|
+
self.__release()
|
|
252
|
+
raise StopIteration()
|
|
253
|
+
if self.offset + self.config.max_size > self.window_end and self.window_end < self.file_size:
|
|
254
|
+
self.__advance_window()
|
|
255
|
+
|
|
256
|
+
cdef const uint8_t[:] window_view = self.window_view
|
|
257
|
+
cdef uint64_t window_pos = self.offset - self.window_start
|
|
258
|
+
cdef const uint8_t* remaining_buf = &window_view[0] + window_pos
|
|
259
|
+
cdef uint64_t remaining_len = self.window_end - self.offset
|
|
260
|
+
cdef _CutResult res
|
|
261
|
+
with nogil:
|
|
262
|
+
res = _cut_gear(self.config, remaining_buf, remaining_len)
|
|
263
|
+
|
|
264
|
+
chunk = Chunk._cy_create(
|
|
265
|
+
offset=self.offset,
|
|
266
|
+
length=res.cut_offset,
|
|
267
|
+
data=self.window[window_pos:window_pos + res.cut_offset],
|
|
268
|
+
gear_hash=res.gear_hash,
|
|
269
|
+
)
|
|
270
|
+
self.offset += res.cut_offset
|
|
271
|
+
return chunk
|
|
272
|
+
|
|
273
|
+
cdef __advance_window(self):
|
|
274
|
+
window_start, window = self.file_mapper.map_window(self.offset)
|
|
275
|
+
self.window = window
|
|
276
|
+
self.window_view = window
|
|
277
|
+
self.window_start = window_start
|
|
278
|
+
self.window_end = self.window_start + len(window)
|
|
279
|
+
if self.window_end >= self.file_size:
|
|
280
|
+
self.file_mapper.close()
|
|
281
|
+
|
|
282
|
+
cdef __release(self):
|
|
283
|
+
self.window = None
|
|
284
|
+
self.window_view = None
|
|
285
|
+
self.file_mapper.close()
|
|
286
|
+
|
|
287
|
+
def __iter__(self):
|
|
288
|
+
return self
|
|
216
289
|
|
|
217
290
|
|
|
218
291
|
cdef class StreamChunker:
|
|
@@ -205,10 +205,45 @@ class BufferChunker(Iterator[Chunk]):
|
|
|
205
205
|
return chunk
|
|
206
206
|
|
|
207
207
|
|
|
208
|
-
class FileMmapChunker(
|
|
208
|
+
class FileMmapChunker(Iterator[Chunk]):
|
|
209
209
|
def __init__(self, config: _Config, file_path: Union[str, bytes, Path]):
|
|
210
|
-
self.
|
|
211
|
-
|
|
210
|
+
self.config = config
|
|
211
|
+
self.file_mapper = utils.create_file_mapper(file_path, config.max_size)
|
|
212
|
+
self.file_size = self.file_mapper.file_size
|
|
213
|
+
self.offset = 0
|
|
214
|
+
|
|
215
|
+
self.window: Optional[memoryview] = None
|
|
216
|
+
self.window_start = 0
|
|
217
|
+
self.window_end = 0
|
|
218
|
+
|
|
219
|
+
def __next__(self) -> Chunk:
|
|
220
|
+
if self.offset >= self.file_size:
|
|
221
|
+
self.__release()
|
|
222
|
+
raise StopIteration()
|
|
223
|
+
if self.offset + self.config.max_size > self.window_end and self.window_end < self.file_size:
|
|
224
|
+
self.__advance_window()
|
|
225
|
+
|
|
226
|
+
window_pos = self.offset - self.window_start
|
|
227
|
+
res = _cut_gear(self.config, self.window[window_pos:])
|
|
228
|
+
|
|
229
|
+
chunk = Chunk(
|
|
230
|
+
offset=self.offset,
|
|
231
|
+
length=res.cut_offset,
|
|
232
|
+
data=self.window[window_pos:window_pos + res.cut_offset],
|
|
233
|
+
gear_hash=res.gear_hash,
|
|
234
|
+
)
|
|
235
|
+
self.offset += res.cut_offset
|
|
236
|
+
return chunk
|
|
237
|
+
|
|
238
|
+
def __advance_window(self):
|
|
239
|
+
self.window_start, self.window = self.file_mapper.map_window(self.offset)
|
|
240
|
+
self.window_end = self.window_start + len(self.window)
|
|
241
|
+
if self.window_end >= self.file_size:
|
|
242
|
+
self.file_mapper.close()
|
|
243
|
+
|
|
244
|
+
def __release(self):
|
|
245
|
+
self.window = None
|
|
246
|
+
self.file_mapper.close()
|
|
212
247
|
|
|
213
248
|
|
|
214
249
|
class StreamChunker(Iterator[Chunk]):
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import math
|
|
3
|
+
import mmap
|
|
4
|
+
import os
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Callable, Union, Optional, ClassVar, Dict, Any, Tuple
|
|
8
|
+
|
|
9
|
+
from pyfastcdc.common import BinaryStreamReader
|
|
10
|
+
|
|
11
|
+
ReadintoFunc = Callable[[memoryview], int]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def logarithm2(value: int) -> int:
|
|
15
|
+
# The same as the nlfiedler/fastcdc-rs v4.0.1 Rust implementation: (value as f64).log2().round() as u32
|
|
16
|
+
if value < 0:
|
|
17
|
+
raise ValueError(f'value must be non-negative, got {value}')
|
|
18
|
+
if value == 0:
|
|
19
|
+
return 0
|
|
20
|
+
return math.floor(math.log2(value) + 0.5)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def create_memoryview_from_buffer(buf: Union[bytes, bytearray, memoryview]) -> memoryview:
|
|
24
|
+
if isinstance(buf, (bytes, bytearray, memoryview)):
|
|
25
|
+
return memoryview(buf)
|
|
26
|
+
raise TypeError('buf must be bytes or bytearray')
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def create_readinto_func(stream: BinaryStreamReader) -> ReadintoFunc:
|
|
30
|
+
readinto_func: ReadintoFunc = getattr(stream, 'readinto', None)
|
|
31
|
+
if readinto_func is not None and callable(readinto_func):
|
|
32
|
+
return readinto_func
|
|
33
|
+
|
|
34
|
+
read_func = getattr(stream, 'read', None)
|
|
35
|
+
if read_func is not None and callable(read_func):
|
|
36
|
+
def readinto_using_read(dest_buf: memoryview) -> int:
|
|
37
|
+
read_buf = read_func(len(dest_buf))
|
|
38
|
+
dest_buf[:len(read_buf)] = read_buf
|
|
39
|
+
return len(read_buf)
|
|
40
|
+
|
|
41
|
+
return readinto_using_read
|
|
42
|
+
|
|
43
|
+
raise TypeError('stream must be readable')
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class FileMapper:
|
|
47
|
+
__MIN_WINDOW_SIZE: ClassVar[int] = 32 * 1048576
|
|
48
|
+
__MMAP_EXTRA_KWARGS: ClassVar[Dict[str, Any]] = {'trackfd': False} if sys.version_info >= (3, 13) and os.name != 'nt' else {}
|
|
49
|
+
|
|
50
|
+
def __init__(self, file_path: Union[str, bytes, Path], lookahead: int):
|
|
51
|
+
self.__file: Optional[io.FileIO] = None
|
|
52
|
+
self.__file = open(file_path, 'rb', buffering=0)
|
|
53
|
+
self.__file_size: int = os.fstat(self.__file.fileno()).st_size
|
|
54
|
+
if self.__file_size == 0:
|
|
55
|
+
self.close()
|
|
56
|
+
|
|
57
|
+
granularity = mmap.ALLOCATIONGRANULARITY
|
|
58
|
+
window_size = max(self.__MIN_WINDOW_SIZE, 4 * lookahead, lookahead + granularity)
|
|
59
|
+
self.__granularity = granularity
|
|
60
|
+
self.__window_size = (window_size + granularity - 1) // granularity * granularity
|
|
61
|
+
|
|
62
|
+
@property
|
|
63
|
+
def file_size(self) -> int:
|
|
64
|
+
return self.__file_size
|
|
65
|
+
|
|
66
|
+
def map_window(self, offset: int) -> Tuple[int, memoryview]:
|
|
67
|
+
if self.__file is None:
|
|
68
|
+
raise ValueError('file mapper is closed')
|
|
69
|
+
if not (0 <= offset < self.__file_size):
|
|
70
|
+
raise ValueError(f'offset {offset} is out of range [0, {self.__file_size})')
|
|
71
|
+
window_start = offset - offset % self.__granularity
|
|
72
|
+
window_len = min(self.__window_size, self.__file_size - window_start)
|
|
73
|
+
mmap_obj = mmap.mmap(self.__file.fileno(), window_len, access=mmap.ACCESS_READ, offset=window_start, **self.__MMAP_EXTRA_KWARGS)
|
|
74
|
+
return window_start, memoryview(mmap_obj)
|
|
75
|
+
|
|
76
|
+
def close(self):
|
|
77
|
+
if self.__file is not None:
|
|
78
|
+
self.__file.close()
|
|
79
|
+
self.__file = None
|
|
80
|
+
|
|
81
|
+
def __del__(self):
|
|
82
|
+
self.close()
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def create_file_mapper(file_path: Union[str, bytes, Path], lookahead: int) -> FileMapper:
|
|
86
|
+
return FileMapper(file_path, lookahead)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pyfastcdc
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.2b1
|
|
4
4
|
Summary: A high-performance FastCDC 2020 implementation written in Python + Cython
|
|
5
5
|
Author: Fallen_Breath
|
|
6
6
|
License: MIT
|
|
@@ -15,6 +15,7 @@ Classifier: Programming Language :: Python :: 3.11
|
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.12
|
|
16
16
|
Classifier: Programming Language :: Python :: 3.13
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
18
19
|
Requires-Python: >=3.6
|
|
19
20
|
Description-Content-Type: text/markdown
|
|
20
21
|
License-File: LICENSE
|
|
@@ -44,7 +45,7 @@ Dynamic: summary
|
|
|
44
45
|
|
|
45
46
|
A high-performance FastCDC 2020 implementation written in Python + [Cython](https://github.com/cython/cython)
|
|
46
47
|
|
|
47
|
-
Supports Python 3.6+. Provides prebuilt wheels for Python 3.
|
|
48
|
+
Supports Python 3.6+. Provides prebuilt wheels for Python 3.9+
|
|
48
49
|
|
|
49
50
|
Its core algorithm implementation is a direct port of the v2020 module from [nlfiedler/fastcdc-rs](https://github.com/nlfiedler/fastcdc-rs),
|
|
50
51
|
which means that the output of PyFastCDC completely matches the output of nlfiedler/fastcdc-rs
|
|
@@ -67,7 +68,7 @@ which is significantly slower (about 0.5% or less in memory chunking speed)
|
|
|
67
68
|
|
|
68
69
|
<details>
|
|
69
70
|
|
|
70
|
-
<summary>I only want to use the Cython
|
|
71
|
+
<summary>I only want to use the Cython implementation, not the slow pure-Python one</summary>
|
|
71
72
|
|
|
72
73
|
You can set the environment variable `PYFASTCDC_REQUIRE_CYTHON=true` or `PYFASTCDC_REQUIRE_CYTHON=1` for the `pip` installation command
|
|
73
74
|
to disable the pure-Python fallback on extension compilation error and make the installation fail hard.
|
|
@@ -92,7 +92,10 @@ else:
|
|
|
92
92
|
from Cython.Build import cythonize
|
|
93
93
|
ext_modules = cythonize(
|
|
94
94
|
'pyfastcdc/cy/*.pyx',
|
|
95
|
-
compiler_directives={
|
|
95
|
+
compiler_directives={
|
|
96
|
+
'language_level': '3',
|
|
97
|
+
**({'freethreading_compatible': True} if sys.version_info >= (3, 13) else {}),
|
|
98
|
+
},
|
|
96
99
|
)
|
|
97
100
|
|
|
98
101
|
print(f'setuptools_version: {setuptools_version}')
|
|
@@ -135,6 +138,7 @@ setup(
|
|
|
135
138
|
'Programming Language :: Python :: 3.12',
|
|
136
139
|
'Programming Language :: Python :: 3.13',
|
|
137
140
|
'Programming Language :: Python :: 3.14',
|
|
141
|
+
'Programming Language :: Python :: 3.15',
|
|
138
142
|
],
|
|
139
143
|
ext_modules=ext_modules,
|
|
140
144
|
cmdclass={'build_ext': BuildExt},
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import subprocess
|
|
3
|
+
import sys
|
|
4
|
+
import sysconfig
|
|
5
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
6
|
+
from io import BytesIO
|
|
7
|
+
from threading import Barrier
|
|
8
|
+
|
|
9
|
+
import pytest
|
|
10
|
+
|
|
11
|
+
from pyfastcdc import FastCDC
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@pytest.mark.skipif(not sysconfig.get_config_var('Py_GIL_DISABLED'), reason='requires free-threaded Python')
|
|
15
|
+
def test_import_keeps_gil_disabled():
|
|
16
|
+
# Check the first import in a fresh process, without forcing the GIL off.
|
|
17
|
+
env = os.environ.copy()
|
|
18
|
+
env.pop('PYTHON_GIL', None)
|
|
19
|
+
result = subprocess.run(
|
|
20
|
+
[sys.executable, '-c', (
|
|
21
|
+
'import sys; assert not sys._is_gil_enabled(); '
|
|
22
|
+
'import pyfastcdc; assert not sys._is_gil_enabled()'
|
|
23
|
+
)],
|
|
24
|
+
env=env, capture_output=True, text=True, timeout=30,
|
|
25
|
+
)
|
|
26
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@pytest.mark.parametrize('method', ['buf', 'stream'])
|
|
30
|
+
def test_independent_instances_in_parallel(method, random_data_1m):
|
|
31
|
+
def collect(seed, data):
|
|
32
|
+
cdc = FastCDC(avg_size=4096, seed=seed)
|
|
33
|
+
chunks = cdc.cut_buf(data) if method == 'buf' else cdc.cut_stream(BytesIO(data))
|
|
34
|
+
# Copy each stream chunk before advancing the iterator and reusing its buffer.
|
|
35
|
+
return [(chunk.offset, chunk.length, chunk.gear_hash, bytes(chunk.data)) for chunk in chunks]
|
|
36
|
+
|
|
37
|
+
cases = [(seed, bytes([seed]) + random_data_1m) for seed in (0, 1)]
|
|
38
|
+
expected = [collect(*case) for case in cases]
|
|
39
|
+
start = Barrier(len(cases), timeout=30)
|
|
40
|
+
free_threaded = bool(sysconfig.get_config_var('Py_GIL_DISABLED'))
|
|
41
|
+
|
|
42
|
+
def worker(case):
|
|
43
|
+
start.wait()
|
|
44
|
+
if free_threaded:
|
|
45
|
+
assert not sys._is_gil_enabled()
|
|
46
|
+
result = collect(*case)
|
|
47
|
+
if free_threaded:
|
|
48
|
+
assert not sys._is_gil_enabled()
|
|
49
|
+
return result
|
|
50
|
+
|
|
51
|
+
with ThreadPoolExecutor(max_workers=len(cases)) as pool:
|
|
52
|
+
futures = [pool.submit(worker, case) for case in cases]
|
|
53
|
+
actual = [future.result() for future in futures]
|
|
54
|
+
assert actual == expected
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import math
|
|
2
|
-
import mmap
|
|
3
|
-
import os
|
|
4
|
-
from pathlib import Path
|
|
5
|
-
from typing import Callable, Union, Optional
|
|
6
|
-
|
|
7
|
-
from pyfastcdc.common import BinaryStreamReader
|
|
8
|
-
|
|
9
|
-
ReadintoFunc = Callable[[memoryview], int]
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
def logarithm2(value: int) -> int:
|
|
13
|
-
# The same as the nlfiedler/fastcdc-rs v4.0.1 Rust implementation: (value as f64).log2().round() as u32
|
|
14
|
-
if value < 0:
|
|
15
|
-
raise ValueError(f'value must be non-negative, got {value}')
|
|
16
|
-
if value == 0:
|
|
17
|
-
return 0
|
|
18
|
-
return math.floor(math.log2(value) + 0.5)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def create_memoryview_from_buffer(buf: Union[bytes, bytearray, memoryview]) -> memoryview:
|
|
22
|
-
if isinstance(buf, (bytes, bytearray, memoryview)):
|
|
23
|
-
return memoryview(buf)
|
|
24
|
-
raise TypeError('buf must be bytes or bytearray')
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
def create_readinto_func(stream: BinaryStreamReader) -> ReadintoFunc:
|
|
28
|
-
readinto_func: ReadintoFunc = getattr(stream, 'readinto', None)
|
|
29
|
-
if readinto_func is not None and callable(readinto_func):
|
|
30
|
-
return readinto_func
|
|
31
|
-
|
|
32
|
-
read_func = getattr(stream, 'read', None)
|
|
33
|
-
if read_func is not None and callable(read_func):
|
|
34
|
-
def readinto_using_read(dest_buf: memoryview) -> int:
|
|
35
|
-
read_buf = read_func(len(dest_buf))
|
|
36
|
-
dest_buf[:len(read_buf)] = read_buf
|
|
37
|
-
return len(read_buf)
|
|
38
|
-
|
|
39
|
-
return readinto_using_read
|
|
40
|
-
|
|
41
|
-
raise TypeError('stream must be readable')
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
class MmapFile:
|
|
45
|
-
def __init__(self, file_path: Union[str, bytes, Path]):
|
|
46
|
-
self.__mmap_obj: Optional[mmap.mmap] = None
|
|
47
|
-
self.__data = memoryview(b'')
|
|
48
|
-
self.__open(file_path)
|
|
49
|
-
|
|
50
|
-
def __open(self, file_path: Union[str, bytes, Path]):
|
|
51
|
-
file_size = os.path.getsize(file_path)
|
|
52
|
-
if file_size == 0:
|
|
53
|
-
return
|
|
54
|
-
|
|
55
|
-
with open(file_path, 'rb') as f:
|
|
56
|
-
self.__mmap_obj = mmap.mmap(f.fileno(), length=file_size, access=mmap.ACCESS_READ)
|
|
57
|
-
self.__data = memoryview(self.__mmap_obj)
|
|
58
|
-
|
|
59
|
-
@property
|
|
60
|
-
def data(self) -> memoryview:
|
|
61
|
-
return self.__data
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
def create_mmap_from_file(file_path: Union[str, bytes, Path]) -> MmapFile:
|
|
65
|
-
return MmapFile(file_path)
|
pyfastcdc-0.3.0/pyproject.toml
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|