Visitar URL original
gh-93325: Micro-optimization of small integer lookup by eendebakpt · Pull Request #93326 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20260525005606/https://github.com/python/cpython/pull/93326
Skip to content
/ cpython Public

gh-93325: Micro-optimization of small integer lookup#93326

Closed
eendebakpt wants to merge 4 commits into
python:mainfrom
eendebakpt:small_int_offset
Closed

gh-93325: Micro-optimization of small integer lookup#93326
eendebakpt wants to merge 4 commits into
python:mainfrom
eendebakpt:small_int_offset

Conversation

@eendebakpt
Copy link
Copy Markdown
Contributor

@eendebakpt eendebakpt commented May 28, 2022 •

The small integers are accessed through _PyLong_SMALL_INTS. This requires adding the constant _PY_NSMALLNEGINTS to the integer desired. This PR adds a new global variable _PyLong_SMALL_INTS_ZERO_OFFSET that points to the zero integer.

Advantages:

  • Methods like get_small_int, _PyLong_GetZero, _PyLong_GetOne require an addition operation less.
  • Since _PyLong_GetZero and _PyLong_GetOne are inline functions, less code is generated

Benchmark:

Mean +- std dev: [main] 1.79 ms +- 0.10 ms -> [PR] 1.69 ms +- 0.18 ms: 1.06x faster
Test script
import pyperf
import time
runner = pyperf.Runner()

code=""" 
for ii in range(200):
    for jj in range(200):
        a = ii - jj
        b= ii+ jj
"""        

runner.timeit('small int test', stmt=code)

Notes:

  • This PR adds a new singleton small_ints_zero_offset. This extra global can be avoided if we: first allocate small_ints, then add _PY_NSMALLNEGINTS to the pointer. For deallocating one first has to substract _PY_NSMALLNEGINTS again. This is a little more complex, but if preferred I will adapt the PR to this.
  • I do not have sufficient knowledge of the Python initialization process to determine the best location to define and initialize the small_ints_zero_offset pointer. Suggestions for improvement are welcome.

Fixes #93325.

@AlexWaygood AlexWaygood added type-feature A feature request or enhancement performance Performance or resource usage labels May 28, 2022
@mdickinson mdickinson self-requested a review May 29, 2022 06:40
@mdickinson
Copy link
Copy Markdown
Member

Thanks for the PR.

How repeatable are those timings? A 6% speed up would be impressive, but it's well within the margin of error, and seems surprisingly large for what should be removal of a single assembly instruction (and possibly not even that - on Intel hardware, we could just be replacing an LEA instruction with an addition, keeping the total number of instructions the same).

As an experiment, I've been running your script in a loop, alternating between commit b8135e9 on this branch and commit dec1e93 on main. This is on macOS / Intel, with both branches configured and built with ./configure --enable-optimizations. The numbers are quite variable, but so far I'm not seeing a convincing performance difference in either direction. Here's a sample:

main .....................
small int test: Mean +- std dev: 1.26 ms +- 0.03 ms

zero-offset .....................
small int test: Mean +- std dev: 1.25 ms +- 0.02 ms

main .....................
small int test: Mean +- std dev: 1.25 ms +- 0.03 ms

zero-offset .....................
small int test: Mean +- std dev: 1.26 ms +- 0.02 ms

main .....................
small int test: Mean +- std dev: 1.25 ms +- 0.02 ms

zero-offset .....................
small int test: Mean +- std dev: 1.25 ms +- 0.01 ms

main .....................
small int test: Mean +- std dev: 1.26 ms +- 0.03 ms

zero-offset .....................
small int test: Mean +- std dev: 1.26 ms +- 0.02 ms

main .....................
small int test: Mean +- std dev: 1.25 ms +- 0.02 ms

zero-offset .....................
small int test: Mean +- std dev: 1.27 ms +- 0.04 ms

@mdickinson
Copy link
Copy Markdown
Member

Looking at the assembly on my machine, I do see a difference: here's what I typically see for the lookup on main (not surprisingly, get_small_int is inlined everywhere, so there are multiple versions of the code, but I've tried to pick typical versions):

	shlq	$5, %rax
	movq	__PyRuntime@GOTPCREL(%rip), %rcx
	leaq	(%rcx,%rax), %r14
	addq	$736, %r14                      ## imm = 0x2E0

And here's the corresponding code in this branch:

	movq	__PyRuntime@GOTPCREL(%rip), %rax
	movq	9120(%rax), %rax
	shlq	$5, %r15
	leaq	(%rax,%r15), %r14

@mdickinson
Copy link
Copy Markdown
Member

Conclusion: I'm not yet seeing any evidence that this micro-optimization is actually an optimization. :-)

@markshannon Any thoughts on this PR?

@mdickinson mdickinson requested a review from markshannon May 29, 2022 08:30
@markshannon
Copy link
Copy Markdown
Member

This is going to be slower.

It replaces an add with a load, which is almost always going to be slower, and never faster.

@sweeneyde
Copy link
Copy Markdown
Member

In many situations, compilers may be able to constant-fold small_ints[_PY_NSMALLNEGINTS + i] as (small_ints + _PY_NSMALLNEGINTS)[i] anyway.

@eendebakpt
Copy link
Copy Markdown
Contributor Author

@mdickinson @markshannon @sweeneyde Thanks for the responses. I did some more testing. The timings reported above are repeatable, but have been taken on a Windows system with the default build from the Visual Studio build files. I repeated the experiments, but now with binaries generated with the command build.bat --pgo. With this build configuration the running times are the same within random fluctuations. (not sure they are identical, but any improvement seems to small to further investigate)

I am still amazed that the compiler can handle this, but the numbers show no speedup so I will close this PR.

@eendebakpt eendebakpt closed this May 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review performance Performance or resource usage type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Micro-optimization of small integer lookup

6 participants