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 — optimize int division · Issue #90564 · python/cpython

optimize int division #90564

New issue
Copy link
New issue
Copy link
Closed
Closed
optimize int division#90564
Copy link
Labels
3.11only security fixesonly security fixesperformancePerformance or resource usagePerformance or resource usage

Description

@gpshead
gpshead
opened on Jan 16, 2022
Issue body actions
BPO 46406
Nosy @tim-one, @rhettinger, @gpshead, @mdickinson
PRs
  • bpo-46406: Faster single digit int division. #30626
  • bpo-46504: faster code for trial quotient in x_divrem() #30856
  • 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 2022-01-23.10:01:22.679>
    created_at = <Date 2022-01-16.23:38:29.594>
    labels = ['3.11', 'performance']
    title = 'optimize int division'
    updated_at = <Date 2022-01-25.01:06:10.163>
    user = 'https://github.com/gpshead'

    bugs.python.org fields:

    activity = <Date 2022-01-25.01:06:10.163>
    actor = 'tim.peters'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-01-23.10:01:22.679>
    closer = 'mark.dickinson'
    components = []
    creation = <Date 2022-01-16.23:38:29.594>
    creator = 'gregory.p.smith'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46406
    keywords = ['patch']
    message_count = 5.0
    messages = ['410735', '410736', '410739', '411358', '411541']
    nosy_count = 4.0
    nosy_names = ['tim.peters', 'rhettinger', 'gregory.p.smith', 'mark.dickinson']
    pr_nums = ['30626', '30856']
    priority = 'normal'
    resolution = None
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue46406'
    versions = ['Python 3.11']

    Activity

    1. gpshead commented on Jan 16, 2022

      @gpshead
      gpshead
      on Jan 16, 2022
      MemberAuthor
      More actions

      Based on a python-dev thread, we've come up with faster int division code for CPython's bignums.

      https://mail.python.org/archives/list/[email protected]/thread/ZICIMX5VFCX4IOFH5NUPVHCUJCQ4Q7QM/#NEUNFZU3TQU4CPTYZNF3WCN7DOJBBTK5

      filing this issue for starters to attach a PR to. details forthcoming.

    2. gpshead
      added
      3.11only security fixesonly security fixes
      performancePerformance or resource usagePerformance or resource usage
      on Jan 16, 2022
    3. gpshead commented on Jan 16, 2022

      @gpshead
      gpshead
      on Jan 16, 2022
      MemberAuthor
      More actions

      The PR was directly inspired by Mark Dickinson's code in the email thread directly using __asm__ to get the instruction he wanted. There is usually a way to make the compiler actually do what you intend. This appears to be it.

      Interestingly, experimenting with small code snippets rather than the entire longobject.c on gotbolt.org to check various compilers output does not always yield as nice of a result. (clang 11+ showed promise there, but this change benefits gcc equally as well in real world CPython microbenchmark timeit tests). https://godbolt.org/z/63eWPczjx was my playground code.

      $ ./b-clang13/python -m timeit -n 1500000 -s 'x = 10**1000; r=x//10; assert r == 10**999, r' 'x//17'
      1500000 loops, best of 5: 450 nsec per loop
      $ ./b-clang13-new-basic-divrem1/python -m timeit -n 1500000 -s 'x = 10**1000; r=x//10; assert r == 10**999, r' 'x//17'
      1500000 loops, best of 5: 375 nsec per loop
      $ ./b-gcc9/python -m timeit -n 1500000 -s 'x = 10**1000; r=x//10; assert r == 10**999, r' 'x//17'
      1500000 loops, best of 5: 448 nsec per loop
      $ ./b-gcc9-new-basic-divrem1/python -m timeit -n 1500000 -s 'x = 10**1000; r=x//10; assert r == 10**999, r' 'x//17'
      1500000 loops, best of 5: 370 nsec per loop
      

      That's on an AMD zen3 (x86_64). Also tested with other divisors, 17 is not specialized by the compiler. These were not --enable-optimizations builds, though the results remain similar on those for non-specialized values as x//10 turns into when using -fprofile-values on gcc9.

      Performance tests using other architectures forthcoming.

      A pyperformance suite run on a benchmark-stable host is worthwhile. I don't actually expect this to show up as significant in most things there; we'll see.

      The new code is not any more difficult to maintain than the previous code regardless.

    4. gpshead commented on Jan 17, 2022

      @gpshead
      gpshead
      on Jan 17, 2022
      MemberAuthor
      More actions

      I tested my PR branch on 32-bit arm (raspbian bullseye) and the microbenchmark timing shows no change (within the noise across repeated runs). Unsurprising as division is entirely different on 32-bit arm.

      Raspbian uses armv6 for compatibility with the original rpi and rpi0. armv6 does not have an integer division instruction. (how RISCy of it) But that doesn't make a difference in this code as the final 32-bit arm ISA, armv7-a, only has a 32:32 divider. (armv8 aka aarch64 is 64-bit and uses a UDIV as one would expect)

      anyways, that satisfies me that it isn't making anything worse elsewhere.

    5. mdickinson commented on Jan 23, 2022

      @mdickinson
      mdickinson
      on Jan 23, 2022
      Member
      More actions

      New changeset c7f20f1 by Gregory P. Smith in branch 'main':
      bpo-46406: Faster single digit int division. (bpo-30626)
      c7f20f1

    6. mdickinson
      closed this as completedon Jan 23, 2022
    7. mdickinson
      closed this as completedon Jan 23, 2022
    8. tim-one commented on Jan 25, 2022

      @tim-one
      tim-one
      on Jan 25, 2022
      Member
      More actions

      New changeset 7c26472 by Tim Peters in branch 'main':
      bpo-46504: faster code for trial quotient in x_divrem() (GH-30856)
      7c26472

    9. ezio-melotti
      transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.11only security fixesonly security fixesperformancePerformance or resource usagePerformance or resource usage

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions

        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.