Comments (7)
@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.
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.
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.
That's great! Very interesting...
Thanks again!
from marshmallow.
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.
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.
@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)
- Are nested options applied correctly? HOT 1
- [RFC] Add BigInt, SmallInt,... fields. HOT 3
- RecursionError: maximum recursion depth exceeded while calling a Python object HOT 1
- Question - Is it possible to ignore fields on dump? HOT 4
- sdist is missing `tox.ini` HOT 4
- Validation does not occur on serialization documentation issue HOT 2
- Dot in key is not parsed correclty on loads() HOT 3
- The data_key argument for one or more fields collides with another field's name or data_key argument. HOT 2
- Serialize data objects in Schema HOT 1
- Raise error for valid e-mail
- Is this safer than Pickle? HOT 1
- Wrong value in __call__() function of validate.Validator HOT 1
- Validation of required nested field is not consistent with previous version
- Create raw list Schema HOT 1
- The function decorated by the "post_load" decorator will undergo changes in parameter types as the function name changes. HOT 1
- Deprecate __version__ and related attributes
- Address deprecation warnings in tests HOT 1
- URL validation fails if URL is missing user field HOT 5
- fields.URL validation error should specify "not a valid FQDN" if trying to pass hostname without domain
- missing `f` for f-Strings HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from marshmallow.