Files
RustPython/crates
Jeong, YunWon a28ff42345 Flatten Python eval loop with trampoline for reduced call overhead (#8431)
* Factor with_iframe into enter_iframe/exit_iframe helpers

Extract the frame entry (recursion check, TLS link, exception save)
and exit (materialization sync, TLS restore, GC tracking) logic
from with_iframe into standalone enter_iframe/exit_iframe methods.
with_iframe now calls them, with no behavioral change.

This prepares for the trampoline loop where enter/exit are called
individually rather than wrapped around a closure.

Assisted-by: Claude

* Allocate InterpreterFrame and LocalsPlus together on the datastack

Add InterpreterFrame::new_on_datastack() that bump-allocates both the
InterpreterFrame struct and its LocalsPlus data array in a single
datastack push, eliminating one allocation per function call.

Update datastack_frame_size_bytes_for_code() to include InterpreterFrame
size. Convert invoke_prepared_exact_args() and the invoke() fast path to
use the combined allocation.

Add release_datastack_frame() method on InterpreterFrame that drops all
localsplus values, runs field destructors (trace, temporary_refs,
retained_back, etc.), and returns the datastack base pointer for pop.

Assisted-by: Claude

* Implement trampoline loop for Python-to-Python calls

Add ExecutionResult::TailCall variant and a trampoline in
run_frame_fast that flattens Python-to-Python calls into a single
Rust stack frame instead of recursing through the eval loop.

CallPyExactArgs now prepares the callee frame on the datastack and
returns TailCall when tailcall_enabled is set (run_iframe path only).
The trampoline dispatches via a state machine (EnterCallee / ReturnValue
/ Unwind) in a single loop, avoiding mutual recursion between helper
functions that would exhaust the C stack.

Exception propagation through suspended frames uses
trampoline_handle_exception which adds traceback entries and calls
unwind_blocks on each caller.

Assisted-by: Claude

* Optimize trampoline: skip recursion check, avoid temporary_refs mutex, add bound method TailCall

- Add enter_iframe_unchecked for trampoline callee entry (recursion
  already checked by specialization_call_recursion_guard)
- Move callable ownership from per-frame temporary_refs mutex to
  trampoline-local SuspendedFrame.owned_refs via VM side channel
- Add TailCall support for CallBoundMethodExactArgs
- Move args directly from caller stack to callee fastlocals
- Read materialized pointer once in exit_iframe

Incremental call overhead: ~55 ns -> ~35 ns

Assisted-by: Claude

* Use UnsafeCell for pending_tailcall_refs

The VM is per-thread so RefCell's runtime borrow checking is
unnecessary overhead. Replace with UnsafeCell for direct access.

Assisted-by: Claude

* Remove .claude/settings.json from tracking

Assisted-by: Claude

* Replace SendPtr with Option<SendNonNull> for type-safe pending tailcall

Use NonNull + Option instead of raw *mut T with manual null checks.
The compiler enforces non-null via the type system, and Option<NonNull>
has the same size as a raw pointer thanks to niche optimization.
Also extract take_pending_tailcall helper to deduplicate the pattern.

Assisted-by: Claude

* Restrict PendingFrame to private, expose only set/take methods

Rename SendNonNull to PendingFrame and make it fully private: the
struct, its field, and the pending_tailcall_frame Cell are all
non-pub. External code accesses the side channel only through
set_pending_tailcall (pub(crate)) and take_pending_tailcall (private).

This ensures the unsafe Send+Sync impl cannot be reused elsewhere
without justifying a new safety argument.

Assisted-by: Claude

* Restore C-stack overflow check in trampoline enter_iframe_unchecked

enter_iframe_unchecked was skipping C-stack checks under the
assumption that the trampoline stays in one Rust stack frame. But
each run_iframe call still consumes Rust stack, so deep Python
recursion through the trampoline can exhaust the C stack (observed
as STATUS_STACK_OVERFLOW on Windows CI).

Keep the C-stack check (every 8th call) while still skipping the
Python recursion depth check (already done by
specialization_call_recursion_guard).

Assisted-by: Claude

* Address code review: entry frame double-free, panic safety, cleanup

- Fix double-free: mark entry frame with is_entry flag in
  SuspendedFrame so the trampoline skips its datastack release
  (the caller owns that cleanup)
- Clear iframe.previous in exit_iframe before unlinking the chain,
  matching with_frame and resume_gen_frame behavior
- Add scopeguard in with_iframe for panic safety
- Use saturating_sub(1) for lasti in trampoline_handle_exception
- Extract datastack_iframe_localsplus_offset helper to avoid
  duplicated alignment computation

Assisted-by: Claude
2026-08-05 13:10:14 +09:00
..
2026-03-31 21:05:08 +09:00
2026-07-12 12:35:00 +00:00
2026-06-30 23:40:29 +09:00
2026-03-31 21:05:08 +09:00