Skip to content

Lazy-import heavy modules for faster startup#219

Open
oxysoft wants to merge 1 commit intoGourieff:mainfrom
oxysoft:lazy-imports
Open

Lazy-import heavy modules for faster startup#219
oxysoft wants to merge 1 commit intoGourieff:mainfrom
oxysoft:lazy-imports

Conversation

@oxysoft
Copy link

@oxysoft oxysoft commented Feb 19, 2026

Summary

  • Move torchvision, insightface, scipy, cv2, segment_anything, ultralytics, and onnxruntime imports from module top-level into function bodies
  • This defers loading until actual use — face detection and swapping work unchanged
  • Import time drops from ~7.6s to ~0.04s during ComfyUI node registration

Details

ComfyUI imports all custom nodes at startup to discover NODE_CLASS_MAPPINGS. The current top-level imports pull in torchvision (~3.7s), insightface + albumentations (~2.0s), and scipy (~1.75s) even before any node is used.

By moving these imports into the methods that actually need them, the node classes register instantly and libraries only load when a user first queues a workflow that uses ReActor.

14 files modified. No functional changes — just import relocation.

Test plan

  • Verify all 15 node classes still appear in ComfyUI node browser
  • Run a face swap workflow to confirm face detection + swapping works
  • Check startup time with python -X importtime or similar

Move torchvision, insightface, scipy, cv2, segment_anything,
ultralytics, and onnxruntime imports from module top-level into
function bodies. This defers loading until actual use, reducing
import time from ~7.6s to ~0.04s during ComfyUI startup.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants