Skip to content

Refactor PyImport_ImportModuleLevelObject(). #4680

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

Merged
merged 2 commits into from
Dec 3, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 57 additions & 50 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -1589,12 +1589,67 @@ resolve_name(PyObject *name, PyObject *globals, int level)
return NULL;
}

static PyObject *
import_find_and_load(PyObject *abs_name)
{
_Py_IDENTIFIER(_find_and_load);
PyObject *mod = NULL;
PyInterpreterState *interp = PyThreadState_GET()->interp;
int import_time = interp->core_config.import_time;
static int import_level;
static _PyTime_t accumulated;

_PyTime_t t1 = 0, accumulated_copy = accumulated;

/* XOptions is initialized after first some imports.
* So we can't have negative cache before completed initialization.
* Anyway, importlib._find_and_load is much slower than
* _PyDict_GetItemIdWithError().
*/
if (import_time) {
static int header = 1;
if (header) {
fputs("import time: self [us] | cumulative | imported package\n",
stderr);
header = 0;
}

import_level++;
t1 = _PyTime_GetPerfCounter();
accumulated = 0;
}

if (PyDTrace_IMPORT_FIND_LOAD_START_ENABLED())
PyDTrace_IMPORT_FIND_LOAD_START(PyUnicode_AsUTF8(abs_name));

mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
&PyId__find_and_load, abs_name,
interp->import_func, NULL);

if (PyDTrace_IMPORT_FIND_LOAD_DONE_ENABLED())
PyDTrace_IMPORT_FIND_LOAD_DONE(PyUnicode_AsUTF8(abs_name),
mod != NULL);

if (import_time) {
_PyTime_t cum = _PyTime_GetPerfCounter() - t1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see .. a cultured individual .. good job 🔥


import_level--;
fprintf(stderr, "import time: %9ld | %10ld | %*s%s\n",
(long)_PyTime_AsMicroseconds(cum - accumulated, _PyTime_ROUND_CEILING),
(long)_PyTime_AsMicroseconds(cum, _PyTime_ROUND_CEILING),
import_level*2, "", PyUnicode_AsUTF8(abs_name));

accumulated = accumulated_copy + cum;
}

return mod;
}

PyObject *
PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
PyObject *locals, PyObject *fromlist,
int level)
{
_Py_IDENTIFIER(_find_and_load);
_Py_IDENTIFIER(_handle_fromlist);
PyObject *abs_name = NULL;
PyObject *final_mod = NULL;
Expand Down Expand Up @@ -1674,55 +1729,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
}
}
else {
int import_time = interp->core_config.import_time;
static int import_level;
static _PyTime_t accumulated;

_PyTime_t t1 = 0, accumulated_copy = accumulated;

/* XOptions is initialized after first some imports.
* So we can't have negative cache before completed initialization.
* Anyway, importlib._find_and_load is much slower than
* _PyDict_GetItemIdWithError().
*/
if (import_time) {
static int header = 1;
if (header) {
fputs("import time: self [us] | cumulative | imported package\n",
stderr);
header = 0;
}

import_level++;
t1 = _PyTime_GetPerfCounter();
accumulated = 0;
}

Py_XDECREF(mod);

if (PyDTrace_IMPORT_FIND_LOAD_START_ENABLED())
PyDTrace_IMPORT_FIND_LOAD_START(PyUnicode_AsUTF8(abs_name));

mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
&PyId__find_and_load, abs_name,
interp->import_func, NULL);

if (PyDTrace_IMPORT_FIND_LOAD_DONE_ENABLED())
PyDTrace_IMPORT_FIND_LOAD_DONE(PyUnicode_AsUTF8(abs_name),
mod != NULL);

if (import_time) {
_PyTime_t cum = _PyTime_GetPerfCounter() - t1;

import_level--;
fprintf(stderr, "import time: %9ld | %10ld | %*s%s\n",
(long)_PyTime_AsMicroseconds(cum - accumulated, _PyTime_ROUND_CEILING),
(long)_PyTime_AsMicroseconds(cum, _PyTime_ROUND_CEILING),
import_level*2, "", PyUnicode_AsUTF8(abs_name));

accumulated = accumulated_copy + cum;
}

mod = import_find_and_load(abs_name);
if (mod == NULL) {
goto error;
}
Expand Down