Repository navigation
Speed up copyright candidate line selection - #5388
NachiketPatil26 wants to merge 1 commit into
Conversation
Copyright detection got noticeably slower on large source trees, with most of the extra time spent in gibberish detection and markup stripping that run on nearly every line. - Check for years and copyright markers before running gibberish detection in is_candidate(). A line without them is never a candidate, so gibberish detection now only runs on the few lines that could be. - Skip the tag splitting in strip_known_markup_from_text() when a line has no "<" or ">", no skipped attribute and no bare tag, since keep_tag() would keep every token and only spaces get normalized. - Do not normalize text twice in detect_gibberish(): ngram() already normalizes it. Detections are unchanged: the scan output for the llvm/lib tree from llvm-project 18.1.8 is byte-identical before and after, and copyright detection on it is about 19% faster. Reference: aboutcode-org#5374 Signed-off-by: Nachiket <nachiketpatil2604@gmail.com>
|
Tested this locally against a sample tree and verified the test suite. The ~20% speedup on candidate selection is a great optimization, and skipping the redundant Two small notes from reading the diff:
Clean patch and solid benchmarks. |
Fixes #5374
Copyright detection spends most of its extra time in gibberish detection and markup stripping, which ran on nearly every line. This keeps the detections exactly the same and skips work that can't change the result:
is_candidate()now checks for years and copyright markers first and only runs gibberish detection on lines that have one. A line without either was never a candidate anyway, so the result is the same.strip_known_markup_from_text()returns early with only whitespace normalized when the line has no<or>, no skipped attribute (href=,@end,mailto:, ...) and none of the bare w3.org tags. In that casekeep_tag()keeps every token, so the full split gives the same string.detect_gibberish()no longer normalizes the line before callingavg_transition_prob(), which normalizes it again throughngram().Results
llvm/libfrom llvm-project 18.1.8 (same corpus as the issue), single process,detect_copyrights()on every file, macOS arm64, Python 3.12:developThe JSON dumps of all detections are byte-identical (142 detections on the full tree).
I also ran the old and new versions of
strip_known_markup_from_text(),detect_gibberish()andis_candidate()side by side on every distinct line ofllvm/lib,tests/cluecode/dataandtests/textcode/data, plus 200k random lines mixing tags,MAILTO:/maİlto:, w3.org URLs and non-ASCII characters (about 2M lines in total): no differences.pytest --test-suite=all tests/cluecode tests/textcode: 5352 passed, 17 xfailed. I added tests for the markup fast path and foris_candidate()still rejecting gibberish lines that have a year and a marker.Tasks