Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove demandimport module #121

Merged
merged 2 commits into from
Oct 26, 2023

Conversation

HarryMichal
Copy link
Contributor

The demandimport module relies on the imp module which has been deprecated in Python 3.4[0] and then removed in Python 3.12[1]. Remove it as it is no longer maintained (last commit in 2019), the performance benefit is marginal and optional imports can be managed in alternative ways (such as using try/expect or by utilising a build system).

[0] https://bugs.python.org/issue17177
[1] https://docs.python.org/dev/whatsnew/3.12.html#imp
[2] bwesterb/py-demandimport@ca9c116

The demandimport module relies on the imp module which has been
deprecated in Python 3.4[0] and then removed in Python 3.12[1]. Remove
it as it is no longer maintained (last commit in 2019), the performance
benefit is marginal and optional imports can be managed in alternative
ways (such as using try/expect or by utilising a build system).

[0] https://bugs.python.org/issue17177
[1] https://docs.python.org/dev/whatsnew/3.12.html#imp
[2] bwesterb/py-demandimport@ca9c116
@JiriPavela
Copy link
Collaborator

Thanks for the PR. We used demandimport not only for optional dependencies (for which try/except is sufficient), but also for lazy importing to improve Perun startup time. That being said, lazy imports were not solved systematically which explains the still too long startup time.

I was aware that demandimport is deprecated, however, we have not found a replacement for lazy imports yet. PEP 690 seemed like it might solve our issue – until it was rejected. So for now, I think removing demandimport is the right move (to enable 3.12 support) and the next step should be finding a suitable alternative for lazy imports.

@tfiedor
Copy link
Collaborator

tfiedor commented Oct 9, 2023

Thanks for the PR. We used demandimport not only for optional dependencies (for which try/except is sufficient), but also for lazy importing to improve Perun startup time. That being said, lazy imports were not solved systematically which explains the still too long startup time.

I was aware that demandimport is deprecated, however, we have not found a replacement for lazy imports yet. PEP 690 seemed like it might solve our issue – until it was rejected. So for now, I think removing demandimport is the right move (to enable 3.12 support) and the next step should be finding a suitable alternative for lazy imports.

I believe, the main issue of the slow startup was bokeh and bkcharts, which we graciously removed, so we could look at the startup again.

@HarryMichal
Copy link
Contributor Author

It's better to have concrete values, so let's see.

Before this patch:

; time perun
real    0m3,811s
user    0m3,410s
sys     0m0,812s

importtime-pre.txt

After this patch:

; time perun
real    0m3,546s
user    0m3,301s
sys     0m0,804s

importtime-post.txt

The star-up timing values need to be taken with a pinch of salt as they are dependent on so much but what they show is that removing demandimport does not cause a regression in the start-up time.

The importtime outputs are much more interesting, though. The biggest offenders seem to be:

  • angr - ~1.3 second (unicorn takes up ~0.5 second of that mainly due to using distutils which pulls in pkg_resources).
  • matplotlib - ~0.4 second
  • scipy - ~0.4 second
  • pandas - ~0.2 second
  • numpy - ~0.1 second

Of these only angr seems to have some low-hanging fruit to boost import time.

@JiriPavela
Copy link
Collaborator

JiriPavela commented Oct 24, 2023

Really interesting results, I believe that demandimport originally helped a bit with the startup time but it's quite possible that since it wasn't properly maintained since python 3.7, it might not be lazily importing in some scenarios anymore.

Let's get rid of demandimport for now and solve lazy imports later on (#127).

@HarryMichal
Copy link
Contributor Author

I believe that demandimport originally helped a bit with the startup time but it's quite possible that since it wasn't properly maintained since python 3.7, it might not be lazily importing in some scenarios anymore.

I think it did that but the point of the lazy import is to do it uniformly. One stray import will regress the import time again. E.g., the only times this PR touches imports of bokeh is in perun/utils/view_helpers.py and perun/view/scatter/factory.py but it is also imported in perun/utils/bokeh_helpers.py. The same applies to pandas and possibly even the others.

Copy link
Collaborator

@tfiedor tfiedor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes seems fine, I will look at the checks as well, before merging (and before Jirka adds his own review).

After the removal of demandimport, the BPF engine of Tracer was
being eagerly imported. However, the underlying bcc package is
only an optional requirement and its absence should not crash
Perun.
@codecov
Copy link

codecov bot commented Oct 26, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (ba11799) 98.04% compared to head (6d5ec6b) 98.02%.
Report is 1 commits behind head on devel.

Additional details and impacted files
@@            Coverage Diff             @@
##            devel     #121      +/-   ##
==========================================
- Coverage   98.04%   98.02%   -0.02%     
==========================================
  Files         129      129              
  Lines        8686     8672      -14     
==========================================
- Hits         8516     8501      -15     
- Misses        170      171       +1     
Flag Coverage Δ
coverage-3.10 97.91% <100.00%> (-0.09%) ⬇️
coverage-3.11 97.97% <100.00%> (+0.28%) ⬆️
coverage-3.9 97.86% <100.00%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
perun/fuzz/interpret.py 99.09% <100.00%> (-0.02%) ⬇️
perun/logic/store.py 98.41% <100.00%> (-0.03%) ⬇️
perun/profile/convert.py 98.18% <100.00%> (-0.07%) ⬇️
perun/utils/view_helpers.py 85.18% <100.00%> (-0.53%) ⬇️
perun/view/bars/factory.py 100.00% <100.00%> (ø)
perun/view/flow/factory.py 97.43% <100.00%> (-0.13%) ⬇️
perun/view/scatter/factory.py 98.91% <100.00%> (-0.03%) ⬇️

... and 4 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@JiriPavela JiriPavela left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the bcc import not being optional and the tests are passing now. I think we're ready for a merge.

@tfiedor tfiedor merged commit 607666e into Perfexionists:devel Oct 26, 2023
8 of 9 checks passed
@HarryMichal HarryMichal deleted the modules/remove-demandimport branch October 26, 2023 12:35
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.

3 participants