Karthikeyan Singaravelan <tir.kar...@gmail.com> added the comment:

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

----------
nosy: +cjw296, lisroach, mariocj89, michael.foord

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue38895>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to