Skip to content

Enh: Defer expensive loading of dask.array#2276

Merged
andrewgsavage merged 5 commits intohgrecco:masterfrom
psobolewskiPhD:lazy_dask
Mar 2, 2026
Merged

Enh: Defer expensive loading of dask.array#2276
andrewgsavage merged 5 commits intohgrecco:masterfrom
psobolewskiPhD:lazy_dask

Conversation

@psobolewskiPhD
Copy link
Contributor

@psobolewskiPhD psobolewskiPhD commented Feb 22, 2026

  • Closes Lazy import of compat layer optional dependencies #2265
    • this isn't a comprehensive solution for every array type, but addresses the big one dask.array
  • Executed pre-commit run --all-files or pixi run lint --all-files with no errors
    • I ran linting manually, don't have pixi handy
  • The change is fully covered by automated unit tests
  • Documented in docs/ as appropriate
    • NA?
  • Added an entry to the CHANGES file
    • will do so if this seems like something y'all are on board with

This is a performance enhancement, aimed at the import time of pint.
As noted int he issue, dask.array is a expensive import, so in this PR I attempt to address the issue by deferring it's loading from import pint (dask/dask.base in contrast are pretty light, so I leave them alone to keep this simple.)
With this PR (all are second runs)

  • python -X importtime -c 'import pint' bottom line:
    • import time: 649 | 176212 | pint
  • python -X importtime -c 'import pint' 2>&1 | grep dask.array
    • nothing
  • python -X importtime -c 'import pint' 2>&1 | grep dask
    • shows dask.base as 31 ms on my machine

With main:

  • python -X importtime -c 'import pint' bottom line:
    • import time: 630 | 465967 | pint
  • python -X importtime -c 'import pint' 2>&1 | grep dask.array
    • import time: 244 | 278716 | dask.array
  • python -X importtime -c 'import pint' 2>&1 | grep dask.base
    • shows dask as 31 ms

With this PR in pint we can tuck some import dask.array as da imports in napari into functions and save ~250 ms on our startup time.

Edit: this has a nice side-effect of also resulting in import pint not importing pandas.

@andrewgsavage
Copy link
Collaborator

looks good, do add a changes note

@andrewgsavage
Copy link
Collaborator

doctest fail looks unrelated

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 23, 2026

Merging this PR will not alter performance

✅ 448 untouched benchmarks


Comparing psobolewskiPhD:lazy_dask (fbfe9bc) with master (86f37c0)

Open in CodSpeed

@psobolewskiPhD
Copy link
Contributor Author

looks good, do add a changes note

Thanks! Done! I wasn't sure if I should reference the issue or the PR in the parens. I went with the issue number?
Happy to edit it or please feel free to push.

@psobolewskiPhD
Copy link
Contributor Author

Oh interesting, it looks like dask.array was also pulling in pandas.
With this branch:
python -X importtime -c 'import pint' 2>&1 | grep pandas

  • nothing
    With main:
    import time: 183 | 130141 | pandas

@psobolewskiPhD
Copy link
Contributor Author

psobolewskiPhD commented Feb 26, 2026

The lint fail appears to be due to pixi version?
https://github.com/hgrecco/pint/actions/runs/22461640461/job/65057385529?pr=2276#step:4:6
Do you want me to try to fix that here? or another PR?
I'm not a pixi user, but I can read docs and give it a shot -- the error seems clear enough, regarding the change from project to workspace

@andrewgsavage andrewgsavage merged commit ea5a863 into hgrecco:master Mar 2, 2026
54 of 55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lazy import of compat layer optional dependencies

2 participants