Skip to content

Commit

Permalink
Reduce costs of checking if lazy imports are enabled
Browse files Browse the repository at this point in the history
Summary: Checking if lazy imports is showing up as a significant (2%) cost in some workloads.  Those workloads are probably already less than ideal in that they're presumably doing imports in functions which is a bad idea.  But we can make that better by first checking to see if we're at a global scope which is super cheap.

Reviewed By: itamaro, Kronuz, cxxxs

Differential Revision: D49522414

fbshipit-source-id: 4ca46456309b36f4f75d03f493eaaf44068548e6
  • Loading branch information
DinoV authored and facebook-github-bot committed Sep 22, 2023
1 parent 1042786 commit 2acbb48
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -3711,9 +3711,9 @@ f->lazy_imports_cache_seq = -1;
PyObject *level = TOP();
PyObject *res;

if (_PyImport_IsLazyImportsEnabled(tstate)
&& f->f_globals == f->f_locals
&& f->f_iblock == 0) {
if (f->f_globals == f->f_locals &&
f->f_iblock == 0 &&
_PyImport_IsLazyImportsEnabled(tstate)) {
res = _PyImport_LazyImportName(f->f_builtins,
f->f_globals,
f->f_locals == NULL ? Py_None : f->f_locals,
Expand Down

0 comments on commit 2acbb48

Please sign in to comment.