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

Delay bleach import to optimise import time #6165

Closed
cdeil opened this issue Jan 6, 2024 · 12 comments · Fixed by #6179
Closed

Delay bleach import to optimise import time #6165

cdeil opened this issue Jan 6, 2024 · 12 comments · Fixed by #6179
Milestone

Comments

@cdeil
Copy link
Contributor

cdeil commented Jan 6, 2024

I'm looking a bit into why Python and Panel imports and startup are slow on my Macbook M1 and hacking on Panel apps I have a slow edit - run cycle. I get weird results unrelated to Panel, even import pandas sometimes takes 10-20 seconds and then on further executions just 0.7 seconds - maybe file caching or antivirus or something else - I haven't been able to identify the problem yet.

However, using tuna and these commands:

time PYTHONPROFILEIMPORTTIME=1 python -c 'import panel' 2> panel_import.log
tuna panel_import.log

I see that a few percent of the Panel import time is from

import bleach

where you import https://pypi.org/project/bleach/

Is it feasible / advisable to delay that import and only do it if needed, which I presume is if someone creates a HTML pane with non-default sanitize_html=True?

@philippjfr philippjfr added this to the v1.3.7 milestone Jan 6, 2024
@philippjfr
Copy link
Member

Seems very sensible, yes.

@cdeil
Copy link
Contributor Author

cdeil commented Jan 6, 2024

@philippjfr
Copy link
Member

Could it be the bytecode file (.pyc) generation?

@cdeil
Copy link
Contributor Author

cdeil commented Jan 6, 2024

Could it be the bytecode file (.pyc) generation?

No. The pyc files are only generated once per environment and cached in site-packages, but I see import panel go back from taking 1 second to many seconds with a given environment simply when coming back the next day (or partial degradation after a few hours, it's not deterministic).

@philippjfr
Copy link
Member

Got you, that's pretty bizarre indeed. Does sound like a security scan type thing.

@philippjfr
Copy link
Member

Based on my timing import bleach takes about 31.8 ms on my machine, so I'm a little on the fence about whether making the import lazy is worth it in this case.

@MarcSkovMadsen
Copy link
Collaborator

Wow. That is a lot.

@philippjfr
Copy link
Member

31 ms or the timings reported above?

@MarcSkovMadsen
Copy link
Collaborator

31ms for something only used in edge cases is a lot.

@philippjfr
Copy link
Member

I wouldn't call it an edge case, it's used for all HTML, Markdown and ReactiveHTML based components.

@philippjfr
Copy link
Member

Sorry guess for HTML/Markdown it is behind the sanitize parameter and therefore indeed an edge case but ReactiveHTML is not an edge case.

@hoxbro
Copy link
Member

hoxbro commented Jan 10, 2024

Even though it is not an edge case for ReactiveHTML, I still think we should not do any imports unless they are absolutely necessary.

For a hvplot-only user, it does not make any sense that Bleach is imported.

I also think this will make it possible to remove it from the build packages.

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 a pull request may close this issue.

4 participants