⚡️ Speed up _get_child_links_recursive()
by 45% in embedchain/loaders/docs_site_loader.py
#1266
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.
Description
📄
_get_child_links_recursive()
inembedchain/loaders/docs_site_loader.py
📈 Performance went up by
45%
(0.45x
faster)⏱️ Runtime went down from
1489.11μs
to1029.61μs
Explanation and details
(click to show)
To improve the performance of this program, you could apply a few changes:
Add a check at the start of the
_get_child_links_recursive()
to return early if theurl
has already been visited. This avoids the overhead of starting an unnecessary HTTP request which is expensive in terms of time.Use
soup.find_all("a", href=True)
to only select anchors with href attribute, this will reduce computation time.Use a generator expression instead of list comprehension for
child_links
andabsolute_paths
to save memory by generating items one at a time, instead of creating the entire list in memory.Instead of looping through absolute_paths and adding links to visited_links inside function
_get_child_links_recursive
, we can use set operation to add all links at once which would be faster.At the end, instead of calling the recursive function one link at a time, we can use a list comprehension to start the recursion for all links.
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
The new optimized code was tested for correctness. The results are listed below.
✅ 4 Passed − 🌀 Generated Regression Tests
(click to show generated tests)
Checklist:
Maintainer Checklist