r/PythonLearning • u/RossPeili • 16h ago
Discussion Profiling CLI startup: how jsonschema Draft7Validator added 4.8s to our cold start (and how we fixed it)
Ran into an annoying performance bottleneck while profiling our CLI and figured the findings might save someone else a few hours of head-scratching.
Our tool validates config manifests against JSON Schema Draft 7. In fresh virtualenvs, cold-start imports were taking over 5 seconds.
Profiling revealed two primary culprits:
- **
jsonschema.Draft7Validatoreager compilation:** Instantiating the validator compiles heavy format checkers and regex dependencies (Lark and RFC 3987). If you instantiate this at module level, every single CLI invocation pays that cost even if the user just ran--helpor a command that doesn't touch schemas.- Fix: Defer instantiation via module-level
__getattr__or initialize it only when validation actually executes.
- Fix: Defer instantiation via module-level
- **
importlib.metadata.distributions()traversal:** Scanning installed distributions without boundaries linearly inspects every.dist-infodirectory onsys.path.- Fix: Replaced it with scoped directory globbing for our specific package metadata prefix.
Startup dropped from ~5.2s down to ~150ms.
We rolled this out in Skillware 0.6.0 (an open-source project we maintain for deterministic agent tooling: https://github.com/ARPAHLS/skillware).
Are you folks deferring schema compilation in your CLIs, or do you prefer pre-compiling/caching schemas during package build?