classification
Title: Optimize bytearray(int) constructor to use calloc()
Type: performance Stage:
Components: Versions: Python 3.5
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: pitrou, skrah, vstinner
Priority: normal Keywords:

Created on 2014-06-02 20:25 by vstinner, last changed 2015-03-18 13:28 by vstinner. This issue is now closed.

Messages (6)
msg219632 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-06-02 20:25
Python 3.5 has a new PyObject_Calloc() function which can be used for fast memory allocation of memory block initialized with zeros.

I already implemented an optimization, but Stefan Krah found issues in my change:
http://bugs.python.org/issue21233#msg217826

I reverted the optimization in the changeset dff6b4b61cac.
msg219633 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-06-02 20:26
Stefan also wrote:

"3) Somewhat similarly, I wonder if it was necessary to refactor
   PyBytes_FromStringAndSize(). I find the new version more difficult
   to understand."

This issue can also be addressed here.
msg219636 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-06-02 20:30
Stefan wrote:
"3) Somewhat similarly, I wonder if it was necessary to refactor
   PyBytes_FromStringAndSize(). I find the new version more difficult
   to understand."

Do you mean that the optimization is useless or that the implementation should be changed?
msg219672 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-06-03 09:08
Responding to a comment in issue21233:

>    Before:
>    =======
>        >>> x = bytearray(0)
>        >>> m = memoryview(x)
>        >>> x.__init__(10)
>        Traceback (most recent call last):
>          File "<stdin>", line 1, in <module>
>        BufferError: Existing exports of data: object cannot be re-sized

I don't think such use cases are supported. Generally, reinitializing an 
object by calling __init__() explicitly is not well-defined, except when 
advertised explicitly in the documentation. The only property we should 
guarantee here is that it doesn't lead to an inconsistent object state, or to 
hard crashes. Raising an exception is fine, and changing the raised
exception to another one should be fine as well.
msg223642 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-07-22 07:49
See also issue #22030 (set).
msg238434 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-03-18 13:28
I'm not interested to work on this optimization, so I just close the issue.
History
Date User Action Args
2015-03-18 13:28:53vstinnersetstatus: open -> closed
resolution: out of date
messages: + msg238434
2014-07-22 07:49:22vstinnersetmessages: + msg223642
2014-06-03 09:08:10pitrousetnosy: + pitrou
messages: + msg219672
2014-06-02 20:30:27vstinnersetmessages: + msg219636
2014-06-02 20:26:51vstinnersetmessages: + msg219633
2014-06-02 20:25:58vstinnercreate