You could simply cache a file path => TOML string dict if that is the approach you choose.
Case in point. After installing pytomlpp
in a venv, I can observe a startup time that is about twice as much as the time to start Python itself, which is in line with your argument.
$ hyperfine --warmup 10 "python -c ''"
Benchmark 1: python -c ''
Time (mean ± σ): 8.1 ms ± 0.9 ms [User: 7.8 ms, System: 0.8 ms]
Range (min … max): 7.0 ms … 11.4 ms 210 runs
(venv) ~/snakerun $ hyperfine --warmup 10 "python -c 'import pytomlpp'"
Benchmark 1: python -c 'import pytomlpp'
Time (mean ± σ): 23.8 ms ± 1.1 ms [User: 19.0 ms, System: 4.6 ms]
Range (min … max): 22.1 ms … 26.6 ms 116 runs
But once I change the start of pytomlpp/_io.py
from
import os
from typing import Any, BinaryIO, Dict, TextIO, Union
from . import _impl
FilePathOrObject = Union[str, TextIO, BinaryIO, os.PathLike]
to
from __future__ import annotations
import os
# from typing import Any, BinaryIO, Dict, TextIO, Union
from . import _impl
#FilePathOrObject = Union[str, TextIO, BinaryIO, os.PathLike]
I get
(venv) ~/snakerun $ hyperfine --warmup 10 "python -c 'import pytomlpp'"
Benchmark 1: python -c 'import pytomlpp'
Time (mean ± σ): 12.4 ms ± 0.9 ms [User: 9.6 ms, System: 2.7 ms]
Range (min … max): 11.2 ms … 16.5 ms 223 runs
So by shipping its type hints separately you would reduce the import time to ~half of the time to start up Python. And I did this without looking deeper into any of what pytomlpp
does.
Bottom line: you spent a little time and effort optimizing your code for startup time (e.g., you say you didn’t use regular expressions for that reason). You could also put a little time and effort into contributing import performance improvements to one of these libraries (for example the typing change in pytomlpp
, or refactoring tomllib
, which, from a cursory glance, uses regular expressions and compiles them on import).
(Sorry, I originally posted a draft of this by accident.)