Skip to content

Delete the redundant conditional statements in traceback.c #124265

@rruuaanng

Description

@rruuaanng

Feature or enhancement

cpython/Python/traceback.c

Lines 974 to 985 in 7a2d77c

dump_frame(fd, frame);
frame = frame->previous;
if (frame == NULL) {
break;
}
if (frame->owner == FRAME_OWNED_BY_CSTACK) {
/* Trampoline frame */
frame = frame->previous;
}
if (frame == NULL) {
break;
}

In the above code, it seems that it's redundant.

if (frame == NULL) {

Removing this extra if statement may eliminate the time consumed by the conditional statement.
In

cpython/Python/traceback.c

Lines 975 to 976 in 7a2d77c

frame = frame->previous;
if (frame == NULL) {

if frame is empty, it may not be executed

cpython/Python/traceback.c

Lines 979 to 982 in 7a2d77c

if (frame->owner == FRAME_OWNED_BY_CSTACK) {
/* Trampoline frame */
frame = frame->previous;
}

So it will not be executed

cpython/Python/traceback.c

Lines 983 to 986 in 7a2d77c

if (frame == NULL) {
break;
}
/* Can't have more than one shim frame in a row */

Logically speaking, if the frame is not NULL, then frame->previous can't be NULL either. This means frame->previous->previous won't be NULL either. If the frame has a forward link, frame->next, then it will have a unique successor.

If there are errors in my proposal or I don't understand the code base, please point out my mistakes, thanks.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions