-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
Conversation
Please open an issue for this at https://bugs.python.org along with the benchmark findings. |
/cc @benjaminp |
Modules/_io/iobase.c
Outdated
{ | ||
const Py_ssize_t len_readahead = PyBytes_GET_SIZE(readahead); | ||
if (len_readahead > 0) { | ||
#ifndef min |
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.
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 .
Thanks to @serhiy-storchaka .
8eccf10
to
89bdc1c
Compare
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; |
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.
This initialization is a bit awkward. Can we "add 1" to everything? i.e., make this n = 0
and remove the -1
above?
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.
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?
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.
I doubt it makes any difference with a modern compiler.
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.
I concur with @benjaminp.
Also, why not use memchr
?
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. |
On Mon, 07 Nov 2022 11:08:01 -0800 Serhiy Storchaka ***@***.***> wrote:
@serhiy-storchaka commented on this pull request.
> @@ -572,26 +572,20 @@ _io__IOBase_readline_impl(PyObject *self, Py_ssize_t
> limit)
Py_DECREF(readahead);
goto fail;
}
- if (PyBytes_GET_SIZE(readahead) > 0) {
- Py_ssize_t n = 0;
- const char *buf = PyBytes_AS_STRING(readahead);
- if (limit >= 0) {
+ {
+ 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;
I concur with @benjaminp.
Also, why not use `memchr`?
hi. you can close this pull-req now. i'm not attched to it.
…--
Shlomi Fish https://www.shlomifish.org/
Emma Watson Factoids - https://shlom.in/emwatson-facts
Sarah: Ken, would you like some more juice?
Ken: No, Sarah, that's OK, I’m stuffed. Anyway, I think I’ll return to my
dimension of evil now - they will be clueless without me. Thanks for everything.
— https://www.shlomifish.org/humour/Buffy/A-Few-Good-Slayers/
Please reply to list if it's a mailing list post - https://shlom.in/reply .
|
Thanks. |
Merge two duplicate branches.
Extract some constants.
Convert to prefix-++ instead of suffix-++.
===========
Benchmarks:
before:
S 18474775
Int 617438
real 0m6.655s
user 0m6.599s
sys 0m0.054s
S 18474775
Int 617438
real 0m6.817s
user 0m6.772s
sys 0m0.045s
S 18474775
Int 617438
real 0m6.763s
user 0m6.706s
sys 0m0.057s
============
After:
S 18474775
Int 617438
real 0m6.651s
user 0m6.595s
sys 0m0.056s
S 18474775
Int 617438
real 0m6.625s
user 0m6.559s
sys 0m0.066s
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