Commit ad18e020 authored by Stefan Behnel's avatar Stefan Behnel

implemented a module global cache for the code objects used in traceback building

parent 6f7bc67e
...@@ -1895,6 +1895,10 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1895,6 +1895,10 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code.put_decref_clear(Naming.empty_tuple, code.put_decref_clear(Naming.empty_tuple,
PyrexTypes.py_object_type, PyrexTypes.py_object_type,
nanny=False) nanny=False)
code.putln("/*--- Code object cache cleanup code ---*/")
code.globalstate.use_utility_code(
UtilityCode.load_cached("CodeObjectCache", "ModuleSetupCode.c"))
code.putln('%s();' % Naming.global_code_object_cache_clear)
# for entry in env.pynum_entries: # for entry in env.pynum_entries:
# code.put_decref_clear(entry.cname, # code.put_decref_clear(entry.cname,
# PyrexTypes.py_object_type, # PyrexTypes.py_object_type,
......
...@@ -99,6 +99,10 @@ binding_cfunc = pyrex_prefix + "binding_PyCFunctionType" ...@@ -99,6 +99,10 @@ binding_cfunc = pyrex_prefix + "binding_PyCFunctionType"
fused_func_prefix = pyrex_prefix + 'fuse_' fused_func_prefix = pyrex_prefix + 'fuse_'
quick_temp_cname = pyrex_prefix + "temp" # temp variable for quick'n'dirty temping quick_temp_cname = pyrex_prefix + "temp" # temp variable for quick'n'dirty temping
global_code_object_cache_find = pyrex_prefix + 'find_code_object'
global_code_object_cache_insert = pyrex_prefix + 'insert_code_object'
global_code_object_cache_clear = pyrex_prefix + 'clear_code_object_cache'
genexpr_id_ref = 'genexpr' genexpr_id_ref = 'genexpr'
line_c_macro = "__LINE__" line_c_macro = "__LINE__"
......
...@@ -8634,6 +8634,8 @@ static CYTHON_INLINE void __Pyx_ExceptionSwap(PyObject **type, PyObject **value, ...@@ -8634,6 +8634,8 @@ static CYTHON_INLINE void __Pyx_ExceptionSwap(PyObject **type, PyObject **value,
#------------------------------------------------------------------------------------ #------------------------------------------------------------------------------------
code_object_cache_utility_code = UtilityCode.load_cached("CodeObjectCache", "ModuleSetupCode.c")
traceback_utility_code = UtilityCode( traceback_utility_code = UtilityCode(
proto = """ proto = """
static void __Pyx_AddTraceback(const char *funcname, int %(CLINENO)s, static void __Pyx_AddTraceback(const char *funcname, int %(CLINENO)s,
...@@ -8709,9 +8711,13 @@ static void __Pyx_AddTraceback(const char *funcname, int %(CLINENO)s, ...@@ -8709,9 +8711,13 @@ static void __Pyx_AddTraceback(const char *funcname, int %(CLINENO)s,
PyObject *py_globals = 0; PyObject *py_globals = 0;
PyFrameObject *py_frame = 0; PyFrameObject *py_frame = 0;
py_code = %(FINDCODEOBJECT)s(%(CLINENO)s);
if (!py_code) {
py_code = __Pyx_CreateCodeObjectForTraceback( py_code = __Pyx_CreateCodeObjectForTraceback(
funcname, %(CLINENO)s, %(LINENO)s, %(FILENAME)s); funcname, %(CLINENO)s, %(LINENO)s, %(FILENAME)s);
if (!py_code) goto bad; if (!py_code) goto bad;
%(INSERTCODEOBJECT)s(%(CLINENO)s, py_code);
}
py_globals = PyModule_GetDict(%(GLOBALS)s); py_globals = PyModule_GetDict(%(GLOBALS)s);
if (!py_globals) goto bad; if (!py_globals) goto bad;
py_frame = PyFrame_New( py_frame = PyFrame_New(
...@@ -8733,9 +8739,12 @@ bad: ...@@ -8733,9 +8739,12 @@ bad:
'CFILENAME': Naming.cfilenm_cname, 'CFILENAME': Naming.cfilenm_cname,
'CLINENO': Naming.clineno_cname, 'CLINENO': Naming.clineno_cname,
'GLOBALS': Naming.module_cname, 'GLOBALS': Naming.module_cname,
'FINDCODEOBJECT' : Naming.global_code_object_cache_find,
'INSERTCODEOBJECT' : Naming.global_code_object_cache_insert,
'EMPTY_TUPLE' : Naming.empty_tuple, 'EMPTY_TUPLE' : Naming.empty_tuple,
'EMPTY_BYTES' : Naming.empty_bytes, 'EMPTY_BYTES' : Naming.empty_bytes,
}) },
requires=[code_object_cache_utility_code])
#------------------------------------------------------------------------------------ #------------------------------------------------------------------------------------
......
...@@ -219,6 +219,125 @@ ...@@ -219,6 +219,125 @@
#define __Pyx_DOCSTR(n) (n) #define __Pyx_DOCSTR(n) (n)
#endif #endif
/////////////// CodeObjectCache.proto ///////////////
typedef struct {
int code_line;
PyCodeObject* code_object;
} __Pyx_CodeObjectCacheEntry;
struct __Pyx_CodeObjectCache {
int count;
int max_count;
__Pyx_CodeObjectCacheEntry* entries;
};
static struct __Pyx_CodeObjectCache __pyx_code_cache = {0,0,NULL};
static void __pyx_clear_code_object_cache(void);
static PyCodeObject *__pyx_find_code_object(int code_line);
static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object);
/////////////// CodeObjectCache ///////////////
// Note that errors are simply ignored in the code below.
// This is just a cache, if a lookup or insertion fails - so what?
static void __pyx_clear_code_object_cache(void) {
__Pyx_CodeObjectCacheEntry* entries = __pyx_code_cache.entries;
int count = __pyx_code_cache.count;
int i;
if (entries == NULL) {
return;
}
__pyx_code_cache.count = 0;
__pyx_code_cache.max_count = 0;
__pyx_code_cache.entries = NULL;
for (i=0; i<count; i++) {
Py_DECREF(entries[i].code_object);
}
PyMem_Free(entries);
}
static int __pyx_bisect_code_objects(int code_line) {
int start, end, mid;
__Pyx_CodeObjectCacheEntry* entries = __pyx_code_cache.entries;
start = 0;
end = __pyx_code_cache.count - 1;
while (start < end) {
mid = (start + end) / 2;
if (code_line < entries[mid].code_line) {
end = mid - 1;
} else if (code_line > entries[mid].code_line) {
start = mid + 1;
} else {
return mid;
}
}
if (start == end || code_line <= entries[start].code_line) {
return start;
} else {
return end;
}
}
static PyCodeObject *__pyx_find_code_object(int code_line) {
PyCodeObject* code_object;
int pos;
if (unlikely(!code_line) || unlikely(!__pyx_code_cache.entries)) {
return NULL;
}
pos = __pyx_bisect_code_objects(code_line);
if (unlikely(pos >= __pyx_code_cache.count) || unlikely(__pyx_code_cache.entries[pos].code_line != code_line)) {
return NULL;
}
code_object = __pyx_code_cache.entries[pos].code_object;
Py_INCREF(code_object);
return code_object;
}
static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object) {
int pos, i;
__Pyx_CodeObjectCacheEntry* entries = __pyx_code_cache.entries;
if (unlikely(!code_line)) {
return;
}
if (unlikely(!entries)) {
entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Malloc(64*sizeof(__Pyx_CodeObjectCacheEntry));
if (likely(entries)) {
__pyx_code_cache.entries = entries;
__pyx_code_cache.max_count = 64;
__pyx_code_cache.count = 1;
entries[0].code_line = code_line;
entries[0].code_object = code_object;
Py_INCREF(code_object);
}
return;
}
pos = __pyx_bisect_code_objects(code_line);
if ((pos < __pyx_code_cache.count) && unlikely(__pyx_code_cache.entries[pos].code_line == code_line)) {
PyCodeObject* tmp = entries[pos].code_object;
entries[pos].code_object = code_object;
Py_DECREF(tmp);
return;
}
if (__pyx_code_cache.count == __pyx_code_cache.max_count) {
int new_max = __pyx_code_cache.max_count + 64;
entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Realloc(
__pyx_code_cache.entries, new_max*sizeof(__Pyx_CodeObjectCacheEntry));
if (unlikely(!entries)) {
return;
}
__pyx_code_cache.entries = entries;
__pyx_code_cache.max_count = new_max;
}
for (i=__pyx_code_cache.count; i>pos; i--) {
entries[i] = entries[i-1];
}
entries[pos].code_line = code_line;
entries[pos].code_object = code_object;
__pyx_code_cache.count++;
Py_INCREF(code_object);
}
/////////////// CheckBinaryVersion.proto /////////////// /////////////// CheckBinaryVersion.proto ///////////////
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment