Skip to content

Commit a57989c

Browse files
author
jeffrey.yasskin
committed
Speed up Python (according to pybench and 2to3-on-itself) by 1-2% by caching
whether any thread has tracing turned on, which saves one load instruction in the fast_next_opcode path in PyEval_EvalFrameEx(). See issue 4477. git-svn-id: http://svn.python.org/projects/python/trunk@67494 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 2c212ee commit a57989c

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

Python/ceval.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,13 @@ enum why_code {
504504
static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
505505
static int unpack_iterable(PyObject *, int, PyObject **);
506506

507+
/* Records whether tracing is on for any thread. Counts the number of
508+
threads for which tstate->c_tracefunc is non-NULL, so if the value
509+
is 0, we know we don't have to check this thread's c_tracefunc.
510+
This speeds up the if statement in PyEval_EvalFrameEx() after
511+
fast_next_opcode*/
512+
static int _Py_TracingPossible = 0;
513+
507514
/* for manipulating the thread switch and periodic "stuff" - used to be
508515
per thread, now just a pair o' globals */
509516
int _Py_CheckInterval = 100;
@@ -886,7 +893,8 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
886893

887894
/* line-by-line tracing support */
888895

889-
if (tstate->c_tracefunc != NULL && !tstate->tracing) {
896+
if (_Py_TracingPossible &&
897+
tstate->c_tracefunc != NULL && !tstate->tracing) {
890898
/* see maybe_call_line_trace
891899
for expository comments */
892900
f->f_stacktop = stack_pointer;
@@ -3414,6 +3422,7 @@ PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
34143422
{
34153423
PyThreadState *tstate = PyThreadState_GET();
34163424
PyObject *temp = tstate->c_traceobj;
3425+
_Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
34173426
Py_XINCREF(arg);
34183427
tstate->c_tracefunc = NULL;
34193428
tstate->c_traceobj = NULL;

0 commit comments

Comments
 (0)