GithubHelp home page GithubHelp logo

Comments (7)

sloria avatar sloria commented on May 20, 2024 3

@mgd722 I haven't compared the two usages in a while, but they should be similar. If you're serializing ORM objects, I'd first look into your relationship loading technique and make sure you're not running into the n+1 problem.

from marshmallow.

sloria avatar sloria commented on May 20, 2024 1

The deepcopy operation is expensive, but necessary, so that serializers can store errors from nested serializers.

I did a little work with cProfile and your code above (the gist of the script is here) and found 2 significant speedups:

  • Passing an instance--not a class--into a Nested field.

Example:

collaborators = fields.Nested(UserSerializer(), many=True)

instead of

collaborators = fields.Nested(UserSerializer, many=True)

This avoids repeating the initialization code (including the deepcopy) for each collaborator. In the future, it'll be better to cache the nested serializer object, or disallow passing classes altogether.

  • Overriding __deepcopy__ method of field objects so they are only shallow copied (9c0f062). Even though the declared_fields dictionary must be deep-copied, field objects themselves don't need to be deep-copied.

These two modifications decreased the execution time of the above script by almost half.

Thanks for reporting this. I will continue to do more profiling and see where performance can be improved even further.

from marshmallow.

sloria avatar sloria commented on May 20, 2024

I underestimated the effect of passing in an instance into a nested Field: doing this for both the "user" and the "collaborators" field reduces the total runtime of the profiling script from ~5.7s to ~1.6s.

class BlogSerializer(Serializer):
    title = fields.String()
    user = fields.Nested(UserSerializer())
    collaborators = fields.Nested(UserSerializer(), many=True)
    categories = fields.List(fields.String)
    id = fields.String()

from marshmallow.

sloat avatar sloat commented on May 20, 2024

That's great! Very interesting...

Thanks again!

from marshmallow.

sloria avatar sloria commented on May 20, 2024

Glad I could help.

I've made some further improvements so that performance should be good whether you pass in a Serializer class or a Serializer object into a Nested field.

from marshmallow.

andras-tim avatar andras-tim commented on May 20, 2024

I have same issue with serialize, and I was run into this issue.

I think, you should update the documentation http://marshmallow.readthedocs.org/en/latest/nesting.html about use instance instead of class passing trough.

from marshmallow.

mgd722 avatar mgd722 commented on May 20, 2024

@sloria, just to be clear-- performance should now be similar in both of the following scenarios:

fields.Nested(UserSerializer(), many=True)
fields.Nested(UserSerializer, many=True)

so if nested fields are running slow for me it's just my shitty code?

from marshmallow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.