perf: Use OpenCV over PIL for PNG encoding in ImageRef.from_pil#562
Open
maxdswain wants to merge 3 commits intodocling-project:mainfrom
Open
perf: Use OpenCV over PIL for PNG encoding in ImageRef.from_pil#562maxdswain wants to merge 3 commits intodocling-project:mainfrom
ImageRef.from_pil#562maxdswain wants to merge 3 commits intodocling-project:mainfrom
Conversation
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.
Overview
The
ImageRef.from_pilclass method is used widely in docling's codebase. It is often used several times per page when parsing documents in thedocling_parse.pdf_parser.PdfDocument._to_bitmap_resources_from_decodermethod. From my profiling, I found that it took up ~45% of processing time when doing aDocumentConverterconversion with all AI models disabled. This led me looking into how it's performance can be improved.The function uses pillow to encode the image to a png, which is notoriously slow. So I swapped it out with opencv, improving the performance of this function by ~55% for this simple test case:
When using these changes in the main docling repo, it reduced by conversion time from 14.2 to 9.21 (~35%) when disabling all AI models.
One caveat is that I did add an extra dependency
opencv-python-headless, however this is already a dependency in the main docling repo'suv.lock.