mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-11 20:30:28 +00:00
shave off stat syscalls by rearranging imports
the hardest import method format to speed up is the "from x import y", because I have no idea whether y is a module or not. If y is a module, "from x import y" behaves similarly to the format "import x.y; y = x.y", which means I can bypass some checks by doing the import and equality separately. If y is a function, "from x import y" is not equivalent to the above format, and the import will trigger a ModuleNotFoundError. Unfortunately I can't check for or propagate such errors during the default APE startup without adding a whole bunch of checks, so I'd prefer to avoid these kinds of imports unless absolutely necessary. in this PR I avoid three such function imports in io.py and ntpath.py. The fix is to import the module wholesale (which happens underneath anyway) and then just check for the attribute - "import x; y = x.y", essentially similar to how it would be if y was a module.
This commit is contained in:
parent
5007f51b22
commit
a4c925e7cd
3 changed files with 12 additions and 15 deletions
6
third_party/python/Lib/io.py
vendored
6
third_party/python/Lib/io.py
vendored
|
@ -92,8 +92,6 @@ for klass in (StringIO, TextIOWrapper):
|
||||||
del klass
|
del klass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from _io import _WindowsConsoleIO
|
RawIOBase.register(_io._WindowsConsoleIO)
|
||||||
except ImportError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
else:
|
|
||||||
RawIOBase.register(_WindowsConsoleIO)
|
|
||||||
|
|
15
third_party/python/Lib/ntpath.py
vendored
15
third_party/python/Lib/ntpath.py
vendored
|
@ -20,6 +20,7 @@ devnull = 'nul'
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import stat
|
import stat
|
||||||
|
import posix
|
||||||
import genericpath
|
import genericpath
|
||||||
from genericpath import *
|
from genericpath import *
|
||||||
|
|
||||||
|
@ -276,8 +277,8 @@ def lexists(path):
|
||||||
# common case: drive letter roots. The alternative which uses GetVolumePathName
|
# common case: drive letter roots. The alternative which uses GetVolumePathName
|
||||||
# fails if the drive letter is the result of a SUBST.
|
# fails if the drive letter is the result of a SUBST.
|
||||||
try:
|
try:
|
||||||
from posix import _getvolumepathname
|
_getvolumepathname = posix._getvolumepathname
|
||||||
except ImportError:
|
except AttributeError:
|
||||||
_getvolumepathname = None
|
_getvolumepathname = None
|
||||||
def ismount(path):
|
def ismount(path):
|
||||||
"""Test whether a path is a mount point (a drive root, the root of a
|
"""Test whether a path is a mount point (a drive root, the root of a
|
||||||
|
@ -534,9 +535,9 @@ def _abspath_fallback(path):
|
||||||
|
|
||||||
# Return an absolute path.
|
# Return an absolute path.
|
||||||
try:
|
try:
|
||||||
from posix import _getfullpathname
|
_getfullpathname = posix._getfullpathname
|
||||||
|
|
||||||
except ImportError: # not running on Windows - mock up something sensible
|
except AttributeError: # not running on Windows - mock up something sensible
|
||||||
abspath = _abspath_fallback
|
abspath = _abspath_fallback
|
||||||
|
|
||||||
else: # use native Windows method on Windows
|
else: # use native Windows method on Windows
|
||||||
|
@ -664,7 +665,7 @@ try:
|
||||||
# GetFinalPathNameByHandle is available starting with Windows 6.0.
|
# GetFinalPathNameByHandle is available starting with Windows 6.0.
|
||||||
# Windows XP and non-Windows OS'es will mock _getfinalpathname.
|
# Windows XP and non-Windows OS'es will mock _getfinalpathname.
|
||||||
if sys.getwindowsversion()[:2] >= (6, 0):
|
if sys.getwindowsversion()[:2] >= (6, 0):
|
||||||
from posix import _getfinalpathname
|
_getfinalpathname = posix._getfinalpathname
|
||||||
else:
|
else:
|
||||||
raise ImportError
|
raise ImportError
|
||||||
except (AttributeError, ImportError, OSError):
|
except (AttributeError, ImportError, OSError):
|
||||||
|
@ -681,7 +682,7 @@ try:
|
||||||
# attribute to tell whether or not the path is a directory.
|
# attribute to tell whether or not the path is a directory.
|
||||||
# This is overkill on Windows - just pass the path to GetFileAttributes
|
# This is overkill on Windows - just pass the path to GetFileAttributes
|
||||||
# and check the attribute from there.
|
# and check the attribute from there.
|
||||||
from posix import _isdir as isdir
|
isdir = posix._isdir
|
||||||
except ImportError:
|
except AttributeError:
|
||||||
# Use genericpath.isdir as imported above.
|
# Use genericpath.isdir as imported above.
|
||||||
pass
|
pass
|
||||||
|
|
6
third_party/python/Python/import.c
vendored
6
third_party/python/Python/import.c
vendored
|
@ -128,11 +128,8 @@ static initentry ZipEntries[] = {
|
||||||
{"encodings.utf_8", {.inside_zip = 1, .is_package = 0}},
|
{"encodings.utf_8", {.inside_zip = 1, .is_package = 0}},
|
||||||
{"genericpath", {.inside_zip = 1, .is_package = 0}},
|
{"genericpath", {.inside_zip = 1, .is_package = 0}},
|
||||||
{"io", {.inside_zip = 1, .is_package = 0}},
|
{"io", {.inside_zip = 1, .is_package = 0}},
|
||||||
{"io._WindowsConsoleIO", {.inside_zip = 0, .is_package = 0}},
|
|
||||||
{"ntpath", {.inside_zip = 1, .is_package = 0}},
|
{"ntpath", {.inside_zip = 1, .is_package = 0}},
|
||||||
{"os", {.inside_zip = 1, .is_package = 0}},
|
{"os", {.inside_zip = 1, .is_package = 0}},
|
||||||
{"posix._getfullpathname", {.inside_zip = 0, .is_package = 0}},
|
|
||||||
{"posix._isdir", {.inside_zip = 0, .is_package = 0}},
|
|
||||||
{"posixpath", {.inside_zip = 1, .is_package = 0}},
|
{"posixpath", {.inside_zip = 1, .is_package = 0}},
|
||||||
{"readline", {.inside_zip = 0, .is_package = 0}},
|
{"readline", {.inside_zip = 0, .is_package = 0}},
|
||||||
{"site", {.inside_zip = 1, .is_package = 0}},
|
{"site", {.inside_zip = 1, .is_package = 0}},
|
||||||
|
@ -2811,8 +2808,9 @@ static PyObject *CosmoImporter_find_spec(PyObject *cls, PyObject **args,
|
||||||
key.tab = NULL;
|
key.tab = NULL;
|
||||||
res = bsearch(&key, ZipCdir_Lookup.entries, ZipCdir_Lookup.n, sizeof(initentry), cmp_initentry);
|
res = bsearch(&key, ZipCdir_Lookup.entries, ZipCdir_Lookup.n, sizeof(initentry), cmp_initentry);
|
||||||
if (res) {
|
if (res) {
|
||||||
if (!res->inside_zip)
|
if (!res->inside_zip) {
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
|
}
|
||||||
inside_zip = res->inside_zip;
|
inside_zip = res->inside_zip;
|
||||||
is_package = res->is_package;
|
is_package = res->is_package;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue