mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
Significant speedup -- when a submodule imports a global module, add a
dummy entry to sys.modules, marking the absence of a submodule by the same name. Thus, if module foo.bar executes the statement "import time", sys.modules['foo.time'] will be set to None, once the absence of a module foo.time is confirmed (by looking for it in foo's path). The next time when foo.bar (or any other submodule of foo) executes "import time", no I/O is necessary to determine that there is no module foo.time. (Justification: It may seem strange to pollute sys.modules. However, since we're doing the lookup anyway it's definitely the fastest solution. This is the same convention that 'ni' uses and I haven't heard any complaints.)
This commit is contained in:
parent
81da02e6aa
commit
f5f5fdbdd0
1 changed files with 20 additions and 5 deletions
|
@ -998,6 +998,7 @@ static PyObject *get_parent Py_PROTO((PyObject *globals,
|
||||||
char *buf, int *p_buflen));
|
char *buf, int *p_buflen));
|
||||||
static PyObject *load_next Py_PROTO((PyObject *mod, PyObject *altmod,
|
static PyObject *load_next Py_PROTO((PyObject *mod, PyObject *altmod,
|
||||||
char **p_name, char *buf, int *p_buflen));
|
char **p_name, char *buf, int *p_buflen));
|
||||||
|
static int mark_miss Py_PROTO((char *name));
|
||||||
static int ensure_fromlist Py_PROTO((PyObject *mod, PyObject *fromlist,
|
static int ensure_fromlist Py_PROTO((PyObject *mod, PyObject *fromlist,
|
||||||
char *buf, int buflen));
|
char *buf, int buflen));
|
||||||
static PyObject * import_submodule Py_PROTO((PyObject *mod,
|
static PyObject * import_submodule Py_PROTO((PyObject *mod,
|
||||||
|
@ -1168,11 +1169,17 @@ load_next(mod, altmod, p_name, buf, p_buflen)
|
||||||
result = import_submodule(mod, p, buf);
|
result = import_submodule(mod, p, buf);
|
||||||
if (result == Py_None && altmod != mod) {
|
if (result == Py_None && altmod != mod) {
|
||||||
Py_DECREF(result);
|
Py_DECREF(result);
|
||||||
/* Here, altmod must be None */
|
/* Here, altmod must be None and mod must not be None */
|
||||||
strncpy(buf, name, len);
|
result = import_submodule(altmod, name, name);
|
||||||
buf[len] = '\0';
|
if (result != NULL && result != Py_None) {
|
||||||
*p_buflen = len;
|
if (mark_miss(buf) != 0) {
|
||||||
result = import_submodule(altmod, buf, buf);
|
Py_DECREF(result);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
strncpy(buf, name, len);
|
||||||
|
buf[len] = '\0';
|
||||||
|
*p_buflen = len;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (result == NULL)
|
if (result == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -1187,6 +1194,14 @@ load_next(mod, altmod, p_name, buf, p_buflen)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
mark_miss(name)
|
||||||
|
char *name;
|
||||||
|
{
|
||||||
|
PyObject *modules = PyImport_GetModuleDict();
|
||||||
|
return PyDict_SetItemString(modules, name, Py_None);
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
ensure_fromlist(mod, fromlist, buf, buflen)
|
ensure_fromlist(mod, fromlist, buf, buflen)
|
||||||
PyObject *mod;
|
PyObject *mod;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue