perf(image-redactor): index analyzer bboxes by position in get_pii_bboxes - #2251
Open
SiddhaBasu wants to merge 1 commit into
Open
SiddhaBasu wants to merge 1 commit into
SiddhaBasu wants to merge 1 commit into
Conversation
…oxes get_pii_bboxes rescanned the full analyzer_bboxes list for every ocr_bbox to find a positional match, an O(ocr_bboxes x analyzer_bboxes) scan. Build a dict keyed by (left, top, width, height) once instead, turning the lookup into O(n + m). No behavior change: dict.setdefault preserves the original loop's first-match-wins semantics when multiple analyzer bboxes share a position, verified by a new duplicate-position test and an equivalence test against the original O(n*m) logic on 500 bboxes. This affects ImagePiiVerifyEngine.verify() and DicomImagePiiVerifyEngine, the verification/visualization tooling that annotates images with detected-PII bounding boxes; the core redact() path does not call this method.
SiddhaBasu
force-pushed
the
perf/image-analyzer-bbox-matching
branch
from
September 12, 2026 18:30
ec75e6b to
7527f94
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change Description
ImageAnalyzerEngine.get_pii_bboxesrescanned the entireanalyzer_bboxeslist for everyocr_bboxto find a positional match (left/top/width/heightequality), an O(len(ocr_bboxes) × len(analyzer_bboxes)) scan. This builds a dict keyed by(left, top, width, height)fromanalyzer_bboxesonce up front, so eachocr_bboxis resolved with a single O(1) dict lookup instead — O(n) + O(m) total.No behavior change. The original loop used
breakon the first matchinganalyzer_bbox, so when multiple analyzer bboxes shared a position, the first one in list order always won. A naivedict[key] = valuebuild would flip that to last-wins, so this usesdict.setdefault(key, analyzer_bbox)to keep identical first-match semantics. Verified with:Scope note:
get_pii_bboxesis called fromImagePiiVerifyEngine.verify()andDicomImagePiiVerifyEngine— the verification/visualization tooling that draws PII/non-PII bounding boxes on an image for inspection. The coreImageRedactorEngine.redact()path does not call this method, so this improves the verify/QA tooling, not the redaction pipeline itself.Issue reference
N/A — found while auditing
presidio-image-redactorfor nested-loop hotspots; no existing issue tracked this.Testing
pytest tests/test_image_analyzer_engine.py: 5 existingget_pii_bboxestests pass, plus 3 new tests added.pytest tests/test_dicom_image_pii_verify_engine.py(a direct caller of this method): unchanged pass/fail counts before and after this change.presidio-image-redactorsuite: identical failure/error counts before and after this change (confirmed viagit stash); only difference is the added tests passing.ruff check/ruff format --checkon the changed source file: clean.coverage.pyinstrumentation crashes on an unrelated torch/thinc import ordering issue in my local environment, reproducible on unmodifiedmainas well. Every branch touched by this diff (dict build, match found, no match, duplicate-position tie-break) is exercised by the test suite.Checklist