Sorry, something went wrong.
| f"-I{CPYTHON / 'Python'}", | ||
| f"-I{CPYTHON / 'Tools' / 'jit'}", | ||
| "-O3", | ||
| # -O2 and -O3 include some optimizations that make sense for |
There was a problem hiding this comment.
Did you investigate -Oz as well? The clang docs are fairly vague, but they say it reduces code size even further, so I'm curious if it's worth investigating as well.
Sorry, something went wrong.
There was a problem hiding this comment.
Nice idea! I'm definitely down to try benchmarking it after this lands.
I suspect it may be quite a bit slower, though. My understanding is that -Os does all of the meaningful performance optimizations except those that increase size, while -Oz will actually hurt performance in pursuit of the smallest possible machine code. Our goal is to be fast, of course, but in this particular case -Os is also just giving us better code (as a side-effect of not aligning jumps or duplicating tails, etc). So smaller isn't necessarily always better.
Sorry, something went wrong.
There was a problem hiding this comment.
Yeah, I'm not sure this is going to be a win. It basically turns off inlining for functions called more than once. For instance, _POP_TWO turns from this on -Os:
Into this on -Oz (outlining PyStackRef_CLOSE makes it 2 bytes shorter, but adds up to three additional jumps):
I'll still try benchmarking it though. But I'll land this PR in the meantime since it's just a one-character change.
Sorry, something went wrong.
There was a problem hiding this comment.
Yep, -Oz is about 1-2% slower across the board.
Sorry, something went wrong.
As the new comment says, upon manual review of -O3, -O2, and -Os, it seems that -Os generates the best code for the JIT's use-case. Perf impact is close to noise, but slightly positive on x86-64 Linux and AArch64 macOS, neutral on AArch64 Linux, and slightly negative on x86-64 Windows. According to the stats, the size of JIT code is down by about 1-2%: https://github.com/faster-cpython/benchmarking-public/blob/main/results/bm-20250628-3.15.0a0-33054dd-JIT/README.md
Here's an example of how skipping tail-duplication removes an extra jump and a duplicate instruction from _POP_TOP (also reducing its size by 19%):
Full diff for the stencils here:
https://gist.github.com/brandtbucher/7340be56f2d2cf7061b5c9bf1c87939c