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
Optimize is_dict_like performance when called with a dict instance #207
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a comment here to explain the reason of this "redundant" logic condition? This will prevent (or at least help to prevent) removing isinstance(spec, dict)
in the future
3e8e818
to
ad30090
Compare
@gstarnberger I've merged this CR ... but I've tried to time
According to the above results def is_dict_like(spec):
if isinstance(spec, dict):
return True
return isinstance(spec, Mapping) |
@macisamuele Thanks for merging the PR. As for the tests above: The output of large_pets(200) is a list so your isinstance check with a dict type will always fail and fallback to the more expensive Mapping isinstance check. When Bravado reads a response object most of the arguments to is_dict_like are of type dict and therefore short-circuiting that check is able to increase performance significantly - in some of my tests with larger responses I see an approx 25% reduction in overall time.
|
isinstance(something, Mapping) is very slow in Python (see https://stackoverflow.com/questions/42378726/why-is-checking-isinstancesomething-mapping-so-slow) and most of the time is_dict_like in schema.py is called with an actual dict instance instead of some other type of mapping.
This change optimizes for that common use-case by doing a cheap dict is_instance check and by only executing the more expensive check if it's not a dictionary. This yields an approx 25% performance improvement for some of my test cases that use very large (around 400 kb) JSON responses.