umap icon indicating copy to clipboard operation
umap copied to clipboard

Lazy import for pynndescent

Open exrhizo opened this issue 7 months ago • 1 comments

Hello Leland,

How do you feel about doing something like this?

pynndescent is adding ~5 seconds on my m1 mac from import when not using umap yet, only importing it for types.

I used py-spy to clarify that most of the umap import time is from, if you like I can message you the json and how to web inspect it.

This helps my developer experience, and it adds some complexity.

Lazy Import Implementation:

  • Replaced direct imports of NNDescent, pynn_named_distances, and pynn_sparse_named_distances with lazy loading
  • Created _lazy_import_pynndescent() function that imports these modules only when needed
  • Updated all 8 usage locations in the code to use the lazy import function

exrhizo avatar Jun 17 '25 15:06 exrhizo

This is potentially useful, although it does result in longer runtimes for umap as the lazy loading occurs at runtime. This may be a little unexpected for some users. Let me think about this and see if I can work out what the right trade-off is here.

lmcinnes avatar Jun 17 '25 16:06 lmcinnes