Visitar URL original
Implement BINARY_SUBSCR specialization for (String Int) by corona10 · Pull Request #94128 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20250525170549/https://github.com/python/cpython/pull/94128
Skip to content

Implement BINARY_SUBSCR specialization for (String Int) #94128

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 6 commits into from

Conversation

corona10
Copy link
Member

@corona10 corona10 commented Jun 22, 2022 •

Failure ratio: 24.2%(string int) from BINARY_SUBSCR

Microbenchmark

Mean +- std dev: [base] 3.75 us +- 0.02 us -> [BINARY_SUBSCR_UNICODE_INT] 3.48 us +- 0.02 us: 1.08x faster

import pyperf

runner = pyperf.Runner()
runner.timeit(name="str index",
              stmt="""
i = 0
while i < len(s):
    ch = s[i]
    i += 1
""",
              setup = """
s = 'helloworld' * 10
"""
              )

Ref leak test


➜  cpython git:(BINARY_SUBSCR_UNICODE_INT) ✗ ./python.exe -m test test_unicode test_userstring test_bytes -R 3:3
Raised RLIMIT_NOFILE: 256 -> 1024
0:00:00 load avg: 3.51 Run tests sequentially
0:00:00 load avg: 3.51 [1/3] test_unicode
beginning 6 repetitions
123456
......
0:00:09 load avg: 3.12 [2/3] test_userstring
beginning 6 repetitions
123456
......
0:00:17 load avg: 2.95 [3/3] test_bytes
beginning 6 repetitions
123456
......

== Tests result: SUCCESS ==

All 3 tests OK.

Total duration: 32.2 sec
Tests result: SUCCESS

@corona10 corona10 force-pushed the BINARY_SUBSCR_UNICODE_INT branch from 96db0f2 to dedf8e8 Compare June 22, 2022 15:43
@corona10 corona10 marked this pull request as ready for review June 22, 2022 16:25
@corona10 corona10 requested a review from markshannon as a code owner June 22, 2022 16:25
@corona10
Copy link
Member Author

As @kumaraditya303 suggested: Specializing in ASCII string only will be worth doing it, need to investigate.

@corona10 corona10 force-pushed the BINARY_SUBSCR_UNICODE_INT branch from 6939bf3 to f1dc8a4 Compare June 22, 2022 17:31
@corona10 corona10 changed the title Implement BINARY_SUBSCR_UNICODE_INT specialization Implement BINARY_SUBSCR specialization for (String Int) Jun 22, 2022
@corona10
Copy link
Member Author

corona10 commented Jun 22, 2022 •

@kumaraditya303 you are right, specializing in ASCII only shows better performance.
I will take a look at the coverage of BINARY_SUBSCR_ASCII_INT for BINARY_SUBSCR_STR_INT
Thank you for the suggestion and please take a look at my changes also :)

Microbenchmark


Mean +- std dev: [base] 3.75 us +- 0.02 us -> [BINARY_SUBSCR_ASCII_INT] 3.36 us +- 0.03 us: 1.12x faster

@corona10 corona10 requested a review from kumaraditya303 June 22, 2022 17:39
@corona10
Copy link
Member Author

corona10 commented Jun 23, 2022 •

Here is stat: https://gist.github.com/corona10/bdf7768e7d634dca87034f1f9506abf3

AS-IS (From Pyperformance stat)

BINARY_SUBSCR

Kind Count Ratio
unquickened 26777990 1.0%
specialization.deferred 1099812093 41.2%
specialization.deopt 400664 0.0%
hit 1520768373 57.0%
miss 21378050 0.8%

Specialization attempts

Count Ratio
Success 745,889 45.8%
Failure 882,644 54.2%
Failure kind Count Ratio
string int 213,383 24.2%
string slice 175,312 19.9%
array int 166,815 18.9%
list slice 101,907 11.5%
buffer slice 92,514 10.5%
other 79,465 9.0%
buffer int 32,637 3.7%
tuple slice 17,103 1.9%
sequence int 3,262 0.4%
array slice 246 0.0%

TO-BE

BINARY_SUBSCR

Kind Count Ratio
unquickened 43401421 1.1%
specialization.deferred 1442458722 36.8%
specialization.deopt 685055 0.0%
hit 2400478471 61.2%
miss 36691726 0.9%

Specialization attempts

Count Ratio
Success 1,135,256 55.5%
Failure 909,350 44.5%
Failure kind Count Ratio
string slice 243,475 26.8%
array int 225,573 24.8%
list slice 132,861 14.6%
buffer slice 118,608 13.0%
other 107,661 11.8%
buffer int 47,972 5.3%
tuple slice 26,443 2.9%
array slice 3,469 0.4%
sequence int 3,288 0.4%

@corona10
Copy link
Member Author

With @kumaraditya303 's suggestion:
Mean +- std dev: [base] 3.82 us +- 0.06 us -> [opt] 3.28 us +- 0.02 us: 1.17x faster

@corona10
Copy link
Member Author

@markshannon PTAL

@ghost
Copy link

ghost commented Jun 29, 2022 •

All commit authors signed the Contributor License Agreement.
CLA signed

@corona10 corona10 force-pushed the BINARY_SUBSCR_UNICODE_INT branch from aff3a44 to 2fc1a52 Compare June 29, 2022 16:19
Copy link
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

A couple of niggles.
Do you have pyperformance results?

Py_ssize_t index = ((PyLongObject*)sub)->ob_digit[0];
DEOPT_IF(index >= ascii->length, BINARY_SUBSCR);
STAT_INC(BINARY_SUBSCR, hit);
Py_UCS1 ch = *((Py_UCS1*)(ascii + 1) + index);
Copy link
Member

Choose a reason for hiding this comment

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

This is a bit obscure. Is there a pre-existing macro for getting the first character in an ascii string?

Copy link
Member Author

@corona10 corona10 Jun 30, 2022 •

Choose a reason for hiding this comment

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

Not exist but I am not sure about adding the macro for this.

@corona10
Copy link
Member Author

A couple of niggles.
Do you have pyperformance results?

Sorry, I don't have a reliable isolated environment to run pyperformance.
But I can run benchmarks with my m1 mac machine (but not isolated..)
or If you don't mind, would you like to run a benchmark for this PR?
Sorry for the frequent request.

@markshannon
Copy link
Member

@corona10
Copy link
Member Author

corona10 commented Jul 2, 2022

A bit faster, but not significantly

Thanks!
Should we satisfy both performance improvement and high failure ratio reduction in the entire benchmark set?

@erlend-aasland
Copy link
Contributor

I fail to see how this qualifies as skip issue, according to our workflow.

@corona10
Copy link
Member Author

corona10 commented Aug 9, 2022

I fail to see how this qualifies as skip issue, according to our workflow

I will create a issue for the future work, but let's close this PR now.

@corona10 corona10 closed this Aug 9, 2022
@corona10 corona10 deleted the BINARY_SUBSCR_UNICODE_INT branch February 15, 2024 01:40
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.

5 participants