-
-
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
Merged
+143
−18
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
radarhere
reviewed
Jan 19, 2026
…urn early Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com>
hugovk
commented
Jan 19, 2026
radarhere
reviewed
Jan 24, 2026
radarhere
approved these changes
Jan 24, 2026
Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com>
radarhere
reviewed
Jan 24, 2026
radarhere
reviewed
Jan 24, 2026
Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 PNG.
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)