This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author xtreak
Recipients cjw296, julianhille, lisroach, mariocj89, marseel, michael.foord, xtreak
Date 2019-11-25.08:32:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1574670773.43.0.961223325046.issue38895@roundup.psfhosted.org>
In-reply-to
Content
Thanks Marcel for the pointer. I can confirm the performance impact. This occurs in the common case where not being an AsyncMock the signature of NonCallableMock.__init__ is created every time and then bind_partial is used to detect the spec being supplied to be async. It seems creating the signature of NonCallableMock.__init__ per mock creation is expensive and since it doesn't change can we just create the signature once and set it as a module level attribute? There might still be room for some more optimisations here to reduce the impact.

$ python3.7 -m timeit -s 'from unittest.mock import Mock' 'Mock()'
20000 loops, best of 5: 17.6 usec per loop

# Creating signature object per run (Python 3.8.0)

$ ./python.exe -m timeit -s 'from unittest.mock import Mock' 'Mock()'
2000 loops, best of 5: 109 usec per loop

# Set the signature object of NonCallableMock.__init__ as a private module level attribute (Python 3.8.0)

./python.exe -m timeit -s 'from unittest.mock import Mock' 'Mock()'
5000 loops, best of 5: 66.4 usec per loop
History
Date User Action Args
2019-11-25 08:32:53xtreaksetrecipients: + xtreak, cjw296, michael.foord, lisroach, mariocj89, julianhille, marseel
2019-11-25 08:32:53xtreaksetmessageid: <1574670773.43.0.961223325046.issue38895@roundup.psfhosted.org>
2019-11-25 08:32:53xtreaklinkissue38895 messages
2019-11-25 08:32:53xtreakcreate