-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
Improve ctypes startup time #146547
Copy link
Copy link
Open
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-ctypestype-refactorCode refactoring (with no changes in behavior)Code refactoring (with no changes in behavior)
Description
Feature or enhancement
Proposal:
In the ctypes source, sysconfig is loaded as early as line 5.
"""create and manipulate C data types in Python"""
import os as _os
import sys as _sys
import sysconfig as _sysconfig
import types as _typesHowever, scanning through the file, it is only used once, and in a conditional branch too:
if _os.name == "nt":
pythonapi = PyDLL("python dll", None, _sys.dllhandle)
elif _sys.platform in ["android", "cygwin"]:
# These are Unix-like platforms which use a dynamically-linked libpython.
pythonapi = PyDLL(_sysconfig.get_config_var("LDLIBRARY"))
else:
pythonapi = PyDLL(None)The reason this is inefficient is that sysconfig imports threading instead of _thread, which cannot be helped since it has to use a top-level RLock. Correct me if I'm wrong, but this means at least 50 ms of import time.
I propose to move the import into this if branch. This may require a backport to 3.13 and 3.14, which have similar inefficiencies. (The import was not present prior.)
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-ctypestype-refactorCode refactoring (with no changes in behavior)Code refactoring (with no changes in behavior)