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.

classification
Title: Speed-up deque.__bool__
Type: Stage:
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: josh.r, python-dev, rhettinger
Priority: normal Keywords: patch

Created on 2015-03-23 06:05 by rhettinger, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
deque_bool1.diff rhettinger, 2015-03-23 06:05 Add __bool__ review
timings_deque_bool.txt rhettinger, 2015-03-23 06:05
Messages (5)
msg238983 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-03-23 06:05
The __bool__ method is a little faster than __len__.
Timings attached.
msg239086 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2015-03-24 01:46
Is it worth the definition of tp_as_number and maintaining another method for a micro-optimization? Presumably every collection in Python would benefit slightly from this (say, defaultdict in the same file, list, tuple, etc.), but none of them bother. Why would deque be worth the optimization and not the more commonly used built-ins?
msg239088 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2015-03-24 02:06
On further checking, this probably gives slightly greater benefit to deque than it would for list, tuple, dict, etc., only because PyObject_IsTrue checks for mapping before sequence; deque is a pure sequence, no mapping, so it has to fail an extra test before it reaches the sequence length interface check and uses it, where any sequence that supports extended slicing must support parts of the mapping interface, and usually copies the sequence function pointer to the mapping length slot as well.

Still hard to justify, though. Testing the truthiness of a deque is not likely to be the critical code path for many applications (if you want to take advantage of deque's atomic behaviors, you're using EAFP patterns anyway, not testing truthiness), and the gain in boolean testing means other (likely low probability tests) will run slower (for example, testing a deque with PyNumber_Check would go slower, because it would pass an extra test before failing each time).
msg239092 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-03-24 06:23
Thanks for taking a look.   IMO, the cost of this one is near zero and there is a modest benefit.  It is simple enough that I don't feel any angst about applying it.  I'm doing several little touch-ups to the module, individually nothing, but collectively making it better overall.
msg239093 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-03-24 06:24
New changeset bb27dff7f83f by Raymond Hettinger in branch 'default':
Issue 23744:  Minor speed-up for deque.__bool__().
https://hg.python.org/cpython/rev/bb27dff7f83f
History
Date User Action Args
2022-04-11 14:58:14adminsetgithub: 67932
2015-03-24 06:24:22rhettingersetstatus: open -> closed
resolution: fixed
2015-03-24 06:24:05python-devsetnosy: + python-dev
messages: + msg239093
2015-03-24 06:23:52rhettingersetmessages: + msg239092
2015-03-24 02:06:23josh.rsetmessages: + msg239088
2015-03-24 01:46:48josh.rsetnosy: + josh.r
messages: + msg239086
2015-03-23 06:05:24rhettingersetfiles: + timings_deque_bool.txt
2015-03-23 06:05:12rhettingercreate