Skip to content

Measure Windows performance (and improve if lacking) #321

Closed

Description

@gvanrossum

There was a report that on Windows the performance on the PyPerformance benchmark is only 3% better than for 3.10. We should try to confirm or refute this result, and if confirmed, attempt to improve the situation.

Likely culprit is the optimizer (or PGO or LTO) giving up on inlining in the main function in ceval.c. In particular it might be the INCREF/DECREF inline functions. A possible hack would be to turn these back into macros again at the top of ceval.c (if no debugging options are defined).

Activity

  1. self-assigned this
    on Mar 14, 2022
  2. moved this from Todo to In Progress in Fancy CPython Boardon Mar 14, 2022
  3. markshannon commented on Mar 21, 2022

    @markshannon
    Member

    Would it be a viable option to use Clang instead of MSVC for Windows?

  4. kumaraditya303 commented on Mar 21, 2022

    @kumaraditya303

    I have got it compiling under clang on windows in debug build

  5. gvanrossum commented on Mar 21, 2022

    @gvanrossum
    CollaboratorAuthor

    FWIW my own measurements on Windows show much better perf than that, but are inconclusive due to noise (we're waiting for a dedicated perf machine running Windows). See #315 (comment) .

  6. gvanrossum commented on Mar 21, 2022

    @gvanrossum
    CollaboratorAuthor

    @zooba Do you see a problem with using clang instead of MSVC on Windows, assuming we can get it working?

  7. ivoflipse commented on Mar 21, 2022

    @ivoflipse

    Wouldn't that mean that anyone building C extensions would also have to switch over to Clang?

    If so, that would require some coordination (e.g. cibuildwheel, conda-forge, numpy/scipy) to make such a switch and support two compilers if you want to make C extensions available for multiple Python versions.

  8. gvanrossum commented on Mar 21, 2022

    @gvanrossum
    CollaboratorAuthor

    @ivoflipse

    Wouldn't that mean that anyone building C extensions would also have to switch over to Clang?

    I don't know. I have no experience with this topic at all, which is why I'm asking @zooba for advice.

    @kumaraditya303 Can you point to your branch here?

  9. zooba commented on Mar 21, 2022

    @zooba

    Yeah, that's a massive ecosystem change that we're not ready for. It almost certainly will affect the ABI in some way, though it might be manageable, and it'll definitely impact/break any build scripts that implicitly or explicitly assume MSVC. As I work through ARM64 support, I'm finding that is "many"... (unless, of course, it turns out to be perfectly safe to link between the two compilers, and then it'll only affect static lib users, which is practically nobody AFAIK).

    Figuring out for certain if the size of ceval is causing it to de-optimise and then sensibly breaking it up is much easier.

  10. gvanrossum commented on Mar 21, 2022

    @gvanrossum
    CollaboratorAuthor

    FWIW @neonene came up with a PR that temporarily enables a flag that shows the inlining decisions. I'll look into this.

    https://bugs.python.org/issue45116#msg406471

  11. sweeneyde commented on Mar 21, 2022

    @sweeneyde

    I have a branch python/cpython@main...sweeneyde:ops_to_funcs that converts several of the longer opcodes into functions, in case that is useful for comparison.

  12. neonene commented on Mar 22, 2022

    @neonene

    Macro benchmarks' improvements are unstable for me in the range of 8~20% compared to 3.10.1+.

    When building with PGO (only), changing the definition of Py_UNREACHABLE() could be an improvement. For example, replacing __assume(0) with a no-return-noop function in pymacro.h. But __assume(0) is not the only suspect that stops the optimizer.

    https://devblogs.microsoft.com/cppblog/visual-studio-2017-throughput-improvements-and-advice/#remove-usages-of-__assume

  13. moved this from In Progress to In Review in Fancy CPython Boardon Mar 28, 2022
  14. 21 remaining items

  15. gvanrossum commented on Apr 18, 2022

    @gvanrossum
    CollaboratorAuthor

    I will try to create branches later this week.

  16. gvanrossum commented on Apr 19, 2022

    @gvanrossum
    CollaboratorAuthor

    Looking at this again I realized that opcode is declared as int so maybe that's why the switch doesn't optimize as expected. I'll look into this some more, starting by changing the switch line to this:

    switch ((unsigned char)opcode) {
    
  17. gvanrossum commented on Apr 19, 2022

    @gvanrossum
    CollaboratorAuthor

    Well I'll be darned. Now the code effectively boils down to this, where initially rcx is the opcode:

    00007FF8FF9A7854  lea         rax,[`__local_stdio_scanf_options'::`2'::_OptionsStorage (07FF8FF970000h)]  
    00007FF8FF9A7871  mov         eax,dword ptr [rax+rcx*4+3F9D4h]  
    00007FF8FF9A787B  lea         rcx,[`__local_stdio_scanf_options'::`2'::_OptionsStorage (07FF8FF970000h)]  
    00007FF8FF9A7886  add         rax,rcx  
    00007FF8FF9A788F  jmp         rax  
    

    (I left some instructions out that are interleaved but don't touch any of these and seem to be related to flushing registers to the stack or moving them around.)

    Crucially, there's nothing like this to be found, which I found in last week's investigations:

    movzx       eax,byte ptr [rcx+rax+1F778h]  
    

    (which was the "compact_table" from above).

    Thus the new code is now equivalent to

    goto *(base + offset_table[opcode]);
    

    which is one memory load less than before (where opcode was an int).

    Let me see if we actually need the dummy cases.

  18. gvanrossum commented on Apr 19, 2022

    @gvanrossum
    CollaboratorAuthor

    Yeah, with just a default: label, the extra memory load is back:

    00007FF8F031789A  movzx       eax,byte ptr [rcx+rax+3DD70h]  
    00007FF8F03178A2  mov         ecx,dword ptr [rcx+rax*4+3DA9Ch]  
    00007FF8F03178A9  lea         rax,[`__local_stdio_scanf_options'::`2'::_OptionsStorage (07FF8F02E0000h)]  
    00007FF8F03178B0  add         rcx,rax  
    00007FF8F03178B3  jmp         rcx  
    

    Next I'll investigate whether the dummy cases need to contain any code.

  19. gvanrossum commented on Apr 19, 2022

    @gvanrossum
    CollaboratorAuthor

    Fortunately, the dummy cases don't need to do anything. I will prepare a separate PR for this.

  20. gvanrossum commented on Apr 20, 2022

    @gvanrossum
    CollaboratorAuthor

    Here's the PR. (Code is included in comments above.) python/cpython#91718

  21. neonene commented on Apr 20, 2022

    @neonene

    This approach makes PREDICT() macro unnecessary for me.

  22. added a commit that references this issue on Apr 21, 2022
  23. neonene commented on Apr 22, 2022

    @neonene

    3.10.x PGO does not need a fix around switch(opcode).
    We can just compare 3.11's performance with current 3.10.

  24. gvanrossum commented on Apr 23, 2022

    @gvanrossum
    CollaboratorAuthor

    I've done the best measurements I can on my corporate laptop, and I conclude that on Windows 3.11 is now ~20% faster than 3.10. This is main at revision 2f233fceae.

    Things I did to stabilize the benchmarks:

    • Built Python 3.10.4 (that exact tag) from source with PGO, so there's no doubt about differences due to different build environments.
    • Used PyPerformance 1.0.5, which has the necessary tweaks to run all benchmarks on Windows.
    • Fixed the CPU speed at 85% of max using powercfg.exe.
    • Put the Python installation on a separate partition (the OS partition is kind of noisy).
    • Closed all application windows except a terminal window where I run the commands, and minimize that.
    • Used AutoClicker.exe set to click once per 5 minutes to avoid the machine going to sleep (it's a corporate machine and the profile doesn't allow me to disable the screensaver).
    • Tried to set airplane mode once the first benchmark starts. (Alas, the VM setup code fails without network access -- possibly only for those deps that use a GitHub URL.) For the second 43.11 run I wasn't around to do this, but it was at most 1% slower.
    • Ran the benchmarks for each version compared twice. These results came within 1% of each other.

    "I close the issue."

  25. Repository owner moved this from In Progress to Done in Fancy CPython Boardon Apr 23, 2022
  26. gvanrossum commented on Apr 23, 2022

    @gvanrossum
    CollaboratorAuthor

    PS. I now also benchmarked 3.11 just before my first Windows perf commit (i.e., just before GH-91719). That one was only 7% faster than 3.10.4, and current head is 13% faster than that. So one or both of these did have a big effect!

  27. gvanrossum commented on Apr 23, 2022

    @gvanrossum
    CollaboratorAuthor

    I ran one more set of benchmarks, and conclude that the switch improvement (GH-91719) contributed 3% and the macro-fied inline functions (GH-89279) contributed the remaining 10%.

    This is assuming none of the intervening commits contributed performance. The list of commits including the endpoints:

    • (benchmark here: 20% faster than 3.10.4)
    • 2f233fceae gh-89279: In ceval.c, redefine some macros for speed (#32387)
    • 9fe82d0b70 gh-91764: Mark version typing.Unpack and LiteralString were added in (#91817)
    • 1218061609 gh-89653: PEP 670: Amend docs (GH-91813)
    • 9e146bbb7e gh-80527: Deprecate PEP 623 Unicode functions (#91801)
    • 82ec638ab7 Docs: Clarify availability of PyOS_CheckStack (GH-91816)
    • bcf14ae433 gh-91291: Accept attributes as keyword arguments in decimal.localcontext (#32242)
    • 5e130a8da4 bpo-43224: Implement pickling of TypeVarTuples (#32119)
    • 2551a6c92f gh-91217: deprecate-pipes (GH-91779)
    • 29afb7d2ef gh-69093: Add indexing and slicing support to sqlite3.Blob (#91599)
    • 1317b70f89 gh-91156: Use locale.getencoding() instead of getpreferredencoding (GH-91732)
    • efe7fd4170 gh-89653: Add assertions to unicodeobject.h functions (#91800)
    • 636ad7b47e gh-89653: PEP 670: Convert unicodeobject.h macros to functions (#91799)
    • 364ed94092 gh-89373: _Py_Dealloc() checks tp_dealloc exception (#32357)
    • 8a4e519e78 gh-79315: Add Include/cpython/pythread.h header (#91798)
    • 6f9addb5ba gh-79315: Add Include/cpython/modsupport.h header (#91797)
    • c1474fa6c6 gh-89653: PEP 670: Group deprecated API in unicodeobject.h (#91796)
    • eaa85cb22f gh-91768: C API no longer use "const PyObject*" type (#91769)
    • 128d624117 gh-89653: PEP 670: Convert unicodeobject.h macros to functions (#91773)
    • (benchmark here: 10% faster than 3.10.4)
    • f8dc6186d1 GH-91719: Make MSVC generate somewhat faster switch code (#91718)
    • (benchmark here: 7% faster than 3.10.4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

Sponsor
SponsoredKunjungi sekarang
Promo