You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Measure Windows performance (and improve if lacking) #321
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).
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) .
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.
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.
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.
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:
(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).
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."
Reacted by Shantanu, Jelle Zijlstra, Damian Shaw, Dennis Sweeney and Yichen Yan
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!
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)
Activity
markshannon commentedon Mar 21, 2022
Would it be a viable option to use Clang instead of MSVC for Windows?
kumaraditya303 commentedon Mar 21, 2022
I have got it compiling under clang on windows in debug build
gvanrossum commentedon Mar 21, 2022
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) .
gvanrossum commentedon Mar 21, 2022
@zooba Do you see a problem with using clang instead of MSVC on Windows, assuming we can get it working?
ivoflipse commentedon Mar 21, 2022
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.
gvanrossum commentedon Mar 21, 2022
@ivoflipse
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?
zooba commentedon Mar 21, 2022
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.
gvanrossum commentedon Mar 21, 2022
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
sweeneyde commentedon Mar 21, 2022
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.
neonene commentedon Mar 22, 2022
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 inpymacro.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
21 remaining items
gvanrossum commentedon Apr 18, 2022
I will try to create branches later this week.
gvanrossum commentedon Apr 19, 2022
Looking at this again I realized that
opcodeis declared asintso 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:gvanrossum commentedon Apr 19, 2022
Well I'll be darned. Now the code effectively boils down to this, where initially
rcxis the opcode:(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:
(which was the "compact_table" from above).
Thus the new code is now equivalent to
which is one memory load less than before (where opcode was an int).
Let me see if we actually need the dummy cases.
gvanrossum commentedon Apr 19, 2022
Yeah, with just a
default:label, the extra memory load is back:Next I'll investigate whether the dummy cases need to contain any code.
gvanrossum commentedon Apr 19, 2022
Fortunately, the dummy cases don't need to do anything. I will prepare a separate PR for this.
Make MSVC generate somewhat faster switch code
gvanrossum commentedon Apr 20, 2022
Here's the PR. (Code is included in comments above.) python/cpython#91718
neonene commentedon Apr 20, 2022
This approach makes
PREDICT()macro unnecessary for me.GH-91719: Make MSVC generate somewhat faster switch code (#91718)
neonene commentedon Apr 22, 2022
3.10.xPGO does not need a fix aroundswitch(opcode).We can just compare
3.11's performance with current3.10.gvanrossum commentedon Apr 23, 2022
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:
"I close the issue."
gvanrossum commentedon Apr 23, 2022
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!
gvanrossum commentedon Apr 23, 2022
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:
locale.getencoding()instead of getpreferredencoding (GH-91732)