pyfastcdc 0.3.1__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.
Files changed (34) hide show
  1. {pyfastcdc-0.3.1/pyfastcdc.egg-info → pyfastcdc-0.3.2b1}/PKG-INFO +1 -1
  2. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc/__init__.py +1 -1
  3. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc/__init__.pyi +8 -2
  4. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc/cy/fastcdc.pyx +80 -7
  5. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc/py/fastcdc.py +38 -3
  6. pyfastcdc-0.3.2b1/pyfastcdc/utils.py +86 -0
  7. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1/pyfastcdc.egg-info}/PKG-INFO +1 -1
  8. pyfastcdc-0.3.1/pyfastcdc/utils.py +0 -65
  9. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/LICENSE +0 -0
  10. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/MANIFEST.in +0 -0
  11. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/README.md +0 -0
  12. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc/common.py +0 -0
  13. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc/cy/__init__.py +0 -0
  14. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc/cy/chunk.pxd +0 -0
  15. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc/cy/chunk.pyx +0 -0
  16. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc/cy/constants.pxd +0 -0
  17. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc/cy/constants.pyx +0 -0
  18. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc/py/__init__.py +0 -0
  19. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc/py/chunk.py +0 -0
  20. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc/py/constants.py +0 -0
  21. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc/py.typed +0 -0
  22. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc.egg-info/SOURCES.txt +0 -0
  23. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc.egg-info/dependency_links.txt +0 -0
  24. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc.egg-info/requires.txt +0 -0
  25. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyfastcdc.egg-info/top_level.txt +0 -0
  26. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/pyproject.toml +0 -0
  27. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/requirements.dev.txt +0 -0
  28. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/requirements.txt +0 -0
  29. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/setup.cfg +0 -0
  30. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/setup.py +0 -0
  31. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/tests/test_arguments.py +0 -0
  32. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/tests/test_correctness.py +0 -0
  33. {pyfastcdc-0.3.1 → pyfastcdc-0.3.2b1}/tests/test_threading.py +0 -0
  34. {pyfastcdc-0.3.1 → 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.1
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
@@ -1,4 +1,4 @@
1
- __version__ = '0.3.1'
1
+ __version__ = '0.3.2b1'
2
2
 
3
3
  __all__ = [
4
4
  'BinaryStreamReader',
@@ -82,6 +82,12 @@ class FastCDC:
82
82
  """
83
83
  Cut the given file with FastCDC algorithm
84
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
+
85
91
  :param file_path: Path to the file to be processed. It should be a readable regular file
86
92
  :return: An iterator that yields ``Chunk`` objects
87
93
  """
@@ -142,8 +148,8 @@ class Chunk:
142
148
 
143
149
  .. note::
144
150
 
145
- For chunks generated from ``cut_files()``, this memory view will be pointed to part of an mmap object of the input file,
146
- which means the mmap object won't be released until all references to the generated chunk objects are released
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
147
153
  """
148
154
 
149
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(BufferChunker):
211
- cdef object mmap_file
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.mmap_file = utils.create_mmap_from_file(file_path)
215
- BufferChunker.__init__(self, fastcdc, self.mmap_file.data)
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(BufferChunker):
208
+ class FileMmapChunker(Iterator[Chunk]):
209
209
  def __init__(self, config: _Config, file_path: Union[str, bytes, Path]):
210
- self.mmap_file = utils.create_mmap_from_file(file_path)
211
- super().__init__(config, self.mmap_file.data)
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.1
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
@@ -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)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes