Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dataflow analysis in the compiler to avoid runtime NULL checks #93143

Open
sweeneyde opened this issue May 23, 2022 · 1 comment
Open

Dataflow analysis in the compiler to avoid runtime NULL checks #93143

sweeneyde opened this issue May 23, 2022 · 1 comment
Labels
interpreter-core performance type-feature

Comments

@sweeneyde
Copy link
Member

@sweeneyde sweeneyde commented May 23, 2022

LOAD_FAST accounts for 14.6% of all bytecodes executed. Including superinstructions brings this up to 14.6+4.7+4.6+2.4+0.9 = 27.1%

cpython/Python/ceval.c

Lines 1864 to 1872 in 202ed25

TARGET(LOAD_FAST) {
PyObject *value = GETLOCAL(oparg);
if (value == NULL) {
goto unbound_local_error;
}
Py_INCREF(value);
PUSH(value);
DISPATCH();
}

We can turn this NULL-check into an assertion in many cases, where we can determine at compile time that the local variable is already initialized. Preliminary tests show that almost all LOAD_FAST instructions can be statically analyzed to be loading already-initialized variables.

The one twist is handling del frame.f_locals["x"] or frame.f_lineno = 17, where previously-safe loads could become unsafe. For now, we can just replace all the LOAD_FAST (no null check) with LOAD_FAST_CHECK in that particular code object.

See also faster-cpython/ideas#306

@sweeneyde sweeneyde added type-feature performance interpreter-core labels May 23, 2022
@sweeneyde
Copy link
Member Author

@sweeneyde sweeneyde commented Jun 1, 2022

TODO: documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core performance type-feature
Projects
None yet
Development

No branches or pull requests

1 participant