Navigation Menu

Skip to content
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

Use 8-byte step to detect ASCII sequence in 64bit Windows builds #82433

Closed
animalize mannequin opened this issue Sep 22, 2019 · 6 comments
Closed

Use 8-byte step to detect ASCII sequence in 64bit Windows builds #82433

animalize mannequin opened this issue Sep 22, 2019 · 6 comments
Labels
3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) OS-windows performance Performance or resource usage

Comments

@animalize
Copy link
Mannequin

animalize mannequin commented Sep 22, 2019

BPO 38252
Nosy @pfmoore, @tjguk, @methane, @zware, @serhiy-storchaka, @zooba, @animalize, @sir-sigurd
PRs
  • bpo-38252: use 8-byte step to detect ASCII sequence in 64bit Windows build #16334
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-10-18.16:52:39.004>
    created_at = <Date 2019-09-22.11:50:02.093>
    labels = ['interpreter-core', '3.10', 'OS-windows', 'performance']
    title = 'Use 8-byte step to detect ASCII sequence in 64bit Windows builds'
    updated_at = <Date 2020-10-18.16:52:39.004>
    user = 'https://github.com/animalize'

    bugs.python.org fields:

    activity = <Date 2020-10-18.16:52:39.004>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-10-18.16:52:39.004>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core', 'Windows']
    creation = <Date 2019-09-22.11:50:02.093>
    creator = 'malin'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38252
    keywords = ['patch']
    message_count = 6.0
    messages = ['352970', '352998', '353007', '353024', '378801', '378869']
    nosy_count = 8.0
    nosy_names = ['paul.moore', 'tim.golden', 'methane', 'zach.ware', 'serhiy.storchaka', 'steve.dower', 'malin', 'sir-sigurd']
    pr_nums = ['16334']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue38252'
    versions = ['Python 3.10']

    @animalize
    Copy link
    Mannequin Author

    animalize mannequin commented Sep 22, 2019

    C type long is 4-byte integer in 64-bit Windows build. [1]

    But ucs1lib_find_max_char() function [2] uses SIZEOF_LONG, so it loses a little performance in 64-bit Windows build.

    Below is the benchmark of using SIZEOF_SIZE_T and this change:

    -   unsigned long value = *(unsigned long *) _p;
    +   sizt_t value = *(sizt_t *) _p;
    

    D:\dev\cpython\PCbuild\amd64\python.exe -m pyperf timeit -s "b=b'a'*10_000_000; f=b.decode;" "f('latin1')"

    before: 5.83 ms +- 0.05 ms
    after : 5.58 ms +- 0.06 ms
    

    [1] https://stackoverflow.com/questions/384502

    [2] https://github.com/python/cpython/blob/v3.8.0b4/Objects/stringlib/find_max_char.h#L9

    Maybe there can be more optimizations, so I didn't prepare a PR for this.

    @animalize animalize mannequin added 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage labels Sep 22, 2019
    @serhiy-storchaka
    Copy link
    Member

    This looks like a good idea. Do you mind to create a PR?

    @animalize
    Copy link
    Mannequin Author

    animalize mannequin commented Sep 23, 2019

    Maybe @sir-sigurd can find more optimizations.

    FYI, _Py_bytes_isascii() function [1] also has similar code.
    [1] https://github.com/python/cpython/blob/v3.8.0b4/Objects/bytes_methods.c#L104

    @animalize animalize mannequin changed the title micro-optimize ucs1lib_find_max_char in Windows 64-bit build Use 8-byte step to detect ASCII sequence in 64bit Windows builds Sep 23, 2019
    @animalize
    Copy link
    Mannequin Author

    animalize mannequin commented Sep 23, 2019

    There are 4 functions have the similar code, see PR 16334.
    Just replaced the unsigned long type with size_t type, got these benchmarks.
    Can this be backported to 3.8 branch?

    1. bytes.isascii()

    D:\dev\cpython\PCbuild\amd64\python.exe -m pyperf timeit -s "b = b'x' * 100_000_000; f = b.isascii;" "f()"

    +-----------+-----------+------------------------------+
    | Benchmark | isascii_a | isascii_b |
    +===========+===========+==============================+
    | timeit | 11.7 ms | 7.84 ms: 1.50x faster (-33%) |
    +-----------+-----------+------------------------------+

    1. bytes.decode('latin1')

    D:\dev\cpython\PCbuild\amd64\python.exe -m pyperf timeit -s "b = b'x' * 100_000_000; f = b.decode;" "f('latin1')"

    +-----------+----------+-----------------------------+
    | Benchmark | latin1_a | latin1_b |
    +===========+==========+=============================+
    | timeit | 60.3 ms | 57.4 ms: 1.05x faster (-5%) |
    +-----------+----------+-----------------------------+

    1. bytes.decode('ascii')

    D:\dev\cpython\PCbuild\amd64\python.exe -m pyperf timeit -s "b = b'x' * 100_000_000; f = b.decode;" "f('ascii')"

    +-----------+---------+-----------------------------+
    | Benchmark | ascii_a | ascii_b |
    +===========+=========+=============================+
    | timeit | 48.5 ms | 47.1 ms: 1.03x faster (-3%) |
    +-----------+---------+-----------------------------+

    1. bytes.decode('utf8')

    D:\dev\cpython\PCbuild\amd64\python.exe -m pyperf timeit -s "b = b'x' * 100_000_000; f = b.decode;" "f('utf8')"

    +-----------+---------+-----------------------------+
    | Benchmark | utf8_a | utf8_b |
    +===========+=========+=============================+
    | timeit | 48.3 ms | 47.1 ms: 1.03x faster (-3%) |
    +-----------+---------+-----------------------------+

    @csabella csabella added 3.10 only security fixes and removed 3.9 only security fixes labels Jun 14, 2020
    @animalize
    Copy link
    Mannequin Author

    animalize mannequin commented Oct 17, 2020

    Although the improvement is not great, it's a very hot code path.

    Could you review the PR?

    @animalize animalize mannequin added the OS-windows label Oct 17, 2020
    @serhiy-storchaka
    Copy link
    Member

    New changeset a0c603c by Ma Lin in branch 'master':
    bpo-38252: Use 8-byte step to detect ASCII sequence in 64bit Windows build (GH-16334)
    a0c603c

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) OS-windows performance Performance or resource usage
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants