Debian Codegraph @

benchmarks/hyperscan_gitignore_r1.py

raw
1 """
2 This module defines the hyperscan backends for `.GitIgnoreSpec`, revision 1,
3 used in benchmarking, but not included in the released library.
4 """
5 from __future__ import annotations
6
7 from collections.abc import (
8 Sequence)
9 from typing import (
10 Any,
11 Optional) # Replaced by `X | None` in 3.10.
12 from typing_extensions import (
13 override) # Added in 3.12.
14
15 try:
16 import hyperscan
17 except ModuleNotFoundError:
18 hyperscan = None
19
20 from pathspec.pattern import (
21 RegexPattern)
22 from pathspec.patterns.gitignore.spec import (
23 _DIR_MARK)
24
25 from benchmarks.hyperscan_pathspec_r1 import (
26 HyperscanPsR1BaseBackend)
27
28
29 class _HyperscanGiR1BaseBackend(HyperscanPsR1BaseBackend):
30 """
31 The :class:`_HyperscanGiR1BaseBackend` base class uses a hyperscan database in
32 block mode for matching files.
33 """
34
35
36 class _HyperscanGiR1BlockBaseBackend(_HyperscanGiR1BaseBackend):
37 """
38 The :class:`_HyperscanGiR1BlockBaseBackend` base class uses a hyperscan
39 database in block mode for matching files.
40 """
41
42 @override
43 @staticmethod
44 def _make_db() -> hyperscan.Database:
45 return hyperscan.Database(mode=hyperscan.HS_MODE_BLOCK)
46
47
48 class HyperscanGiR1BlockClosureBackend(_HyperscanGiR1BlockBaseBackend):
49 """
50 The :class:`HyperscanGiR1BlockClosureBackend` class uses a hyperscan database
51 in block mode for matching files, and uses a closure to capture state.
52 """
53
54 # Prevent accidental usage.
55 _out: tuple[()]
56
57 @override
58 def match_file(self, file: str) -> tuple[Optional[bool], Optional[int]]:
59 out_include: Optional[bool] = None
60 out_index: Optional[int] = -1
61 out_priority = 0
62
63 def on_match(
64 expr_id: int, _from: int, _to: int, _flags: int, _context: Any,
65 ) -> Optional[bool]:
66 nonlocal out_include, out_index, out_priority
67 expr_dat = self._expr_data[expr_id]
68 index = expr_dat.index
69
70 # Rematch pattern because Hyperscan does not support capture groups.
71 pattern = self._patterns[index]
72 match = pattern.match_file(file)
73
74 # Check for directory marker.
75 dir_mark = match.match.groupdict().get(_DIR_MARK)
76
77 if dir_mark:
78 # Pattern matched by a directory pattern.
79 priority = 1
80 else:
81 # Pattern matched by a file pattern.
82 priority = 2
83
84 # WARNING: Hyperscan does not guarantee matches will be produced in order!
85 include = expr_dat.include
86 if (
87 (include and dir_mark and index > out_index)
88 or (priority == out_priority and index > out_index)
89 or priority > out_priority
90 ):
91 out_include = include
92 out_index = index
93 out_priority = priority
94
95 self._db.scan(file.encode('utf8'), match_event_handler=on_match)
96
97 if out_index == -1:
98 out_index = None
99
100 return (out_include, out_index)
101
102
103 class HyperscanGiR1BlockStateBackend(_HyperscanGiR1BlockBaseBackend):
104 """
105 The :class:`HyperscanGiR1BlockStateBackend` class uses a hyperscan database in
106 block mode for matching files, and stores state in variables.
107 """
108
109 # Change type hint.
110 _out: tuple[Optional[bool], int, int]
111
112 def __init__(self, patterns: Sequence[RegexPattern]) -> None:
113 super().__init__(patterns)
114 self._out = (None, -1, 0)
115
116 @override
117 def match_file(self, file: str) -> tuple[Optional[bool], Optional[int]]:
118 self._out = (None, -1, 0)
119 self._db.scan(
120 file.encode('utf8'), match_event_handler=self.__on_match, context=file,
121 )
122
123 out_include, out_index = self._out[:2]
124 if out_index == -1:
125 out_index = None
126
127 return (out_include, out_index)
128
129 @override
130 def __on_match(
131 self,
132 expr_id: int,
133 _from: int,
134 _to: int,
135 _flags: int,
136 context: Any,
137 ) -> Optional[bool]:
138 file: str = context
139 expr_dat = self._expr_data[expr_id]
140 index = expr_dat.index
141
142 # Rematch pattern because Hyperscan does not support capture groups.
143 pattern = self._patterns[index]
144 match = pattern.match_file(file)
145
146 # Check for directory marker.
147 dir_mark = match.match.groupdict().get(_DIR_MARK)
148
149 if dir_mark:
150 # Pattern matched by a directory pattern.
151 priority = 1
152 else:
153 # Pattern matched by a file pattern.
154 priority = 2
155
156 # WARNING: Hyperscan does not guarantee matches will be produced in order!
157 include = expr_dat.include
158 prev_index = self._out[1]
159 prev_priority = self._out[2]
160 if (
161 (include and dir_mark and index > prev_index)
162 or (priority == prev_priority and index > prev_index)
163 or priority > prev_priority
164 ):
165 self._out = (include, index, priority)
166
167
168 class _HyperscanGiR1StreamBaseBackend(_HyperscanGiR1BaseBackend):
169 """
170 The :class:`_HyperscanGiR1StreamBaseBackend` base class uses a hyperscan
171 database in streaming mode for matching files.
172 """
173
174 @override
175 @staticmethod
176 def _make_db() -> hyperscan.Database:
177 return hyperscan.Database(mode=hyperscan.HS_MODE_STREAM)
178
179
180 class HyperscanGiR1StreamClosureBackend(_HyperscanGiR1StreamBaseBackend):
181 """
182 The :class:`HyperscanGiR1StreamClosureBackend` class uses a hyperscan database
183 in streaming mode for matching files, and uses a closure to capture state.
184 """
185
186 # Prevent accidental usage.
187 _out: tuple[()]
188
189 @override
190 def match_file(self, file: str) -> tuple[Optional[bool], Optional[int]]:
191 out_include: Optional[bool] = None
192 out_index: Optional[int] = -1
193 out_priority = 0
194
195 def on_match(
196 expr_id: int, _from: int, _to: int, _flags: int, _context: Any,
197 ) -> Optional[bool]:
198 nonlocal out_include, out_index, out_priority
199 expr_dat = self._expr_data[expr_id]
200 index = expr_dat.index
201
202 # Rematch pattern because Hyperscan does not support capture groups.
203 pattern = self._patterns[index]
204 match = pattern.match_file(file)
205
206 # Check for directory marker.
207 dir_mark = match.match.groupdict().get(_DIR_MARK)
208
209 if dir_mark:
210 # Pattern matched by a directory pattern.
211 priority = 1
212 else:
213 # Pattern matched by a file pattern.
214 priority = 2
215
216 # WARNING: Hyperscan does not guarantee matches will be produced in order!
217 include = expr_dat.include
218 if (
219 (include and dir_mark and index > out_index)
220 or (priority == out_priority and index > out_index)
221 or priority > out_priority
222 ):
223 out_include = include
224 out_index = index
225 out_priority = priority
226
227 with self._db.stream(match_event_handler=on_match) as stream:
228 stream.scan(file.encode('utf8'))
229
230 if out_index == -1:
231 out_index = None
232
233 return (out_include, out_index)
234
235
236 # WARNING: This segfaults.
237 class HyperscanGiR1StreamStateBackend(_HyperscanGiR1StreamBaseBackend):
238 """
239 The :class:`HyperscanGiR1StreamStateBackend` class uses a hyperscan database
240 in streaming mode for matching files, and stores state in variables.
241 """
242
243 # Change type hint.
244 _out: tuple[Optional[bool], int, int]
245
246 def __init__(self, patterns: Sequence[RegexPattern]) -> None:
247 super().__init__(patterns)
248 self._out = (None, -1, 0)
249
250 @override
251 def match_file(self, file: str) -> tuple[Optional[bool], Optional[int]]:
252 self._out = (None, -1, 0)
253 with self._db.stream(match_event_handler=self.__on_match, context=file) as stream:
254 stream.scan(file.encode('utf8'))
255
256 out_include, out_index = self._out[:2]
257 if out_index == -1:
258 out_index = None
259
260 return (out_include, out_index)
261
262 @override
263 def __on_match(
264 self,
265 expr_id: int,
266 _from: int,
267 _to: int,
268 _flags: int,
269 context: Any,
270 ) -> Optional[bool]:
271 file: str = context
272 expr_dat = self._expr_data[expr_id]
273 index = expr_dat.index
274
275 # Rematch pattern because Hyperscan does not support capture groups.
276 pattern = self._patterns[index]
277 match = pattern.match_file(file)
278
279 # Check for directory marker.
280 dir_mark = match.match.groupdict().get(_DIR_MARK)
281
282 if dir_mark:
283 # Pattern matched by a directory pattern.
284 priority = 1
285 else:
286 # Pattern matched by a file pattern.
287 priority = 2
288
289 # WARNING: Hyperscan does not guarantee matches will be produced in order!
290 include = expr_dat.include
291 prev_index = self._out[1]
292 prev_priority = self._out[2]
293 if (
294 (include and dir_mark and index > prev_index)
295 or (priority == prev_priority and index > prev_index)
296 or priority > prev_priority
297 ):
298 self._out = (include, index, priority)