From 9821ac28595ad321bfdf5d666984c1b9f16bb6d6 Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Sat, 14 Nov 2026 17:00:42 +0500 Subject: [PATCH] [mypyc] Cache code objects used for traceback entries `CPy_AddTraceback` created a new code object for every traceback entry added in native code. On free-threaded builds code objects use deferred reference counting, so they are only freed by the cyclic GC, and the GC only runs when the eval breaker is handled. A native loop that raises and catches exceptions without calling interpreted code never gets there, so each iteration leaked a dead code object until the native function returned. For example, a loop doing `try: raise TypeError("x") except TypeError: ...` grew RSS by ~500 bytes per iteration (~1.4 GB for 3M iterations in a single call). Cache the code objects in a small direct-mapped table keyed by filename, function name and line, protected by a mutex on free-threaded builds. This fixes the memory growth, and it also makes raising exceptions in native code faster on all builds, since creating a code object is relatively expensive (the loop above got ~3.6x faster on 3.14t and ~2.2x faster on 3.14). --- mypyc/lib-rt/exc_ops.c | 103 +++++++++++++++++++++++++++- mypyc/test-data/run-exceptions.test | 58 ++++++++++++++++ 2 files changed, 158 insertions(+), 3 deletions(-) diff --git a/mypyc/lib-rt/exc_ops.c b/mypyc/lib-rt/exc_ops.c index c5ec2b6386e8..58ba4015e9b6 100644 --- a/mypyc/lib-rt/exc_ops.c +++ b/mypyc/lib-rt/exc_ops.c @@ -288,9 +288,106 @@ void CPy_TypeError(const char *expected, PyObject *value) { #include "internal/pycore_frame.h" #endif +// Cache of the code objects used for traceback entries. +// +// Creating a new code object for each traceback entry is slow. It's also a +// problem for memory use on free-threaded builds, since code objects use +// deferred reference counting there and are only freed by the cyclic GC. The +// GC only runs when the eval breaker is handled, which doesn't happen in a +// native loop that doesn't call interpreted code, so a loop that repeatedly +// raises and catches an exception would accumulate dead code objects until +// it returns. +// +// The cache is direct-mapped. The slot is chosen based on the string pointers +// and the line number, but the string contents are compared on lookup, since +// the strings aren't required to have static storage duration. +#define CPY_TB_CODE_CACHE_SIZE 256 + +typedef struct { + char *filename; // Owned copy + char *funcname; // Owned copy + int line; + PyCodeObject *code; +} CPyTracebackCodeCacheEntry; + +static CPyTracebackCodeCacheEntry tb_code_cache[CPY_TB_CODE_CACHE_SIZE]; + +#ifdef Py_GIL_DISABLED +static PyMutex tb_code_cache_mutex; +#define TB_CODE_CACHE_LOCK() PyMutex_Lock(&tb_code_cache_mutex) +#define TB_CODE_CACHE_UNLOCK() PyMutex_Unlock(&tb_code_cache_mutex) +#else +#define TB_CODE_CACHE_LOCK() +#define TB_CODE_CACHE_UNLOCK() +#endif + +static inline size_t tb_code_cache_index(const char *filename, const char *funcname, int line) { + size_t h = (size_t)(uintptr_t)funcname + (size_t)(uintptr_t)filename * 3 + + (size_t)line * 0x9e3779b1u; + h ^= h >> 16; + h ^= h >> 8; + return h & (CPY_TB_CODE_CACHE_SIZE - 1); +} + +static char *copy_c_string(const char *s) { + size_t size = strlen(s) + 1; + char *result = PyMem_RawMalloc(size); + if (result != NULL) { + memcpy(result, s, size); + } + return result; +} + +// Return a new reference to a code object for a traceback entry, or NULL with +// an error set. +static PyCodeObject *get_traceback_code(const char *filename, const char *funcname, int line) { + CPyTracebackCodeCacheEntry *entry = + &tb_code_cache[tb_code_cache_index(filename, funcname, line)]; + PyCodeObject *code = NULL; + + TB_CODE_CACHE_LOCK(); + if (entry->code != NULL && entry->line == line + && strcmp(entry->funcname, funcname) == 0 + && strcmp(entry->filename, filename) == 0) { + code = (PyCodeObject *)Py_NewRef(entry->code); + } + TB_CODE_CACHE_UNLOCK(); + if (code != NULL) { + return code; + } + + code = PyCode_NewEmpty(filename, funcname, line); + if (code == NULL) { + return NULL; + } + char *filename_copy = copy_c_string(filename); + char *funcname_copy = copy_c_string(funcname); + if (filename_copy == NULL || funcname_copy == NULL) { + // Not being able to cache the code object is fine. + PyMem_RawFree(filename_copy); + PyMem_RawFree(funcname_copy); + return code; + } + + TB_CODE_CACHE_LOCK(); + CPyTracebackCodeCacheEntry old = *entry; + entry->filename = filename_copy; + entry->funcname = funcname_copy; + entry->line = line; + entry->code = (PyCodeObject *)Py_NewRef(code); + TB_CODE_CACHE_UNLOCK(); + + // Release the evicted entry only after the cache is consistent again, since + // the decref could run arbitrary code. + PyMem_RawFree(old.filename); + PyMem_RawFree(old.funcname); + Py_XDECREF(old.code); + return code; +} + // This function is basically exactly the same with _PyTraceback_Add -// which is available in all the versions we support. -// We're continuing to use this because we'll probably optimize this later. +// which is available in all the versions we support, except that +// code objects are cached (see above). void CPy_AddTraceback(const char *filename, const char *funcname, int line, PyObject *globals) { PyObject *exc, *val, *tb; PyThreadState *thread_state = PyThreadState_GET(); @@ -303,7 +400,7 @@ void CPy_AddTraceback(const char *filename, const char *funcname, int line, PyOb // FS encoding, which could have a decoder in Python. We don't do // that so *that* doesn't apply to us.) PyErr_Fetch(&exc, &val, &tb); - PyCodeObject *code_obj = PyCode_NewEmpty(filename, funcname, line); + PyCodeObject *code_obj = get_traceback_code(filename, funcname, line); if (code_obj == NULL) { goto error; } diff --git a/mypyc/test-data/run-exceptions.test b/mypyc/test-data/run-exceptions.test index 93fa01f3bb43..aaf853ecd778 100644 --- a/mypyc/test-data/run-exceptions.test +++ b/mypyc/test-data/run-exceptions.test @@ -741,3 +741,61 @@ except RuntimeError as e: assert str(e) == "No active exception to reraise" else: assert False + +[case testTracebackCodeObjectReuse] +import gc +from typing import Any, List + +def fail(n: int) -> None: + raise ValueError(str(n)) + +def catch(n: int) -> List[Any]: + tbs = [] + for i in range(n): + try: + fail(i) + except ValueError as e: + exc: Any = e + tbs.append(exc.__traceback__) + return tbs + +def raise_and_catch(n: int) -> int: + count = 0 + for i in range(n): + try: + raise ValueError("x") + except ValueError: + count += 1 + return count + +def entries(tb: Any) -> List[Any]: + result = [] + while tb is not None: + code = tb.tb_frame.f_code + result.append((code.co_filename.endswith("native.py"), code.co_name, tb.tb_lineno)) + tb = tb.tb_next + return result + +def test_traceback_entries() -> None: + tbs = catch(2) + assert entries(tbs[0]) == [(True, "catch", 11), (True, "fail", 5)] + assert entries(tbs[1]) == entries(tbs[0]) + # Code objects of traceback entries are cached + assert tbs[0].tb_frame.f_code is tbs[1].tb_frame.f_code + assert tbs[0].tb_next.tb_frame.f_code is tbs[1].tb_next.tb_frame.f_code + +def count_code_objects() -> int: + return sum(1 for o in gc.get_objects() if type(o).__name__ == "code") + +def test_no_garbage_code_objects() -> None: + # On free-threaded builds code objects are only freed by the cyclic GC, + # so creating a new code object for each traceback entry would create + # garbage that can't be freed while a native loop runs. + gc.collect() + gc.disable() + try: + before = count_code_objects() + assert raise_and_catch(1000) == 1000 + assert count_code_objects() - before < 10 + finally: + gc.enable()