perf: Defer heavy imports in ServiceLocator to reduce import time#1836
Open
Lidang-Jiang wants to merge 1 commit intoapify:masterfrom
Open
perf: Defer heavy imports in ServiceLocator to reduce import time#1836Lidang-Jiang wants to merge 1 commit intoapify:masterfrom
Lidang-Jiang wants to merge 1 commit intoapify:masterfrom
Conversation
Move top-level imports of Configuration, EventManager, LocalEventManager, FileSystemStorageClient, StorageClient, and ServiceConflictError into the methods where they are actually used. These modules are now only imported on first access rather than at module load time. This reduces `import crawlee` time by ~73% (0.40s -> 0.11s) by deferring the import of pyee, pydantic-based Configuration, and storage client initialization until they are actually needed. The ServiceLocator docstring already states "All services are initialized to its default value lazily" - this change makes the imports match that design intent. Ref apify#1253
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.
Summary
Ref #1253 — Importing the crawler class takes over 2 seconds.
This is a partial optimization that addresses the
ServiceLocatormodule's contribution to slow import times. The_service_locator.pymodule was eagerly importing heavy dependencies (Configuration,EventManager,LocalEventManager,FileSystemStorageClient,StorageClient,ServiceConflictError) at module level, even though these are only needed when services are first accessed.Changes:
Configuration,EventManager,LocalEventManager,FileSystemStorageClient,StorageClient, andServiceConflictErrorimports from module level into the methods where they are usedTYPE_CHECKINGguards for type annotations that reference these classesImpact:
import crawleeis ~73% faster (0.40s → 0.11s)from crawlee.crawlers import ParselCrawlerimport is not significantly affected because_basic_crawler.pyeagerly imports many of the same modules — further optimization of that module could be a follow-upThe
ServiceLocatordocstring already states "All services are initialized to its default value lazily" — this change makes the imports consistent with that design intent.Before
After
Test results (113 tests pass)
Test plan
test_service_locator.pytests passruff checkpassesimport crawleebenchmark shows ~73% improvement