Repository navigation
import arcadedb: do not import importlib.metadata or numpy at import time - #482
Merged
Merged
Conversation
…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
force-pushed
the
perf/477-lazy-import
branch
from
November 24, 2026 17:30
5eabd2d to
da80759
Compare
Collaborator
Author
|
I rebased this onto CI on the new head: 19 success, 3 skipped (the three example jobs that the |
This was referenced Nov 25, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
import arcadedbno longer importsimportlib.metadataor numpy itself.importlib.metadata(about 30 ms): the check that only one of the two distributions is installed now looks forarcadedb-<version>.dist-infoandarcadedb_jvm-<version>.dist-info(and.egg-info) onsys.pathwithos.listdir, which is whatimportlib.metadata.versionfinds for an ordinary install. Asys.pathentry that is not a directory (a zip, an egg) is the case it does not read: it asksimportlib.metadatathen, as before.check(backend, lookup)andinstalled(..., lookup)keep their signatures and the injectedlookupstill works.arcadedb.__version__is read on first access (module__getattr__), soarcadedb.__version__andfrom arcadedb import __version__give the same value as before.import numpy as _npat the top.arcadedb/_lazy_numpy.pygives them the same_npname:Noneexactly when numpy is not installed (afind_speclookup, no import), so everyif _np is Nonefallback 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.floatingand_np.dtypeare 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_DATETIMEin_native/codec.py,_FLOAT_DTYPESin_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 importtimeshows it underjpype), so only theimportlib.metadatapart helps there.One edge differs: a numpy that is installed but fails to import (a broken install) used to give
_np = Nonesilently; now it raises itsImportErrorat 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 thearcadedbline, 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" rowsnumpyis not imported by arcadedb on the native build, andimportlib.metadatais not imported by arcadedb on either.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
mainagainst 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.ndarrayand the like) goes through the stand-in's__getattr__, onesys.moduleslookup, 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 arcadedbis faster: it no longer importsimportlib.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 arcadedbimports neitherimportlib.metadatanor 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 withimportlib.metadataand handles adist-infoonsys.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, andto_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 withtest_native_codec_loads.pyandtest_dist_conflict.py). The 4 native failures intest_native_columnar_insert.pyareModuleNotFoundErrorfor pandas and pyarrow, which my throwaway environment did not install; CI installs them.Checklist
uvx pre-commit run --files <changed files>passes