Skip to content

GH-135379: Specialize int operations for compact ints only #135668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 19, 2025

Conversation

markshannon
Copy link
Member

@markshannon markshannon commented Jun 18, 2025

This changes the way we handle ints during specialization.
Instead of handling all ints we only specialize for compact ints.
Since compact ints represent the large majority of ints we get a faster common path, at the cost of a few more de-optimizations.
Using compact ints only, and de-optimizing in the extremely rare no-memory case, we can simplify the performance critical paths for integer operations.

This is helpful for top-of-stack caching as the newer simpler instructions to not escape or need to handle errors, so avoids the need to spill to memory.

This also gives us the potential to use BINARY_OP_EXTEND for multi-digit ints in the future.

@Fidget-Spinner
Copy link
Member

Can you please add tests for the new synbol to optimizer_symbols.c?

Copy link
Member

@Fidget-Spinner Fidget-Spinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need tests and one comment. Otherwise LGTM.

return;
case JIT_SYM_KNOWN_VALUE_TAG:
if (!PyLong_CheckCompact(sym->value.value)) {
Py_CLEAR(sym->value.value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to clear things here? It's automatically cleared on finalization of the whole optimizer I think.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to do it here (we do the same in _Py_uop_sym_set_type) as we are changing the tag, so the finalization won't know there was an object there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AH ok that's very tricky! Good observation.

@markshannon
Copy link
Member Author

Performance is about neutral, all within noise:

  • Linux ARM 0.2% slower
  • Linux x86 1.4% faster
  • Windows x86 1.0% slower
  • Mac ARM 0.0% slower

Which is good, as the main point of this PR is to ultimately improve tier 2 performance as this will enable better TOS caching and easier implementation of tagged ints.

@markshannon markshannon merged commit 9731dd2 into python:main Jun 19, 2025
72 checks passed
@bedevere-bot
Copy link

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 CentOS9 NoGIL 3.x (tier-1) has failed when building commit 9731dd2.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1609/builds/2948) and take a look at the build logs.
  4. Check if the failure is related to this commit (9731dd2) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1609/builds/2948

Failed tests:

  • test_cext

Failed subtests:

  • test_build - test.test_cext.TestExt.test_build
  • test_build_c11 - test.test_cext.TestExt.test_build_c11
  • test_build_c99 - test.test_cext.TestExt.test_build_c99

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 34, in test_build
    self.check_build('_test_cext')
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 57, in check_build
    self._check_build(extension_name, python_exe,
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                      std=std, limited=limited)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 94, in _check_build
    run_cmd('Install', cmd)
    ~~~~~~~^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 75, in run_cmd
    subprocess.run(cmd, check=True, env=env)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/subprocess.py", line 577, in run
    raise CalledProcessError(retcode, process.args,
                             output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/build/test_python_2169572æ/tempcwd/env/bin/python', '-X', 'dev', '-m', 'pip', 'install', '--no-build-isolation', '/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/build/test_python_2169572æ/tempcwd/pkg', '-v']' returned non-zero exit status 1.


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 34, in test_build
    self.check_build('_test_cext')
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 57, in check_build
    self._check_build(extension_name, python_exe,
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                      std=std, limited=limited)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 94, in _check_build
    run_cmd('Install', cmd)
    ~~~~~~~^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 75, in run_cmd
    subprocess.run(cmd, check=True, env=env)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/subprocess.py", line 577, in run
    raise CalledProcessError(retcode, process.args,
                             output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/build/test_python_2213783æ/tempcwd/env/bin/python', '-X', 'dev', '-m', 'pip', 'install', '--no-build-isolation', '/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/build/test_python_2213783æ/tempcwd/pkg', '-v']' returned non-zero exit status 1.


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 37, in test_build_c11
    self.check_build('_test_c11_cext', std='c11')
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 57, in check_build
    self._check_build(extension_name, python_exe,
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                      std=std, limited=limited)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 94, in _check_build
    run_cmd('Install', cmd)
    ~~~~~~~^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 75, in run_cmd
    subprocess.run(cmd, check=True, env=env)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/subprocess.py", line 577, in run
    raise CalledProcessError(retcode, process.args,
                             output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/build/test_python_2213783æ/tempcwd/env/bin/python', '-X', 'dev', '-m', 'pip', 'install', '--no-build-isolation', '/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/build/test_python_2213783æ/tempcwd/pkg', '-v']' returned non-zero exit status 1.


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 44, in test_build_c99
    self.check_build('_test_c99_cext', std='c99')
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 57, in check_build
    self._check_build(extension_name, python_exe,
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                      std=std, limited=limited)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 94, in _check_build
    run_cmd('Install', cmd)
    ~~~~~~~^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 75, in run_cmd
    subprocess.run(cmd, check=True, env=env)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/subprocess.py", line 577, in run
    raise CalledProcessError(retcode, process.args,
                             output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/build/test_python_2213783æ/tempcwd/env/bin/python', '-X', 'dev', '-m', 'pip', 'install', '--no-build-isolation', '/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/build/test_python_2213783æ/tempcwd/pkg', '-v']' returned non-zero exit status 1.


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 37, in test_build_c11
    self.check_build('_test_c11_cext', std='c11')
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 57, in check_build
    self._check_build(extension_name, python_exe,
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                      std=std, limited=limited)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 94, in _check_build
    run_cmd('Install', cmd)
    ~~~~~~~^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 75, in run_cmd
    subprocess.run(cmd, check=True, env=env)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/subprocess.py", line 577, in run
    raise CalledProcessError(retcode, process.args,
                             output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/build/test_python_2169572æ/tempcwd/env/bin/python', '-X', 'dev', '-m', 'pip', 'install', '--no-build-isolation', '/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/build/test_python_2169572æ/tempcwd/pkg', '-v']' returned non-zero exit status 1.


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 44, in test_build_c99
    self.check_build('_test_c99_cext', std='c99')
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 57, in check_build
    self._check_build(extension_name, python_exe,
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                      std=std, limited=limited)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 94, in _check_build
    run_cmd('Install', cmd)
    ~~~~~~~^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/test/test_cext/__init__.py", line 75, in run_cmd
    subprocess.run(cmd, check=True, env=env)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/Lib/subprocess.py", line 577, in run
    raise CalledProcessError(retcode, process.args,
                             output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/build/test_python_2169572æ/tempcwd/env/bin/python', '-X', 'dev', '-m', 'pip', 'install', '--no-build-isolation', '/home/buildbot/buildarea/3.x.itamaro-centos-aws.nogil/build/build/test_python_2169572æ/tempcwd/pkg', '-v']' returned non-zero exit status 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants