Skip to content
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

contrib.rest_framework.PictureField serialization is slow #144

Closed
amureki opened this issue Jan 11, 2024 · 1 comment
Closed

contrib.rest_framework.PictureField serialization is slow #144

amureki opened this issue Jan 11, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@amureki
Copy link
Collaborator

amureki commented Jan 11, 2024

Greetings Johannes!

While doing bulk operations with images that involved this library, I noticed a noticeable difference in the serialization time compared to the standard DRF library.
Here is a quick snippet that I am trying:

import time
from django.db import models

from pictures.contrib.rest_framework import PictureField as PictureFieldRest
from rest_framework.fields import ImageField as DRFImageField

class MyPicture(models.Model):
    image = PictureField(
        aspect_ratios=["3/2", "1/1", "15/2"],
        file_types=["WEBP", "JPEG"],
        width_field="image_width",
        height_field="image_height",
    )
    image_width = models.PositiveSmallIntegerField(editable=False)
    image_height = models.PositiveSmallIntegerField(editable=False)


def test_serialization_speed():
    items_count = 5000

    start = time.time()
    serialized = []
    for item in MyPicture.objects.all()[:items_count]:
        serialized.append(
            DRFImageField().to_representation(item.image)
        )
    print("DRF ImageField serialization TIME: ", time.time() - start)

    start = time.time()
    serialized = []
    for item in MyPicture.objects.all()[:items_count]:
        serialized.append(
            PictureFieldRest().to_representation(item.image)
        )
    print("PictureField serialization TIME: ", time.time() - start)

The results on my dev machine (MacBook M1, 16 Gb) are the following:

DRF ImageField serialization TIME:  0.11259627342224121
PictureField serialization TIME:  11.82898998260498

I believe that the issue comes from the serialization of this part:
https://github.com/codingjoe/django-pictures/blob/main/pictures/contrib/rest_framework.py#L25-L38

We have plenty of combinations of aspect ratios and containers, each of them has to be serialized from SimplePicture object to its url, which leads to a heavy CPU-bound operation.
However, I cannot yet come up with the solution to that problem.
Would you have any ideas on this?

Best,
Rust

@codingjoe codingjoe added the bug Something isn't working label Jan 14, 2024
@codingjoe
Copy link
Owner

codingjoe commented Jan 14, 2024

@amureki, as a see it, we currently render the ratios part of the payload twice if you declare a specific ratio. We first serialize everything and overwrite it later. Avoiding this could resolve in a considerable performance boost. Would you care to provide a patch?

amureki added a commit to amureki/django-pictures that referenced this issue Jan 23, 2024
amureki added a commit to amureki/django-pictures that referenced this issue Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants