-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Lazy import only required plugin: open 2.3-15.6x & save 2.2-9x faster
#9398
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
base: main
Are you sure you want to change the base?
Conversation
…urn early Co-authored-by: Andrew Murray <[email protected]>
| } | ||
|
|
||
|
|
||
| def _load_plugin_for_extension(ext: str | bytes) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also considered calling this _lazy_init to parallel preinit and init, but I don't mind :)
| def _load_plugin_for_extension(ext: str | bytes) -> bool: | |
| def _lazy_init(ext: str | bytes) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, could go either way. I think _load_plugin_for_extension is clearer about its purpose, but _lazy_init highlights the relationship to init and preinit
| ext = filename_ext.decode() if isinstance(filename_ext, bytes) else filename_ext | ||
|
|
||
| # Try loading only the plugin for this extension first | ||
| if not _load_plugin_for_extension(ext): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if not _load_plugin_for_extension(ext): | |
| if not _lazy_init(ext): |
| # Try to load just the plugin needed for this file extension | ||
| # before falling back to preinit() which loads common plugins | ||
| ext = os.path.splitext(filename)[1] if filename else "" | ||
| if not (ext and _load_plugin_for_extension(ext)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if not (ext and _load_plugin_for_extension(ext)): | |
| if not (ext and _lazy_init(ext)): |
preinit + init
When
loading orsaveing an image, if Pillow isn't yet initialised, we call apreinitfunction.This loads five drivers for five popular formats by importing their plugins: BMP, GIF, JPEG, PPM and PPM.
Then we check each of these plugins in turn to see if one will accept it (which usually involves reading at least part of the image data for a magic prefix), and if so, then load it.
If none of these common five match, we call
init, which imports the remaining 42 plugins. We then check each of these for a match.This has been the case since at least PIL 1.1.1 (released in 2000).
Lazy
This is all a bit wasteful if we only need one or two image formats during a program's lifetime. (Longer running ones may need a few more, but unlikely all 47, and in any case speed is less of a worry for long-running programs.)
This PR adds a mapping of common extensions to plugins. Before
preinitandinit, we can do a very cheap lookup, and may save us importing many plugins, and save us trying to see load with many plugins.Of course, we may have an image without an extension, or with the "wrong" extension, but that's fine, I expect it's rare and anyway we'll fallback to the full
preinit->initflow.Benchmarks
Combined
This scripts times the new code:
preinitfirst.initfirst.preinitfirst.initfirst.Python 3.10
Python 3.14
Read
These are hyperfine comparisons between
mainand the new code, and include the overhead of the Python interpreter startup andPIL import Imageetc.Read png (preinit group)
Read webp (init group)
Save
Save png (preinit group)
Save webp (init group)