Fix: O(n²) parent lookup in BFS and duplicate URL enqueue in BestFirst - #2244
Open
yashikam19 wants to merge 1 commit into
Open
Fix: O(n²) parent lookup in BFS and duplicate URL enqueue in BestFirst#2244yashikam19 wants to merge 1 commit into
yashikam19 wants to merge 1 commit into
Conversation
…estFirst
- BFSDeepCrawlStrategy: build a per-level {url: parent} dict once instead of
linearly re-scanning current_level for every fetched result
(_arun_batch and _arun_stream)
- BestFirstCrawlingStrategy: track a set at enqueue time, since
is only populated when a URL is dequeued/processed — without it,
a URL discovered via multiple parent pages before being crawled could be
pushed onto the priority queue more than once, wasting a scorer call and a
queue slot
Fixes unclecode#2242
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.
Fixes #2242
Summary
Fixes two related inefficiencies in the deep-crawl strategies found while
integrating
BestFirstCrawlingStrategyfor site-wide crawling:BFSDeepCrawlStrategy._arun_batch/_arun_streamre-scanned the entirecurrent_levellist to find each result's parent URL - O(n²) for a levelof size n. Replaced with a dict built once per level (
dict(current_level)),O(n).
BestFirstCrawlingStrategyonly trackedvisited(updated at dequeuetime), so a URL discovered via multiple parent pages before being crawled
could be pushed onto the priority queue more than once - wasted scorer
calls and queue slots. Added a
queuedset updated at enqueue time toprevent this.
Both are internal-only fixes - no behavior or public API change, same outputs.
Files Changed
crawl4ai/deep_crawling/bfs_strategy.py- buildparent_by_urldict onceper level in
_arun_batchand_arun_stream, instead of a linear scan perresult.
crawl4ai/deep_crawling/bff_strategy.py- added aqueued: Set[str]trackedalongside
visited, checked/updated at the point a URL is pushed onto thepriority queue (both fresh-start and resume-from-state paths).
How Has This Been Tested?
pytest tests/deep_crawling/ tests/general/test_deep_crawl*.py: 70 passed,0 regressions
actual
sitemap.xml(1000 URLs, one legitimate request, no crawling) andtimed the parent-lookup code directly - 11.69ms (old) → 0.08ms (new), ~145x.
Checklist: