Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 100 additions & 3 deletions mypyc/lib-rt/exc_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down
58 changes: 58 additions & 0 deletions mypyc/test-data/run-exceptions.test
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Use lower-case list instead List in tests.


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()
Loading