Skip to content

Conversation

@markshannon
Copy link
Member

@markshannonmarkshannon commented Mar 6, 2024

Since the globals and builtin fields of function objects are immutable, we can check the function instead of the globals and builtins.

This is:

  • Never slower, we never add more checks
  • Faster for inlined calls as _INIT_CALL_PY_EXACT_ARGS implicitly checks the function, so no additional checks are needed.
  • Faster in many other cases, as we can replace _CHECK_GLOBALS + _CHECK_BUILTINS with _CHECK_FUNCTION.

@gvanrossum
Copy link
Member

@markshannon, Is this correct? WhenI run a really simple test with debug output, I get a DEOPT message for _CHECK_FUNCTION for each iteration; before this PR I only got one DEOPT message at the end of the loop.

Code:

import_testinternalcapidefcast(typ, val): returnvaldeftestfunc(n): x=0foriinrange(n): x=cast(int, i) returnxdefmain(): count=20a=testfunc(count) asserta+1==count, (a, count) old_opt=_testinternalcapi.get_optimizer() opt=_testinternalcapi.new_uop_optimizer() _testinternalcapi.set_optimizer(opt) a=testfunc(count) _testinternalcapi.set_optimizer(old_opt) asserta+1==count, (a, count) main()

Before:

Optimized executor (length 27): 0 OPTIMIZED: _START_EXECUTOR (52685, target=-842150451, operand=12a70eb70) 1 OPTIMIZED: _CHECK_VALIDITY (0, target=33, operand=1031faf9a) 2 OPTIMIZED: _SET_IP (0, target=14, operand=1031faf74) 3 OPTIMIZED: _ITER_CHECK_RANGE (19, target=14, operand=0) 4 OPTIMIZED: _GUARD_NOT_EXHAUSTED_RANGE (19, target=14, operand=0) 5 OPTIMIZED: _ITER_NEXT_RANGE (19, target=14, operand=0) 6 OPTIMIZED: _CHECK_VALIDITY (0, target=16, operand=1031faf78) 7 OPTIMIZED: _STORE_FAST_2 (2, target=16, operand=0) 8 OPTIMIZED: _CHECK_GLOBALS (3, target=17, operand=10330f050) 9 OPTIMIZED: _LOAD_CONST_INLINE_WITH_NULL (3, target=17, operand=1032fa150) 10 OPTIMIZED: _CHECK_BUILTINS (4, target=22, operand=10323fa70) 11 OPTIMIZED: _LOAD_CONST_INLINE_BORROW (4, target=22, operand=102e39508) 12 OPTIMIZED: _LOAD_FAST_2 (2, target=27, operand=0) 13 OPTIMIZED: _SET_IP (0, target=28, operand=1031faf90) 14 OPTIMIZED: _CHECK_FUNCTION_EXACT_ARGS (2, target=28, operand=2c9) 15 OPTIMIZED: _CHECK_STACK_SPACE (2, target=28, operand=0) 16 OPTIMIZED: _INIT_CALL_PY_EXACT_ARGS_2 (2, target=28, operand=0) 17 OPTIMIZED: _SAVE_RETURN_OFFSET (4, target=28, operand=0) 18 OPTIMIZED: _PUSH_FRAME (2, target=28, operand=1032fa150) 19 OPTIMIZED: _CHECK_VALIDITY (0, target=0, operand=1033690f8) 20 OPTIMIZED: _RESUME_CHECK (0, target=0, operand=0) 21 OPTIMIZED: _LOAD_FAST_1 (1, target=1, operand=0) 22 OPTIMIZED: _SET_IP (0, target=2, operand=1033690fc) 23 OPTIMIZED: _POP_FRAME (0, target=2, operand=10336fa10) 24 OPTIMIZED: _CHECK_VALIDITY (0, target=32, operand=1031faf98) 25 OPTIMIZED: _STORE_FAST_1 (1, target=32, operand=0) 26 OPTIMIZED: _JUMP_TO_TOP (0, target=0, operand=0) DEOPT: [UOp _GUARD_NOT_EXHAUSTED_RANGE (19, target=14, operand=0) -> FOR_ITER_RANGE] 

After:

Optimized executor (length 26): 0 OPTIMIZED: _START_EXECUTOR (52685, target=-842150451, operand=12670ec50) 1 OPTIMIZED: _CHECK_VALIDITY (0, target=33, operand=100dd2f9a) 2 OPTIMIZED: _SET_IP (0, target=14, operand=100dd2f74) 3 OPTIMIZED: _ITER_CHECK_RANGE (19, target=14, operand=0) 4 OPTIMIZED: _GUARD_NOT_EXHAUSTED_RANGE (19, target=14, operand=0) 5 OPTIMIZED: _ITER_NEXT_RANGE (19, target=14, operand=0) 6 OPTIMIZED: _CHECK_VALIDITY (0, target=16, operand=100dd2f78) 7 OPTIMIZED: _STORE_FAST_2 (2, target=16, operand=0) 8 OPTIMIZED: _CHECK_FUNCTION (3, target=17, operand=100ee7050) 9 OPTIMIZED: _LOAD_CONST_INLINE_WITH_NULL (3, target=17, operand=100ed2210) 10 OPTIMIZED: _LOAD_CONST_INLINE_BORROW (4, target=22, operand=100a11508) 11 OPTIMIZED: _LOAD_FAST_2 (2, target=27, operand=0) 12 OPTIMIZED: _SET_IP (0, target=28, operand=100dd2f90) 13 OPTIMIZED: _CHECK_FUNCTION_EXACT_ARGS (2, target=28, operand=2c9) 14 OPTIMIZED: _CHECK_STACK_SPACE (2, target=28, operand=0) 15 OPTIMIZED: _INIT_CALL_PY_EXACT_ARGS_2 (2, target=28, operand=0) 16 OPTIMIZED: _SAVE_RETURN_OFFSET (4, target=28, operand=0) 17 OPTIMIZED: _PUSH_FRAME (2, target=28, operand=100ed2210) 18 OPTIMIZED: _CHECK_VALIDITY (0, target=0, operand=100f410f8) 19 OPTIMIZED: _RESUME_CHECK (0, target=0, operand=0) 20 OPTIMIZED: _LOAD_FAST_1 (1, target=1, operand=0) 21 OPTIMIZED: _SET_IP (0, target=2, operand=100f410fc) 22 OPTIMIZED: _POP_FRAME (0, target=2, operand=100f47ad0) 23 OPTIMIZED: _CHECK_VALIDITY (0, target=32, operand=100dd2f98) 24 OPTIMIZED: _STORE_FAST_1 (1, target=32, operand=0) 25 OPTIMIZED: _JUMP_TO_TOP (0, target=0, operand=0) DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE] DEOPT: [UOp _GUARD_NOT_EXHAUSTED_RANGE (19, target=14, operand=0) -> FOR_ITER_RANGE] 

That's the _CHECK_FUNCTION call at index 8 deoptimizing every time.

@brandtbucher
Copy link
Member

@markshannon, Is this correct? WhenI run a really simple test with debug output, I get a DEOPT message for _CHECK_FUNCTION for each iteration; before this PR I only got one DEOPT message at the end of the loop.

Yep, the bug is that this PR doesn't update the operand, just the opcode. So it's incorrectly comparing the function to the builtin or globals dicts.

@markshannon
Copy link
MemberAuthor

#116460

adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 25, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
@markshannonmarkshannon deleted the globals-check-to-function-check branch August 6, 2024 10:18
@markshannonmarkshannon restored the globals-check-to-function-check branch August 15, 2025 14:33
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@markshannon@gvanrossum@brandtbucher@Fidget-Spinner