Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rotate{left,right} methods to bytearray #47739

Closed
pitrou opened this issue Aug 1, 2008 · 10 comments
Closed

add rotate{left,right} methods to bytearray #47739

pitrou opened this issue Aug 1, 2008 · 10 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@pitrou
Copy link
Member

pitrou commented Aug 1, 2008

BPO 3489
Nosy @rhettinger, @terryjreedy, @josiahcarlson, @pitrou, @ethanfurman, @serhiy-storchaka

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2013-05-18.16:04:53.633>
created_at = <Date 2008-08-01.17:57:57.741>
labels = ['interpreter-core', 'type-feature']
title = 'add rotate{left,right} methods to bytearray'
updated_at = <Date 2013-05-18.16:04:53.632>
user = 'https://github.com/pitrou'

bugs.python.org fields:

activity = <Date 2013-05-18.16:04:53.632>
actor = 'terry.reedy'
assignee = 'none'
closed = True
closed_date = <Date 2013-05-18.16:04:53.633>
closer = 'terry.reedy'
components = ['Interpreter Core']
creation = <Date 2008-08-01.17:57:57.741>
creator = 'pitrou'
dependencies = []
files = []
hgrepos = []
issue_num = 3489
keywords = []
message_count = 10.0
messages = ['70579', '70909', '70910', '70915', '70916', '87954', '113438', '189494', '189500', '189502']
nosy_count = 6.0
nosy_names = ['rhettinger', 'terry.reedy', 'josiahcarlson', 'pitrou', 'ethan.furman', 'serhiy.storchaka']
pr_nums = []
priority = 'low'
resolution = 'rejected'
stage = None
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue3489'
versions = ['Python 3.3']

@pitrou
Copy link
Member Author

pitrou commented Aug 1, 2008

While tweaking the BufferedWriter implementation it came to me that it
would be useful to have rotate_left and rotate_right methods on
bytearray, so as to rotate the array by a number of bytes without any
wasteful memory allocations and copies.

(or, if memoryview is one day implemented it could be a memoryview
method instead...)

@pitrou pitrou added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Aug 1, 2008
@josiahcarlson
Copy link
Mannequin

josiahcarlson mannequin commented Aug 8, 2008

Sadly, this isn't quite as easy as it would seem. The O(1) memory
overhead version of this requires 2n reads and 2n writes, but does both
reads and writes at two memory locations at a time, which may have
nontrivial performance implications.

The simple version that copies out the small part of the shift into a
temporary buffer, doing a memcpy/memmov internally, then copying the
small data back is likely to have much better performance (worst-case
1.5n reads and 1.5n writes.

Offering this ability in the momoryview object would be very
interesting, though I'm not sure that the memoryview object is able to
offer a multi-segment buffer interface where the segments are not the
same length (this could be hacked by having a single pointer per byte,
but at that point we may as well perform a copy).

@pitrou
Copy link
Member Author

pitrou commented Aug 8, 2008

Hi,

Sadly, this isn't quite as easy as it would seem.

You are right, I was overly optimist when thinking about this.

Offering this ability in the momoryview object would be very
interesting, though I'm not sure that the memoryview object is able to
offer a multi-segment buffer interface where the segments are not the
same length (this could be hacked by having a single pointer per byte,
but at that point we may as well perform a copy).

I'm not sure what you mean, but I think we can just restrict it to the
simple case of a single contiguous buffer.

shift{left,right} could be useful too (and faster).

@josiahcarlson
Copy link
Mannequin

josiahcarlson mannequin commented Aug 8, 2008

In order for MemoryView to know what bytes it is pointing to in memory,
it (generally) keeps a pointer with a length. In order to rotate the
data without any copies, you need a pointer and length for each rotation
plus the original. For example, the equivalent to a rotate left of 8
characters using slicing is... x[8:] + x[:8]. That is two segments.
That's a "multi-segment buffer interface". But typical multi-segment
buffer interfaces require each segment to be exactly the same length
(like numpy), which is not the case with rotations.

@pitrou
Copy link
Member Author

pitrou commented Aug 8, 2008

Le vendredi 08 août 2008 à 21:44 +0000, Josiah Carlson a écrit :

Josiah Carlson <josiahcarlson@users.sourceforge.net> added the comment:

In order for MemoryView to know what bytes it is pointing to in memory,
it (generally) keeps a pointer with a length. In order to rotate the
data without any copies, you need a pointer and length for each rotation
plus the original. For example, the equivalent to a rotate left of 8
characters using slicing is... x[8:] + x[:8].

Hmm, I think it's simpler if the rotate is done in-place rather than
returning a new object. Most uses of memoryviews are going to be with
APIs requiring a single contiguous segment.
(of course for read-only buffers it would raise an error)

@rhettinger
Copy link
Contributor

Am -1 on this. Rotating byte arrays has very few use cases and the ones
it does have can typically be met by indexing.

@terryjreedy
Copy link
Member

Antoine, do you disagree with Raymond or should we close this?
In any case, I believe this would delayed by the moratorium.

@ethanfurman
Copy link
Member

Antoine, do you want to pursue, or can we close this?

@pitrou
Copy link
Member Author

pitrou commented May 18, 2013

I think we can close. bpo-17100 would have been more useful actually.

@serhiy-storchaka
Copy link
Member

I think you rather need the inplace shift operation. Or even the move the tail of buffer to the start without filling the remaining. I.e. something like

buffer[:size] = buffer[-size:]

but without creating immediate bytes object. Now it may be written as:

buffer[:size] = memoryview(buffer)[-size:]

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants