Skip to content

Improve performance for noncallable attributes #8502

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
merged 1 commit into from
Jun 6, 2022

Conversation

Krukov
Copy link
Contributor

@Krukov Krukov commented May 27, 2022

Description

The issue that this MR trying to fix, was described in the other MR that was not finished #8318
I faced the same problem, python spend most of the time in larges serializers with a lot of nested objects in is_simple_callable.
Based on your comments in mentioned MR, this MR contains only a 2 lines of code to fix this issue.
Here an example of code ( this issue effect almost all fields types):

from timeit import timeit
from rest_framework import serializers


class Comment:
    def __init__(self, email, number=0):
        self.email = email
        self.number = number


class CommentSerializer(serializers.Serializer):
    email = serializers.CharField()
    number = serializers.IntegerField()


comments = [Comment(email='[email protected]') for _ in range(100)]


def serialize():
    serializer = CommentSerializer(comments, many=True)
    return serializer.data


print(timeit('serialize()', number=1000, setup="from __main__ import serialize"))

This MR gives only 1-2% boost for 1 field object serialization, but about 50% boost for large and branched objects with a lot of fields

@tomchristie
Copy link
Member

Okay, yup. Thanks!

@Krukov
Copy link
Contributor Author

Krukov commented Jun 6, 2022

Thanks @tomchristie

Is it any chance that you will release it soon? I mean maybe you can tell how long it will take?

sigvef pushed a commit to sigvef/django-rest-framework that referenced this pull request Dec 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants