classification
Title: Cache constant "slice" instances
Type: performance Stage: needs patch
Components: Interpreter Core Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: belopolsky, josh.r, meador.inge, pitrou, scoder, yselivanov
Priority: normal Keywords:

Created on 2011-02-03 19:25 by scoder, last changed 2018-12-04 02:39 by josh.r.

Messages (5)
msg127804 - (view) Author: Stefan Behnel (scoder) * Date: 2011-02-03 19:25
Follow-up to ticket 10227. The following facts seem to indicate that it would be worth caching constant instances of the slice type, such as in [:] or [:-1].

with cached slice instance:

$ ./python -m timeit -s 'l = list(range(100)); s=slice(None)' 'l[s]'
1000000 loops, best of 3: 0.464 usec per loop
$ ./python -m timeit -s 'l = list(range(10)); s=slice(None)' 'l[s]'
10000000 loops, best of 3: 0.149 usec per loop
$ ./python -m timeit -s 'l = list(range(10)); s=slice(None,1)' 'l[s]'
10000000 loops, best of 3: 0.135 usec per loop

uncached normal usage:

$ ./python -m timeit -s 'l = list(range(100))' 'l[:]'
1000000 loops, best of 3: 0.499 usec per loop
$ ./python -m timeit -s 'l = list(range(100))' 'l[:1]'
10000000 loops, best of 3: 0.171 usec per loop

Timings based on Python 3.2 rc2.

A quick grep against the py3k stdlib finds 2096 lines in 393 files that use constant slices.
msg127806 - (view) Author: Stefan Behnel (scoder) * Date: 2011-02-03 19:27
Erm, issue 10227.
msg127808 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2011-02-03 19:35
Similar idea has been rejected in issue2268 because the win was too small to justify the number of changes that had to be made.
msg127810 - (view) Author: Stefan Behnel (scoder) * Date: 2011-02-03 19:48
Hmm, ok, but AFAICT, your patch was rejected rather because of the way it approached the problem, not so much because of the issue itself.

Plus, the fact that Python 3 requires slices in more places than Python 2 (which had the lower level getslice protocol) makes this a bigger issue now than it was three years ago.
msg127818 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-02-03 20:20
> Hmm, ok, but AFAICT, your patch was rejected rather because of the way
> it approached the problem, not so much because of the issue itself.

I would be rather for the patch myself. The bytecode currently generated
for sliced indexing is awfully suboptimal.

> Plus, the fact that Python 3 requires slices in more places than
> Python 2 (which had the lower level getslice protocol) makes this a
> bigger issue now than it was three years ago.

True.
History
Date User Action Args
2018-12-04 02:39:34josh.rsetversions: + Python 3.8, - Python 3.5
2014-03-06 22:40:28josh.rsetnosy: + josh.r
2014-01-31 21:58:35yselivanovsetnosy: + yselivanov
2014-01-31 21:57:44yselivanovsetversions: + Python 3.5, - Python 3.3
2011-11-28 01:34:57meador.ingesetnosy: + meador.inge
2011-11-18 16:39:24ezio.melottisetstage: needs patch
2011-02-03 20:20:16pitrousetmessages: + msg127818
2011-02-03 19:48:57scodersetmessages: + msg127810
2011-02-03 19:47:03pitrousetnosy: + pitrou
2011-02-03 19:35:38belopolskysetnosy: + belopolsky
messages: + msg127808
2011-02-03 19:27:21scodersetmessages: + msg127806
2011-02-03 19:25:20scodercreate