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: filtertuple, filterstring and filterunicode don't have optimization for PyBool_Type
Type: performance Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Joshua.Landau, rhettinger
Priority: normal Keywords:

Created on 2014-09-21 01:04 by Joshua.Landau, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg227199 - (view) Author: Joshua Landau (Joshua.Landau) * Date: 2014-09-21 01:04
All code referred to is from bltinmodule.c, Python 2.7.8:
https://github.com/python/cpython/blob/2.7/Python/bltinmodule.c

filter implements and optimization for PyBool_Type, making it equivalent to PyNone:

    # Line 303
    if (func == (PyObject *)&PyBool_Type || func == Py_None)

The specializations for tuples, byte strings and unicode don't have this:

    # Lines 2776, 2827, 2956, 2976
    if (func == Py_None)

This is a damper against recommending `filter(bool, ...)`.

---

Python 3 of course does not have these specializations, so has no bug.
msg227201 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-09-21 03:19
Is there code that isn't working as documented or are you asking for additional optimizations to make the other cases run a bit faster when 'bool' is passed in as the filter function?
msg227202 - (view) Author: Joshua Landau (Joshua.Landau) * Date: 2014-09-21 04:17
It's solely a speed thing. I think it was an oversight that the optimisation was only applied to lists. I didn't expect the optimisation to break when applied to tuples.
msg227204 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-09-21 07:25
It is a bit late in the game for 2.7 micro-optimizations.

FWIW, I don't think these weren't oversights. There isn't really much of a use case for the bool filter for strings and unicode inputs.  And the case of tuple inputs isn't used much ("lists are for looping/filtering and tuples are like records in a DB or structs in C").

Since there is no bug here and we're trying to touch 2.7 as little as possible these days, I recommend closing as "not a bug" or "won't fix".

If optimizing uncommon cases is important for you, then just use "filter(None, iterable)" instead of "filter(bool, iterable)" or work with itertools.ifilter() and itertools.ifilterfalse().
msg227207 - (view) Author: Joshua Landau (Joshua.Landau) * Date: 2014-09-21 08:26
That sounds OK to me. It's a bit of a non-issue once you know about it.
History
Date User Action Args
2022-04-11 14:58:08adminsetgithub: 66641
2014-09-21 12:04:13pitrousetstatus: open -> closed
resolution: rejected
2014-09-21 08:26:35Joshua.Landausetmessages: + msg227207
2014-09-21 07:25:23rhettingersetmessages: + msg227204
2014-09-21 04:17:28Joshua.Landausetmessages: + msg227202
2014-09-21 03:19:21rhettingersetnosy: + rhettinger
messages: + msg227201
2014-09-21 01:04:35Joshua.Landaucreate