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

Closed
sweeneyde opened this issue May 23, 2022 · 1 comment · Fixed by #93498
Closed

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

sweeneyde opened this issue May 23, 2022 · 1 comment · Fixed by #93498
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage type-feature A feature request or enhancement

Comments

@sweeneyde
Copy link
Member

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 A feature request or enhancement performance Performance or resource usage interpreter-core (Objects, Python, Grammar, and Parser dirs) labels May 23, 2022
@sweeneyde
Copy link
Member Author

TODO: documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant