gh-107674: Lazy load line number to improve performance of tracing (GH-118127)

This commit is contained in:
Tian Gao 2024-04-29 01:54:52 -07:00 committed by GitHub
parent c7e7bfc4ca
commit 375c94c75d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 61 additions and 19 deletions

View file

@ -41,12 +41,20 @@ int
PyFrame_GetLineNumber(PyFrameObject *f)
{
assert(f != NULL);
if (f->f_lineno != 0) {
if (f->f_lineno == -1) {
// We should calculate it once. If we can't get the line number,
// set f->f_lineno to 0.
f->f_lineno = PyUnstable_InterpreterFrame_GetLine(f->f_frame);
if (f->f_lineno < 0) {
f->f_lineno = 0;
return -1;
}
}
if (f->f_lineno > 0) {
return f->f_lineno;
}
else {
return PyUnstable_InterpreterFrame_GetLine(f->f_frame);
}
return PyUnstable_InterpreterFrame_GetLine(f->f_frame);
}
static PyObject *