Skip to content

import arcadedb: do not import importlib.metadata or numpy at import time - #482

Merged
tae898 merged 1 commit into
mainfrom
perf/477-lazy-import
Nov 24, 2026
Merged

tae898 merged 1 commit into
mainfrom
perf/477-lazy-import

Conversation

@tae898

@tae898 tae898 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

What does this PR do?

import arcadedb no longer imports importlib.metadata or numpy itself.

  • importlib.metadata (about 30 ms): the check that only one of the two distributions is installed now looks for arcadedb-<version>.dist-info and arcadedb_jvm-<version>.dist-info (and .egg-info) on sys.path with os.listdir, which is what importlib.metadata.version finds for an ordinary install. A sys.path entry that is not a directory (a zip, an egg) is the case it does not read: it asks importlib.metadata then, as before. check(backend, lookup) and installed(..., lookup) keep their signatures and the injected lookup still works. arcadedb.__version__ is read on first access (module __getattr__), so arcadedb.__version__ and from arcadedb import __version__ give the same value as before.
  • numpy (about 60 ms when installed): six modules did import numpy as _np at the top. arcadedb/_lazy_numpy.py gives them the same _np name: None exactly when numpy is not installed (a find_spec lookup, no import), so every if _np is None fallback is unchanged (the native build's float32 tail without numpy from fix(#444): the native reply reader reads the float32 tail without NumPy #470 included, tested here with numpy not installed). _np.ndarray, _np.generic, _np.floating and _np.dtype are only used to ask whether a value the caller passed in is a numpy object: while numpy is not imported in the process no such value exists, so the answer is a class nothing is an instance of and nothing is imported. Any other attribute (_np.asarray, _np.fromiter, _np.float32, a call that builds an array) imports numpy on first use and is then a plain attribute. Two module-level numpy constants became lazy for this (_NS_DATETIME in _native/codec.py, _FLOAT_DTYPES in _native/schema_objects.py).

Differences between the two builds: the native build no longer imports numpy at all until a call needs it. The JVM build still imports it when numpy is installed, because JPype imports numpy itself (python -X importtime shows it under jpype), so only the importlib.metadata part helps there.

One edge differs: a numpy that is installed but fails to import (a broken install) used to give _np = None silently; now it raises its ImportError at the first call that needs it. Not tested, I have no broken install to try.

Performance changes

python -X importtime -c "import arcadedb", the cumulative time of the arcadedb line, fresh process each, interleaved before (main) and after (this branch), 5 repeats each, nice -n 19, Python 3.12, from an empty working directory, on the main wheels of 2026-11-24 with the Python files of each arm laid over them. Laptop, noisy, not a claim. In the "after" rows numpy is not imported by arcadedb on the native build, and importlib.metadata is not imported by arcadedb on either.

case before median (min, max) ms after median (min, max) ms
native, no numpy installed 51.2 (50.4, 96.5) 40.9 (39.5, 92.3)
native, numpy and pyarrow installed 131.3 (129.9, 136.5) 43.2 (40.7, 44.7)
JVM, no numpy installed 56.5 (53.9, 107.0) 48.7 (47.7, 99.2)
JVM, numpy and pyarrow installed 139.0 (134.9, 141.6) 126.1 (121.0, 138.4)

The 90 to 110 ms maxima in the no-extras rows are one cold first sample of each arm. I did not run a control of main against itself, so differences of a few milliseconds are inside the noise. While numpy is installed but not imported, each value test on the hot conversion paths (_np.ndarray and the like) goes through the stand-in's __getattr__, one sys.modules lookup, instead of a module attribute read; I did not time that and the change claims nothing about it.

Related issues

Closes #477

Docs

No page describes the import cost or __version__; nothing to update.

Milestone

27.1.1. It is not purely additive: it changes how six modules bind numpy, with the one edge above, so I would not put it in the 26.12.1 cut.

Release note draft

import arcadedb is faster: it no longer imports importlib.metadata, and the native build no longer imports numpy until a call needs it (about 130 ms to 43 ms with numpy and pyarrow installed, 51 ms to 41 ms without, on one laptop). arcadedb.__version__ is read on first access.

Tests

tests/test_lazy_import.py, run by both builds: import arcadedb imports neither importlib.metadata nor numpy itself (compared with what the interpreter and JPype had loaded before), __version__ is read on first use and equals the distribution version, the distribution scan agrees with importlib.metadata and handles a dist-info on sys.path, a zip entry and a name that is not a wheel's normalized name, the value tests import nothing and an array call imports numpy, and to_columns() still returns numpy arrays. Local run on the main wheels with this branch's Python files laid over them (laptop): 449 passed on the JVM build for this file and 13 others that touch the changed modules; 280 passed on the native build with numpy, plus the same new file with numpy not installed (29 passed with test_native_codec_loads.py and test_dist_conflict.py). The 4 native failures in test_native_columnar_insert.py are ModuleNotFoundError for pandas and pyarrow, which my throwaway environment did not install; CI installs them.

Checklist

  • A regression test fails on the old code and passes on the new
  • The test suite that the change can affect passes
  • uvx pre-commit run --files <changed files> passes

@tae898 tae898 added this to the 27.1.1 milestone Nov 24, 2026
@tae898 tae898 added the performance Speed or memory of the bindings, with a measurement label Nov 24, 2026
@tae898 tae898 self-assigned this Nov 24, 2026
@tae898 tae898 modified the milestones: 27.1.1, 26.12.1 Nov 24, 2026
…time

The audit measured 46 ms for `import arcadedb` without extras and 112 ms with numpy and pyarrow installed (native wheel). Two imports
caused most of it: importlib.metadata (imported by _dist_conflict for the check that only one of the two distributions is installed,
and by __init__ for __version__) and numpy (imported at the top of six modules).

Distribution check: look for the dist-info or egg-info directory of the other distribution on sys.path with os.listdir, which is what
importlib.metadata.version finds for an ordinary install; a sys.path entry that is not a directory (zip, egg) falls back to
importlib.metadata, and an injected lookup still works. __version__ is computed on first access (module __getattr__).

numpy: _lazy_numpy.lazy_numpy() returns None when numpy is not installed (find_spec, no import), so every `if _np is None` fallback
stays, and otherwise a stand-in with the same _np name. The four names only ever used in isinstance and issubclass on a caller's value
(ndarray, generic, floating, dtype) answer with a class nothing is an instance of while numpy is not in sys.modules (no such value can
exist), and are the real classes after; any other attribute imports numpy on first use and is cached. Two module-level numpy constants
(_NS_DATETIME, _FLOAT_DTYPES) became lazy.

The JVM build still imports numpy when it is installed, because JPype does; only the metadata part helps there. A numpy that is
installed but fails to import now raises at first use instead of giving _np = None. Measured interleaved, 5 repeats, nice -n 19
(laptop, noisy, not a claim): native with numpy and pyarrow 131 ms to 43 ms, native without extras 51 ms to 41 ms, JVM without extras
57 ms to 49 ms, JVM with numpy and pyarrow 139 ms to 126 ms.
@tae898
tae898 force-pushed the perf/477-lazy-import branch from 5eabd2d to da80759 Compare November 24, 2026 17:30
@tae898
tae898 merged commit 2d06e46 into main Nov 24, 2026
22 checks passed
@tae898
tae898 deleted the perf/477-lazy-import branch November 24, 2026 18:23
@tae898

tae898 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

I rebased this onto main (head da80759). The only conflict was in src/arcadedb/vector.py, where #481 added TYPE_CHECKING and the Database import next to the numpy import that this PR replaces: I kept both, so _np = lazy_numpy() stays and the hints stay. __init__.py merged on its own, but __version__ is now read through a module __getattr__, which a type checker does not see as a name, so I added a bare __version__: str annotation (no value, nothing is created at import) to keep the name visible to typed code. #481 has no .pyi stub, so none was added. tests/test_typed_package.py and tests/test_lazy_import.py are unchanged.

CI on the new head: 19 success, 3 skipped (the three example jobs that the ci:full label turns on), 0 failures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Speed or memory of the bindings, with a measurement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

import arcadedb takes 46 to 112 ms: importlib.metadata in the distribution check and an eager numpy import

1 participant