GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-4356: Add key parameter to functions in bisect module by remilapeyre · Pull Request #11781 · python/cpython

Conversation

@remilapeyre

@remilapeyre remilapeyre commented Feb 7, 2019 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

https://bugs.python.org/issue4356

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

f6v, kgfly, and wimglenn reacted with thumbs up emoji
All reactions
  • 👍 3 reactions
Rémi Lapeyre
Add key parameter to functions in bisect module
e36dd1e
@remilapeyre
remilapeyre requested a review from rhettinger as a code owner February 7, 2019 15:13
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Feb 7, 2019
@bedevere-bot bedevere-bot added the awaiting review label Feb 7, 2019
Rémi Lapeyre added 2 commits February 7, 2019 16:27
Rémi Lapeyre
Support None as argument
d6082a5
Rémi Lapeyre
Fix documentation
b7aaa1a
@rhettinger rhettinger self-assigned this Feb 7, 2019
@remilapeyre

remilapeyre commented Feb 7, 2019

Copy link
Copy Markdown
Contributor Author

Hi Raymond, thanks for your comment.

I may be missing something but I'm not convinced the key function will get called multiple time per value.

I did some research before implementing this and I think your first point comes from the implementation of the key parameter in sorted() where the result of key on each element of the iterable is cached to avoid computing multiple time. ISTM that this is only necessary because the worst case complexity for sorting is n*ln(n) and a given element will get compared to multiple others to find its place in the resulting collection.

In binary search, most elements are not touched and it element e that is compared to the input x is only touched once so key(e) should only be computed once. As I commented in the code, I cached the result of key(x) so it would not be computed at each iteration. After all, each comparison done are against the same value (key(x)) so it would be wasteful to do them more than once, whether we use an auxiliary function or not.

About your second point, I think you say this because of the branching in the hot path. I did some tests before posting the pull request. The performance seems to be the same (I'm not sure this is a good to measure it thought, I would love some input on that):

➜  cpython git:(add-key-argument-to-bisect) python3 -m timeit -s "import bisect" "bisect.bisect(range(1_000_000_000_000_000), 25)"
50000 loops, best of 5: 5.23 usec per loop
➜  cpython git:(add-key-argument-to-bisect) ./python.exe -m timeit -s "import bisect" "bisect.bisect(range(1_000_000_000_000_000), 25)"
50000 loops, best of 5: 4.74 usec per loop
➜  cpython git:(add-key-argument-to-bisect) ./python.exe -m timeit -s "import bisect" "bisect.bisect(range(1_000_000_000_000_000), 25, key=lambda e: e)"
20000 loops, best of 5: 10 usec per loop

I guess the branch predictor does a good job here (?) and why no change of performance is seen (does someone know a good reference on branch predictors, out-of-order execution and other low-level performance details? I would like to learn more about them).

If I'm not making any mistake, the key argument can be safely be added here and the sorted collection is not necessary.

Am I missing the point completely?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@remilapeyre

remilapeyre commented Feb 7, 2019

Copy link
Copy Markdown
Contributor Author

Here's an example where key would break if called twice with the same object:

import bisect
from collections import defaultdict


class Test:
    def __init__(self, value):
        self.value = value


cache = defaultdict(int)

def key(e):
    cache[e] += 1
    assert cache[e] <= 1
    return e.value


l = [Test(i) for i in range(10000)]

bisect.bisect(l, Test(25), key=key)

It seems not to be an issue:

➜  cpython git:(add-key-argument-to-bisect) ./python.exe
Python 3.8.0a1+ (heads/add-key-argument-to-bisect:b7aaa1adad, Feb  7 2019, 17:33:24) 
[Clang 10.0.0 (clang-1000.10.44.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import bisect
>>> from collections import defaultdict
>>> 
>>> 
>>> class Test:
...     def __init__(self, value):
...         self.value = value
... 
>>> 
>>> cache = defaultdict(int)
>>> 
>>> def key(e):
...     cache[e] += 1
...     assert cache[e] <= 1
...     return e.value
... 
>>> 
>>> l = [Test(i) for i in range(10000)]
>>> 
>>> bisect.bisect(l, Test(25), key=key)
26
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

eamanu
eamanu reviewed Feb 8, 2019
View reviewed changes
Comment thread Doc/library/bisect.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@alexchamberlain

alexchamberlain commented Mar 3, 2019

Copy link
Copy Markdown

I found this whilst composing a message to Python-ideas about adding a key argument to bisect - just wanted to say I think this is a great idea. At the very least, this makes bisect consistent with the sort methods, but more importantly, it allows you to customise the ordering of objects on the fly.

I had also considered asking for a custom "comparator" argument, but quickly realised key is more consistent and you can convert a comparator to a key with a simple class implementing __lt__ (see functools.cmp_to_key.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@remilapeyre

remilapeyre commented Mar 3, 2019

Copy link
Copy Markdown
Contributor Author

CC @rhettinger

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

dimaqq
dimaqq reviewed Mar 17, 2019
View reviewed changes
Comment thread Doc/library/bisect.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

dimaqq
dimaqq reviewed Mar 17, 2019
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2019-02-07-15-44-12.bpo-4356.i6h86W.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

dimaqq
dimaqq reviewed Mar 17, 2019
View reviewed changes
Comment thread Doc/library/bisect.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Rémi Lapeyre
Improve documentation
1bc959d
@remilapeyre

remilapeyre commented Mar 17, 2019

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback @dimaqq!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rhettinger
rhettinger requested changes May 26, 2019
View reviewed changes
Comment thread Lib/bisect.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the awaiting review label May 26, 2019
@bedevere-bot

bedevere-bot commented May 26, 2019

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added the awaiting changes label May 26, 2019
rhettinger
rhettinger requested changes May 26, 2019
View reviewed changes

@rhettinger rhettinger left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key argument should be keyword only.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Rémi Lapeyre added 8 commits May 31, 2019 16:57
Rémi Lapeyre
Merge remote-tracking branch 'origin/master' into add-key-argument-to…
adb5a54
…-bisect
Rémi Lapeyre
Split logic for key=None
e1ce337
Rémi Lapeyre
Test that key is a keyword only argument
4337e73
Rémi Lapeyre
Make key a keyword only argument
d9022a9
Rémi Lapeyre
Add note in the documentation regarding lru_cache
84c18be
Rémi Lapeyre
Add test for reverse support
54960ed
Rémi Lapeyre
Add reverse support
ef98658
Rémi Lapeyre
Whitelist suspicious constructs in documentation
49ef1f7
@remilapeyre

remilapeyre commented May 31, 2019

Copy link
Copy Markdown
Contributor Author

Thanks for the review, I have made the requested changes; please review again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added awaiting change review and removed awaiting changes labels May 31, 2019
@bedevere-bot

bedevere-bot commented May 31, 2019

Copy link
Copy Markdown

Thanks for making the requested changes!

@rhettinger: please review the changes made to this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Rémi Lapeyre
Implement reverse bisect in Python
6bfe7bc
@remilapeyre remilapeyre mentioned this pull request Jun 11, 2019
bpo-37229: Add compare_function to bisect functions #13970
Closed
@csabella
csabella requested a review from rhettinger January 16, 2020 11:35
@remilapeyre remilapeyre mentioned this pull request May 31, 2020
bpo-4356: Add key function support to the bisect module #20556
Merged
@rhettinger

rhettinger commented May 31, 2020

Copy link
Copy Markdown
Contributor

Apologies, I had made my own PR 20556 before remembering that this one existed.

We should reconcile the two — each has tests the other doesn't, also there are some doc improvements in each that aren't in the other.

Am having second thoughts about including reversed because it doubles the complexity of the code, the tests, and spills over into documentation complexity. What do you think, save reversed for another day or put it now?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@csabella

csabella commented Aug 23, 2020

Copy link
Copy Markdown
Contributor

Closing in favor of #20556.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@csabella csabella closed this Aug 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@rhettinger rhettinger Awaiting requested review from rhettinger
2 more reviewers
@dimaqq dimaqq dimaqq left review comments
@eamanu eamanu eamanu left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@rhettinger rhettinger

Labels

awaiting change review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@remilapeyre @alexchamberlain @bedevere-bot @rhettinger @csabella @dimaqq @eamanu @the-knights-who-say-ni
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.