Skip to content

Skip unnecessary regex matching for emails and URLs - #5408

Open
Shrashti1234 wants to merge 3 commits into
aboutcode-org:developfrom
Shrashti1234:optimize-clue-line-prefilter
Open

Shrashti1234 wants to merge 3 commits into
aboutcode-org:developfrom
Shrashti1234:optimize-clue-line-prefilter

Conversation

@Shrashti1234

@Shrashti1234 Shrashti1234 commented Nov 2, 2026 •

Copy link
Copy Markdown

Fixes #595.

Email and URL extraction currently runs its regular expression on every text line. Add an optional prefilter to find() so email extraction skips lines without @, and URL extraction skips lines containing neither :// nor ..

The URL check is deliberately conservative: scheme-based URLs, including dotless hosts and IP literals, pass through ://; scheme-less www/ftp prefixes and git-style URLs ending in .git pass through the dot check. Existing extraction, filtering, uniqueness, and source line numbers are unchanged. Generic pattern searches retain their existing behavior when no prefilter is supplied.

Add regression tests that check actual detections and regex invocation counts, plus coverage for generic pattern searches. Update the changelog and contributor list.

The first CI run also exposed two pre-existing license metadata failures. A separate follow-up commit registers the missing FSL template URL as an ignorable clue in fsl-1.1_1.RULE and fsl-1.1_2.RULE. The same failures are present in the unmodified base build. Both rules now pass a direct comparison of models.get_ignorables(rule.text) with models.get_normalized_ignorables(rule), matching the CI validator's comparison. Tests remain enabled.

The other failed CI group reported ValueError: signal only works in main thread. The pinned pytest-xdist 2.5.0/execnet 1.9.0 combination can run worker tests outside the main thread. Update those test-only pins to pytest-xdist 3.8.0 and execnet 2.1.1, which use the main_thread_only worker execution model introduced in xdist 3.6. See the upstream changelog. No tests have been disabled.

Validation

Windows 11, CPython 3.14.2:

  • python -m pytest tests/cluecode/test_finder.py -q: 63 passed, 2 expected failures.
  • python -m pytest tests/cluecode/test_plugin_email_url.py tests/cluecode/test_finder_data.py tests/scancode/test_api.py -q -k "email or url": 321 passed.
  • The new email and URL prefilter tests failed on the original implementation because it invoked the regex on irrelevant lines.
  • git diff --check: passed.
  • Ruff passed with D401 excluded; the unchanged _find_tester helper already triggers D401.
  • With the updated test runner: python -m pytest -n 3 tests/cluecode/test_finder.py tests/cluecode/test_plugin_email_url.py tests/cluecode/test_finder_data.py tests/scancode/test_interrupt.py tests/scancode/test_api.py -q -k 'finder or email or url or interrupt': 388 passed, 2 expected failures.
  • Both FSL rules pass direct ignorable-clue validation after the metadata correction; both failed that same comparison beforehand.
  • The full repository test suite was not run locally. Hosted Azure CI subsequently passed all 30 jobs, including the POSIX timeout/scanner group and the previously failing license-validation group.

Hosted CI result

Azure build 19676 completed successfully with 30/30 jobs passed. GitHub reports 32/32 successful checks, including DCO and the aggregate pipeline check, and the documentation build is also successful. This validates the test-runner and rule-metadata corrections alongside the finder optimization.

Performance measurement

Compared against 05a98f66b4b07290fd34325b694aa59069bc3e71 using all 165 Python files under src/, excluding _vendor directories. Loaded the original finder and modified finder into the same interpreter, warmed file/type-detection caches, then alternated three baseline and modified batches for each mode. Each batch called find_emails() and/or find_urls() on every file and materialized the results.

Median batch times:

Mode Before After
Emails 0.440 s 0.209 s
URLs 1.596 s 1.055 s
Both 2.100 s 1.298 s

Every batch produced identical detections and line numbers. These measurements cover finder calls on this source corpus; they are not timings for a full CLI scan or a Linux/LibreOffice corpus.

AI disclosure: OpenAI Codex implemented this change, wrote the tests and PR description, and ran the validation at my request.

Tasks

  • Reviewed contribution guidelines.
  • Descriptive title and linked issue.
  • Focused regression and integration tests passed locally.
  • Unique feature branch and DCO sign-off.
  • Updated CHANGELOG.rst and AUTHORS.rst.
  • Hosted CI passed: 32 GitHub checks plus the documentation build are green on 9a98644857001467329ac74a68689e64c2a95f0b.

Add an optional line prefilter to the shared finder. Skip email matching without @ and URL matching without a scheme separator or dot, preserving generic pattern searches and source line numbers.

Fixes aboutcode-org#595

Assisted-by: OpenAI Codex
Signed-off-by: Shrashti1234 <sharishhtia@gmail.com>
@knowprasad

Copy link
Copy Markdown

Took a look through the changes in \cluecode/finder.py\ and tested the prefilter logic.

Adding the optional \prefilter\ check directly inside the
umbered_text_lines\ loop works cleanly. Because it skips matching without changing how lines are enumerated, line numbers stay completely accurate for the downstream results.

Filtering for @\ before running the email regex and requiring either ://\ or .\ for URLs catches the vast majority of non-matching lines early. Testing this against plain text lines confirms it avoids regex calls as intended, while still keeping coverage for scheme-based URLs without dots (like \http://localhost\) as well as scheme-less hostnames.

The unit tests in \ est_finder.py\ verifying \pattern.findall.call_args_list\ make the skipped invocations easy to verify. Nice work on this fix!

Both reference rules contain the FSL template URL but did not declare it in ignorable_urls. This caused the license_validate_ignorables_2 CI job to fail before the finder optimization as well. Register the detected URL so the metadata agrees with the existing validator.

Assisted-by: OpenAI Codex
Signed-off-by: Shrashti1234 <sharishhtia@gmail.com>
Update pytest-xdist to 3.8.0 and execnet to 2.1.1. The older pinned runner can schedule tests outside the interpreter main thread, causing POSIX SIGALRM timeout tests to fail with ValueError. xdist 3.6 introduced the main_thread_only execution model; 3.8 retains that fix and supports all Python versions used here.

Validated the finder, scanner integration, API email/URL, and interrupt tests with three workers: 388 passed and two expected failures on Windows/Python 3.14.2. POSIX validation is left to the hosted CI jobs.

Assisted-by: OpenAI Codex
Signed-off-by: Shrashti1234 <sharishhtia@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

optimization for extracting email and URLs

2 participants