Mark a URL when you find it, not when you fetch it
One line in that loop decides how often a page gets fetched: the moment you mark it as seen. Take the 41-page site: the home page links p0 to p39, and each p links to the next one.
Predict
Mark each URL when you fetch it instead of when you find it. On the 41-page site, with one worker, how many fetches?
Here is each fetch, both ways:
Fetch
Mark when fetched
Mark when found
home
queue p0 to p39
queue p0 to p39
p0
p1 isn't marked yet, so queue p1 again
p1 is marked: skip
p1
queue p2 again
p2 is marked: skip
in all
80 fetches for 41 pages
41 fetches for 41 pages
The line that matters most is where seen.add sits. Mark a URL when you find it, before it goes into the frontier, and nothing gets queued twice. Mark it when you fetch it instead, and every link to a page that's still waiting queues that page again.
With eight workers, marking at fetch time cost 62 to 68 fetches, varying from run to run. The tests catch it, because each page must be fetched exactly once.
In the editor, finish nearest(start, fetch, max_pages): the crawl loop under a budget. It fetches at most max_pages pages, nearest the start first, and marks each link when it finds it.
Pick an answer above to see the rest
Instructions
1
Finish nearest(start, fetch, max_pages): fetch at most max_pages pages, nearest the start first, and return them in the order you fetched them. Mark each link when you find it, so no page is fetched twice. The links here are already clean, so skip normalizing.