Skip to content

gh-131798: JIT: Optimize _CHECK_IS_NOT_PY_CALLABLE_EX and _CHECK_IS_NOT_PY_CALLABLE_KW#148494

Merged
Fidget-Spinner merged 2 commits intopython:mainfrom
Wulian233:_CHECK_IS_NOT_PY_CALLABLE_KW
Apr 13, 2026
Merged

gh-131798: JIT: Optimize _CHECK_IS_NOT_PY_CALLABLE_EX and _CHECK_IS_NOT_PY_CALLABLE_KW#148494
Fidget-Spinner merged 2 commits intopython:mainfrom
Wulian233:_CHECK_IS_NOT_PY_CALLABLE_KW

Conversation

@Wulian233
Copy link
Copy Markdown
Contributor

@Wulian233 Wulian233 commented Apr 13, 2026

While implementing this, I ran into a confusing problem. As shown in the diff below, when I assign f = sorted instead of calling sorted() directly, the JIT fails to recognize the optimization, causing the test to fail.

  def test_check_is_not_py_callable_kw(self):
      def testfunc(n):
          total = 0
-         xs = (3, 1, 2)
-         for _ in range(n):
-             total += sorted(xs, reverse=False)[0]
+         f = sorted
+         xs = (3, 1, 2)
+         for _ in range(n):
+             total += f(xs, reverse=False)[0]
          return total

      res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
      self.assertEqual(res, TIER2_THRESHOLD)
      self.assertIsNotNone(ex)
      uops = get_opnames(ex)
      self.assertNotIn("_CHECK_IS_NOT_PY_CALLABLE_KW", uops)

Edit: Please add skip news

@Fidget-Spinner
Copy link
Copy Markdown
Member

While implementing this, I ran into a confusing problem. As shown in the diff below, when I assign f = sorted instead of calling sorted() directly, the JIT fails to recognize the optimization, causing the test to fail.

That's not really a problem, just how the JIT behaves. We only compile the region inside the for loop first, and then a function only if it's hot enough. As for the code example above you placed the sorted outside the for loop, the JIT has no visibility of it.

@Wulian233
Copy link
Copy Markdown
Contributor Author

oh I understand, thank you!

Copy link
Copy Markdown
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.

LGTM. Thank you.

@Fidget-Spinner Fidget-Spinner merged commit afaf58b into python:main Apr 13, 2026
77 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants