Only call text_layout once in getmask2 #7206
Merged
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.
Helps #6618
Backstory
The following is an earlier version of
ImageFont
'sgetmask2()
, as described in the quoted comment.Pillow/src/PIL/ImageFont.py
Lines 752 to 777 in 3cfdef3
#6618 (comment)
#7059 removed the deprecated fill parameter. So here is the current version of
ImageFont
'sgetmask2()
.Pillow/src/PIL/ImageFont.py
Lines 554 to 575 in 1fc8d82
This is the only place that
font.render
is called.Change
So I added a change to make
font.getsize
a builtin part offont.render
, meaning thattext_layout
is not called once by each, but only once.From my tests, this causes
getmask2
to be 10% faster.There is an awkward part of this change though - the _imagingft extension is not connected to the C code for creating new images. I couldn't call
ImagingNewDirty
andImagingFill
. Despite the quoted comment's expectation that this could all be done in C, it didn't realise that our C code is actually split up into these extensions. Instead, I've passedImage.core.fill
into C, to then be called byPyObject_CallFunction
.