Skip to content

bpo-37371: Optimize and refactor readline(). #14306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

shlomif
Copy link
Contributor

@shlomif shlomif commented Jun 22, 2019

  1. Merge two duplicate branches.

  2. Extract some constants.

  3. Convert to prefix-++ instead of suffix-++.

===========

Benchmarks:

before:

  • PATH=/home/shlomif/apps/python3/bin:/home/shlomif/apps/fcs/bin:/home/shlomif/progs/freecell/git/0fc-b/:/home/shlomif/progs/freecell/git/0fc-b//board_gen:/home/shlomif/bin:/home/shlomif/apps/perl/modules/bin:/home/shlomif/apps/perl/modules/local/bin:/home/shlomif/apps/neovim/bin:/home/shlomif/apps/fop/fop-20140425:/home/shlomif/apps/vim/bin:/home/shlomif/apps/golang/bin:/home/shlomif/.local/bin:/home/shlomif/.perl6/bin:/home/shlomif/.cargo/bin:/usr/local/bin:/usr/bin:/usr/lib64/qt5/bin:/usr/lib64/qt4/bin:/usr/games
  • export LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
  • LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
  • python3 collect-stats.py 0fc-log.txt
    S 18474775
    Int 617438

real 0m6.655s
user 0m6.599s
sys 0m0.054s

  • PATH=/home/shlomif/apps/python3/bin:/home/shlomif/apps/fcs/bin:/home/shlomif/progs/freecell/git/0fc-b/:/home/shlomif/progs/freecell/git/0fc-b//board_gen:/home/shlomif/bin:/home/shlomif/apps/perl/modules/bin:/home/shlomif/apps/perl/modules/local/bin:/home/shlomif/apps/neovim/bin:/home/shlomif/apps/fop/fop-20140425:/home/shlomif/apps/vim/bin:/home/shlomif/apps/golang/bin:/home/shlomif/.local/bin:/home/shlomif/.perl6/bin:/home/shlomif/.cargo/bin:/usr/local/bin:/usr/bin:/usr/lib64/qt5/bin:/usr/lib64/qt4/bin:/usr/games
  • export LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
  • LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
  • python3 collect-stats.py 0fc-log.txt
    S 18474775
    Int 617438

real 0m6.817s
user 0m6.772s
sys 0m0.045s

  • PATH=/home/shlomif/apps/python3/bin:/home/shlomif/apps/fcs/bin:/home/shlomif/progs/freecell/git/0fc-b/:/home/shlomif/progs/freecell/git/0fc-b//board_gen:/home/shlomif/bin:/home/shlomif/apps/perl/modules/bin:/home/shlomif/apps/perl/modules/local/bin:/home/shlomif/apps/neovim/bin:/home/shlomif/apps/fop/fop-20140425:/home/shlomif/apps/vim/bin:/home/shlomif/apps/golang/bin:/home/shlomif/.local/bin:/home/shlomif/.perl6/bin:/home/shlomif/.cargo/bin:/usr/local/bin:/usr/bin:/usr/lib64/qt5/bin:/usr/lib64/qt4/bin:/usr/games
  • export LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
  • LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
  • python3 collect-stats.py 0fc-log.txt
    S 18474775
    Int 617438

real 0m6.763s
user 0m6.706s
sys 0m0.057s

============

After:

  • PATH=/home/shlomif/apps/python3/bin:/home/shlomif/apps/fcs/bin:/home/shlomif/progs/freecell/git/0fc-b/:/home/shlomif/progs/freecell/git/0fc-b//board_gen:/home/shlomif/bin:/home/shlomif/apps/perl/modules/bin:/home/shlomif/apps/perl/modules/local/bin:/home/shlomif/apps/neovim/bin:/home/shlomif/apps/fop/fop-20140425:/home/shlomif/apps/vim/bin:/home/shlomif/apps/golang/bin:/home/shlomif/.local/bin:/home/shlomif/.perl6/bin:/home/shlomif/.cargo/bin:/usr/local/bin:/usr/bin:/usr/lib64/qt5/bin:/usr/lib64/qt4/bin:/usr/games
  • export LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
  • LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
  • python3 collect-stats.py 0fc-log.txt
    S 18474775
    Int 617438

real 0m6.651s
user 0m6.595s
sys 0m0.056s

  • PATH=/home/shlomif/apps/python3/bin:/home/shlomif/apps/fcs/bin:/home/shlomif/progs/freecell/git/0fc-b/:/home/shlomif/progs/freecell/git/0fc-b//board_gen:/home/shlomif/bin:/home/shlomif/apps/perl/modules/bin:/home/shlomif/apps/perl/modules/local/bin:/home/shlomif/apps/neovim/bin:/home/shlomif/apps/fop/fop-20140425:/home/shlomif/apps/vim/bin:/home/shlomif/apps/golang/bin:/home/shlomif/.local/bin:/home/shlomif/.perl6/bin:/home/shlomif/.cargo/bin:/usr/local/bin:/usr/bin:/usr/lib64/qt5/bin:/usr/lib64/qt4/bin:/usr/games
  • export LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
  • LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
  • python3 collect-stats.py 0fc-log.txt
    S 18474775
    Int 617438

real 0m6.625s
user 0m6.559s
sys 0m0.066s

  • PATH=/home/shlomif/apps/python3/bin:/home/shlomif/apps/fcs/bin:/home/shlomif/progs/freecell/git/0fc-b/:/home/shlomif/progs/freecell/git/0fc-b//board_gen:/home/shlomif/bin:/home/shlomif/apps/perl/modules/bin:/home/shlomif/apps/perl/modules/local/bin:/home/shlomif/apps/neovim/bin:/home/shlomif/apps/fop/fop-20140425:/home/shlomif/apps/vim/bin:/home/shlomif/apps/golang/bin:/home/shlomif/.local/bin:/home/shlomif/.perl6/bin:/home/shlomif/.cargo/bin:/usr/local/bin:/usr/bin:/usr/lib64/qt5/bin:/usr/lib64/qt4/bin:/usr/games
  • export LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
  • LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
  • python3 collect-stats.py 0fc-log.txt
    S 18474775
    Int 617438

real 0m6.601s
user 0m6.559s
sys 0m0.042s

=========

Based on https://github.com/shlomif/freecell-pro-0fc-deals .

https://bugs.python.org/issue37371

@tirkarthi
Copy link
Member

Please open an issue for this at https://bugs.python.org along with the benchmark findings.

@shlomif
Copy link
Contributor Author

shlomif commented Jun 22, 2019

@mangrisano
Copy link
Contributor

/cc @benjaminp

{
const Py_ssize_t len_readahead = PyBytes_GET_SIZE(readahead);
if (len_readahead > 0) {
#ifndef min
Copy link
Member

Choose a reason for hiding this comment

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

You can use Py_MIN.

1. Merge two duplicate branches.

2. Extract some constants.

3. Convert to prefix-++ instead of suffix-++.

===========

Benchmarks:

before:

+ PATH=/home/shlomif/apps/python3/bin:/home/shlomif/apps/fcs/bin:/home/shlomif/progs/freecell/git/0fc-b/:/home/shlomif/progs/freecell/git/0fc-b//board_gen:/home/shlomif/bin:/home/shlomif/apps/perl/modules/bin:/home/shlomif/apps/perl/modules/local/bin:/home/shlomif/apps/neovim/bin:/home/shlomif/apps/fop/fop-20140425:/home/shlomif/apps/vim/bin:/home/shlomif/apps/golang/bin:/home/shlomif/.local/bin:/home/shlomif/.perl6/bin:/home/shlomif/.cargo/bin:/usr/local/bin:/usr/bin:/usr/lib64/qt5/bin:/usr/lib64/qt4/bin:/usr/games
+ export LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
+ LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
+ python3 collect-stats.py 0fc-log.txt
S	18474775
Int	617438

real	0m6.655s
user	0m6.599s
sys	0m0.054s
+ PATH=/home/shlomif/apps/python3/bin:/home/shlomif/apps/fcs/bin:/home/shlomif/progs/freecell/git/0fc-b/:/home/shlomif/progs/freecell/git/0fc-b//board_gen:/home/shlomif/bin:/home/shlomif/apps/perl/modules/bin:/home/shlomif/apps/perl/modules/local/bin:/home/shlomif/apps/neovim/bin:/home/shlomif/apps/fop/fop-20140425:/home/shlomif/apps/vim/bin:/home/shlomif/apps/golang/bin:/home/shlomif/.local/bin:/home/shlomif/.perl6/bin:/home/shlomif/.cargo/bin:/usr/local/bin:/usr/bin:/usr/lib64/qt5/bin:/usr/lib64/qt4/bin:/usr/games
+ export LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
+ LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
+ python3 collect-stats.py 0fc-log.txt
S	18474775
Int	617438

real	0m6.817s
user	0m6.772s
sys	0m0.045s
+ PATH=/home/shlomif/apps/python3/bin:/home/shlomif/apps/fcs/bin:/home/shlomif/progs/freecell/git/0fc-b/:/home/shlomif/progs/freecell/git/0fc-b//board_gen:/home/shlomif/bin:/home/shlomif/apps/perl/modules/bin:/home/shlomif/apps/perl/modules/local/bin:/home/shlomif/apps/neovim/bin:/home/shlomif/apps/fop/fop-20140425:/home/shlomif/apps/vim/bin:/home/shlomif/apps/golang/bin:/home/shlomif/.local/bin:/home/shlomif/.perl6/bin:/home/shlomif/.cargo/bin:/usr/local/bin:/usr/bin:/usr/lib64/qt5/bin:/usr/lib64/qt4/bin:/usr/games
+ export LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
+ LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
+ python3 collect-stats.py 0fc-log.txt
S	18474775
Int	617438

real	0m6.763s
user	0m6.706s
sys	0m0.057s

============

After:

+ PATH=/home/shlomif/apps/python3/bin:/home/shlomif/apps/fcs/bin:/home/shlomif/progs/freecell/git/0fc-b/:/home/shlomif/progs/freecell/git/0fc-b//board_gen:/home/shlomif/bin:/home/shlomif/apps/perl/modules/bin:/home/shlomif/apps/perl/modules/local/bin:/home/shlomif/apps/neovim/bin:/home/shlomif/apps/fop/fop-20140425:/home/shlomif/apps/vim/bin:/home/shlomif/apps/golang/bin:/home/shlomif/.local/bin:/home/shlomif/.perl6/bin:/home/shlomif/.cargo/bin:/usr/local/bin:/usr/bin:/usr/lib64/qt5/bin:/usr/lib64/qt4/bin:/usr/games
+ export LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
+ LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
+ python3 collect-stats.py 0fc-log.txt
S	18474775
Int	617438

real	0m6.651s
user	0m6.595s
sys	0m0.056s
+ PATH=/home/shlomif/apps/python3/bin:/home/shlomif/apps/fcs/bin:/home/shlomif/progs/freecell/git/0fc-b/:/home/shlomif/progs/freecell/git/0fc-b//board_gen:/home/shlomif/bin:/home/shlomif/apps/perl/modules/bin:/home/shlomif/apps/perl/modules/local/bin:/home/shlomif/apps/neovim/bin:/home/shlomif/apps/fop/fop-20140425:/home/shlomif/apps/vim/bin:/home/shlomif/apps/golang/bin:/home/shlomif/.local/bin:/home/shlomif/.perl6/bin:/home/shlomif/.cargo/bin:/usr/local/bin:/usr/bin:/usr/lib64/qt5/bin:/usr/lib64/qt4/bin:/usr/games
+ export LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
+ LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
+ python3 collect-stats.py 0fc-log.txt
S	18474775
Int	617438

real	0m6.625s
user	0m6.559s
sys	0m0.066s
+ PATH=/home/shlomif/apps/python3/bin:/home/shlomif/apps/fcs/bin:/home/shlomif/progs/freecell/git/0fc-b/:/home/shlomif/progs/freecell/git/0fc-b//board_gen:/home/shlomif/bin:/home/shlomif/apps/perl/modules/bin:/home/shlomif/apps/perl/modules/local/bin:/home/shlomif/apps/neovim/bin:/home/shlomif/apps/fop/fop-20140425:/home/shlomif/apps/vim/bin:/home/shlomif/apps/golang/bin:/home/shlomif/.local/bin:/home/shlomif/.perl6/bin:/home/shlomif/.cargo/bin:/usr/local/bin:/usr/bin:/usr/lib64/qt5/bin:/usr/lib64/qt4/bin:/usr/games
+ export LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
+ LD_LIBRARY_PATH=/home/shlomif/apps/python3/lib
+ python3 collect-stats.py 0fc-log.txt
S	18474775
Int	617438

real	0m6.601s
user	0m6.559s
sys	0m0.042s

=========

Based on https://github.com/shlomif/freecell-pro-0fc-deals .
@csabella csabella changed the title Optimize and refactor readline(). bpo-37371: Optimize and refactor readline(). Jan 13, 2020
@shlomif
Copy link
Contributor Author

shlomif commented Jan 28, 2020

Hi all,

can this be merged now, or do you need anything beforehand?

const Py_ssize_t len_readahead = PyBytes_GET_SIZE(readahead);
if (len_readahead > 0) {
const Py_ssize_t upper_limit = (((limit >= 0) ? Py_MIN(limit, len_readahead) : len_readahead)-1);
Py_ssize_t n = -1;
Copy link
Contributor

Choose a reason for hiding this comment

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

This initialization is a bit awkward. Can we "add 1" to everything? i.e., make this n = 0 and remove the -1 above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, I did it that way so we can use prefix-++ instead of suffix-++ because the former is believed to be faster. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

I doubt it makes any difference with a modern compiler.

Copy link
Member

Choose a reason for hiding this comment

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

I concur with @benjaminp.

Also, why not use memchr?

@iritkatriel iritkatriel added the performance Performance or resource usage label Aug 31, 2022
@iritkatriel iritkatriel added pending The issue will be closed if no feedback is provided and removed performance Performance or resource usage labels Nov 7, 2022
@iritkatriel
Copy link
Member

I removed the performance label because it's not clear that this makes a difference we would need benchmarks to see that it does).

Added the pending label because it seems to have been abandoned (there was no response to @benjaminp 's review). This means that this PR will be closed in a few weeks if there is no followup.

@shlomif
Copy link
Contributor Author

shlomif commented Nov 8, 2022 via email

@iritkatriel
Copy link
Member

Thanks.

@iritkatriel iritkatriel closed this Nov 8, 2022
@shlomif shlomif mannequin mentioned this pull request Nov 8, 2022
@AA-Turner AA-Turner removed the pending The issue will be closed if no feedback is provided label Apr 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.