[Python-ideas] Adding bytes.frombuffer() constructor
Nick Coghlan
ncoghlan at gmail.com
Sun Aug 7 11:47:36 EDT 2016
On 7 August 2016 at 15:08, Michael Selik <michael.selik at gmail.com> wrote:
>
>
> On Sat, Aug 6, 2016 at 8:45 PM INADA Naoki <songofacandy at gmail.com> wrote:
>
>> 1. bytes(bytearray[:n])
>> 2. bytes(memoryview(bytearray)[:n])
>>
>> (1) is simplest, but it produces temporary bytearray having n bytes.
>>
>
> Does that actually make the difference between unacceptably inefficient
> performance and acceptably efficient for an application you're working on?
>
>
>> While (2) is more efficient than (1), it uses still temporary memoryview
>> object, and it looks bit tricky.
>>
>
> Using the memoryview is nicely explicit whereas ``bytes.frombuffer`` could
> be creating a temporary bytearray as part of its construction.
>
It could, but it wouldn't (since that would be pointlessly inefficient).
The main question to be answered here would be whether adding a dedicated
spelling for "bytes(memoryview(bytearray)[:n])" actually smooths out the
learning curve for memoryview in general, where folks would learn:
1. "bytes(mybytearray[:n])" copies the data twice for no good reason
2. "bytes.frombuffer(mybytearray, n)" avoids the double copy
3. "bytes(memoryview(mybytearray)[:n])" generalises to arbitrary slices
With memoryview being a builtin, I'm not sure that argument can be made
successfully - the transformation in going from step 1 direct to step 3 is
just "wrap the original object with memoryview before slicing to avoid the
double copy", and that's no more complicated than using a different
constructor method.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160808/10ddaaf9/attachment-0001.html>
More information about the Python-ideas
mailing list