Coverage for archdocs/prefilter.py: 100%

2 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-18 22:03 +0000

1"""Cheap rejection of sources before any regex runs. 

2 

3`str.__contains__` scans with memchr while `re` walks its own bytecode on a virtual 

4machine, so on a miss a substring costs tens of times less than a pattern. Every parser 

5misses on the overwhelming majority of a project's files — celery is mentioned in a module 

6or two and nowhere else — so the miss is what to optimise: first the cheap "is the word in 

7this file at all", and only then the expensive "is it in the right context". 

8 

9Literals have to be a necessary condition for the pattern to match, covering every one of 

10its alternatives: a superfluous literal costs one wasted regex run, a missing one silently 

11costs a feature on the diagram. The source arrives already lowercased, because the patterns 

12are compiled with `IGNORECASE` and a literal has to reject the same sources the pattern would. 

13Every parser lowercases the file for itself: measured against a full scan those copies are a 

14couple of percent, and the alternative is one more argument threaded through every parser. 

15""" 

16 

17 

18def contains_any_literal(lowered_source: str, every_literal: tuple[str, ...], /) -> bool: 

19 return any(one_literal in lowered_source for one_literal in every_literal)