]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-138122: Implement frame caching in RemoteUnwinder to reduce memory reads (#142137)
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Sat, 6 Dec 2025 22:37:34 +0000 (22:37 +0000)
committerGitHub <noreply@github.com>
Sat, 6 Dec 2025 22:37:34 +0000 (22:37 +0000)
commit572c780aa875e4eb00961743f216214ce6d03f9f
tree90555f4a317ba2b174a772741e42fefd3cd9b201
parent332da6295f365b09cabf30a9222323b056ab1410
gh-138122: Implement frame caching in RemoteUnwinder to reduce memory reads (#142137)

This PR implements frame caching in the RemoteUnwinder class to significantly reduce memory reads when profiling remote processes with deep call stacks.

When cache_frames=True, the unwinder stores the frame chain from each sample and reuses unchanged portions in subsequent samples. Since most profiling samples capture similar call stacks (especially the parent frames), this optimization avoids repeatedly reading the same frame data from the target process.

The implementation adds a last_profiled_frame field to the thread state that tracks where the previous sample stopped. On the next sample, if the current frame chain reaches this marker, the cached frames from that point onward are reused instead of being re-read from remote memory.

The sampling profiler now enables frame caching by default.
24 files changed:
Include/cpython/pystate.h
Include/internal/pycore_debug_offsets.h
Include/internal/pycore_global_objects_fini_generated.h
Include/internal/pycore_global_strings.h
Include/internal/pycore_runtime_init_generated.h
Include/internal/pycore_unicodeobject_generated.h
InternalDocs/frames.md
Lib/profiling/sampling/sample.py
Lib/test/test_external_inspection.py
Misc/NEWS.d/next/Library/2025-12-01-14-43-58.gh-issue-138122.nRm3ic.rst [new file with mode: 0644]
Modules/Setup.stdlib.in
Modules/_remote_debugging/_remote_debugging.h
Modules/_remote_debugging/clinic/module.c.h
Modules/_remote_debugging/code_objects.c
Modules/_remote_debugging/frame_cache.c [new file with mode: 0644]
Modules/_remote_debugging/frames.c
Modules/_remote_debugging/module.c
Modules/_remote_debugging/threads.c
PCbuild/_remote_debugging.vcxproj
PCbuild/_remote_debugging.vcxproj.filters
Python/ceval.c
Python/remote_debug.h
Python/remote_debugging.c
Tools/inspection/benchmark_external_inspection.py