Message357431
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 |
|
Date |
User |
Action |
Args |
2019-11-25 08:32:53 | xtreak | set | recipients:
+ xtreak, cjw296, michael.foord, lisroach, mariocj89, julianhille, marseel |
2019-11-25 08:32:53 | xtreak | set | messageid: <1574670773.43.0.961223325046.issue38895@roundup.psfhosted.org> |
2019-11-25 08:32:53 | xtreak | link | issue38895 messages |
2019-11-25 08:32:53 | xtreak | create | |
|