Problem/Motivation
I'm seeing the following when I update a single crop selection on an Image. Since the crop x,y coordinates have changed, ImageWidgetCropManager::updateCropProperties()
is called. If we take a look at this method:
public function updateCropProperties(Crop $crop, array $crop_properties) {
// Parse all properties if this crop have changed.
foreach ($crop_properties as $crop_coordinate => $value) {
// Edit the crop properties if he have changed.
$crop->set($crop_coordinate, $value, TRUE)
->save();
}
}
we can see $crop->set($crop_coordinate, $value, TRUE)
is called for each item in $crop_properties
.
Looking at $crop_properties
, a sample value looks like:
Array
(
[width] => 1055
[height] => 593
[x] => 528
[y] => 1070
)
It appears that the Crop entity is being saved for each item in the array (so 4x). If we take a look at Crop::save()
:
Crop::save() {
parent::save();
image_path_flush($this->uri->value);
}
Thus for each Crop entity save, image_path_flush()
is called. image_path_flush()
looks like:
function image_path_flush($path) {
$styles = ImageStyle::loadMultiple();
foreach ($styles as $style) {
$style->flush($path);
}
}
In summary the side effect of saving the Crop entity for each setting of each of the 4 crop properties results in a load of all Image styles present on the system and a flush of these image styles.
Proposed resolution
When you have only a couple image styles and a couple of crop types, this probably isn't that noticeable. However, if you have more crop types and more image styles the impact of repeatedly flushing all of the image styles can reduce performance.
I'm not sure if it is necessary to save after setting each crop property. I think we can move the Crop::save() in updateCropProperties() to outside of the for loop.
Comment | File | Size | Author |
---|---|---|---|
#2 | reduce_number_of_image-flushes-2811755-2.patch | 571 bytes | mirie |
Comments
Comment #2
mirie CreditAttribution: mirie at Phase2 commentedHere's a patch that moves the save() to outside of the for loop.
Comment #3
woprrr CreditAttribution: woprrr as a volunteer commentedHii @mirie,
Thanks a lot for you analyse and feedback !!!
It's true that case can be optimized !! I just need to verify the real impact in my memories in 8.0-RC we have a limitation with that (needed to save each coordinates one by one ...). i test it fast !!
Comment #4
woprrr CreditAttribution: woprrr as a volunteer commentedAll ok Thanks :)
Comment #6
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedComment #7
woprrr CreditAttribution: woprrr as a volunteer commentedThank @deviantintegral to have switch to fixed :)
Comment #8
woprrr CreditAttribution: woprrr as a volunteer commented