Opened 3 weeks ago

Closed 3 weeks ago

#37484 closed Cleanup/optimization (wontfix)

Avoid importing ctypes at import time in django.core.files.locks on Windows

Reported by: Alexandre Zollinger Chohfi Owned by:
Component: Core (Other) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

On Windows, django/core/files/locks.py imports ctypes and ctypes.wintypes and defines the OVERLAPPED structure and the LockFileEx/UnlockFileEx prototypes at module level. The module is imported by django.core.cache.backends.filebased, which django.core.checks imports, so every import django.db.models on Windows imports ctypes and loads _ctypes.pyd, libffi and the OLE DLLs _ctypes links against, whether or not anything ever locks a file. On other platforms the module only imports fcntl.

Measured on Windows 11 with Python 3.14.4 and Django main (8e3dec6):

  • python -X importtime -c "import django.db.models": django.core.files.locks takes 11.2 ms cumulative on Windows (0.1 ms on Linux, same machine under WSL2).
  • Building the ctypes objects on the first lock()/unlock() call instead: import django.db.models is 6.0 ms faster (310.7 -> 300.9 ms, 95% CI 1.2-13.6 ms, 60 alternating runs timed inside the process).

The constants and the lock/unlock functions stay at module level, so the public behavior is unchanged. runtests.py files cache file_storage passes on Windows with the change.

I'd be happy to submit the patch.

AI disclosure: the patch was prepared with GitHub Copilot (Claude Opus 5.5 model), which was used to find the import, and write the patch.

Change History (2)

comment:1 by Alexandre Zollinger Chohfi, 3 weeks ago

A correction to the numbers above. On Windows, django.core.management.color imports colorama when it is installed, and colorama imports ctypes itself, on the same import django.db.models path. So with colorama installed, this change saves nothing.

Re-measured on current main (a1f2b45), Python 3.14.4, 150 alternating runs of import django.db.models in a fresh process:

  • without colorama: 389.7 -> 380.4 ms (-8.3 ms, 95% CI 4.2 to 12.8)
  • with colorama: 390.2 -> 389.9 ms (no difference)

colorama is pulled in by pytest and tqdm on Windows, so many development environments have it. Environments without it, like a lean server venv, still get the saving. Deferring the colorama import in color.py would cover both cases, but it changes when just_fix_windows_console() runs, so that seems like a separate discussion.

comment:2 by David Smith, 3 weeks ago

Resolution: → wontfix
Status: new → closed

The report is quite difficult to understand. It seems the core of the proposal is a ~8ms improvement to startup time on Windows.

That doesn't seem to me like a enough of an improvement to be worthwhile. If a realistic example can be shown where this is materially impacting folk then we can reconsider.

Note: See TracTickets for help on using tickets.
Back to Top