This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Make slicing of immutable structures return a view instead of a copy
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.6
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Filip Haglund, abarry, lemburg, martin.panter, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2016-01-10 20:45 by Filip Haglund, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg257937 - (view) Author: Filip Haglund (Filip Haglund) Date: 2016-01-10 20:45
Slicing tuples returns a copy, which is O(n) time. This could be O(1) since tuples are immutable, by just pointing to the same data.

This probably applies to other immutable structures as well, such as strings.
msg257939 - (view) Author: Anilyka Barry (abarry) * (Python triager) Date: 2016-01-10 21:22
This is an interesting idea, +1 from me. Do you want to submit a patch?
msg257942 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-01-10 22:04
I think which technique (copy or view) is better depends on the situation. If you are making a large temporary slice, a view may be more efficient. But if you are making a long-term slice and don’t need the original any more, a copy would allow the original memory to be freed early.

Do you have any use cases? You can already get a kind of slice view into bytes etc by using memoryview().
msg257952 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2016-01-11 08:31
While it's theoretically possible, there's a reason why we haven't done this in the past: we don't want to keep the possibly large original object alive when using a slice.

For buffer interface types, you can already use memoryview to get a view in case you need this.

In most other cases, it's better to work with indexes into the original object rather than views on the data.
msg258374 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-01-16 09:37
Proposals for strings views have be rejected precisely because of the keep-alive effect.

I do not remember if tuples were explicitly part of earlier discussions. One could make the argument that million-item tuples, and especially slicing thereof is rarer than the same for strings.  On the other hand, copying short slices of short to medium tuples is not a big deal.

Because of previous discussions, I think this issue should be either closed or suspended for python-ideas discussion.
msg258375 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-01-16 09:48
The support of sharing a content between different tuples requires changing the structure of the tuple object, allocating additional block for every tuple, adding a level of indirection and reference counting. This will increase memory consumption, creating and access time for all tuples. All this is critically important for Python interpreter. I think this idea has no a chance.
Date User Action Args
2022-04-11 14:58:26adminsetgithub: 70265
2016-01-16 09:48:53serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg258375

resolution: rejected
stage: needs patch -> resolved
2016-01-16 09:37:58terry.reedysetnosy: + terry.reedy
messages: + msg258374
2016-01-11 08:31:30lemburgsetnosy: + lemburg
messages: + msg257952
2016-01-10 22:04:57martin.pantersetnosy: + martin.panter
messages: + msg257942
2016-01-10 21:22:29abarrysetversions: - Python 2.7, Python 3.2, Python 3.3, Python 3.4, Python 3.5
nosy: + abarry

messages: + msg257939

stage: needs patch
2016-01-10 20:45:56Filip Haglundcreate