Skip to content

Skip interpolation work for literal dotenv values - #764

Open
xhl123XHL352 wants to merge 1 commit into
theskumar:mainfrom
xhl123XHL352:xhl/skip-interpolation-for-literal-values
Open

xhl123XHL352 wants to merge 1 commit into
theskumar:mainfrom
xhl123XHL352:xhl/skip-interpolation-for-literal-values

Conversation

@xhl123XHL352

Copy link
Copy Markdown

Change

resolve_variables() currently tokenizes every value and builds an environment mapping for it. The parser expands braced references, so values that have no ${ can be used directly.

End-to-end benchmark

Called dotenv_values(path) on synthetic .env files on Linux with CPython 3.12.16. Each entry below is the median of nine runs per revision, alternating their order. All parsed mappings were identical.

Workload main With this change
200 literal settings 18.38 ms 3.95 ms
1,000 literal settings 110.61 ms 19.35 ms
5,000 literal settings 873.99 ms 96.95 ms
5,000 settings with 20% references 867.45 ms 256.42 ms

With the lookup approach from #706 applied locally, the 5,000-literal workload went from 432.55 ms to 96.68 ms after adding this fast path.

Reproduce the 5,000-literal workload
from pathlib import Path
from statistics import median
from tempfile import TemporaryDirectory
from timeit import repeat

from dotenv import dotenv_values

with TemporaryDirectory() as directory:
    path = Path(directory) / '.env'
    path.write_text(''.join(
        f'DOTENV_BENCH_{i}=/srv/jobs/input-{i}\n' for i in range(5000)
    ))
    samples = repeat(lambda: dotenv_values(path), number=1, repeat=9)
    print(f'{median(samples) * 1000:.2f} ms')

Verification

  • Compared both revisions for override precedence, empty and valueless entries, bare dollar references, incomplete expressions, defaults, and multiline values.
  • Full pytest runs on both revisions produced 281 passes, 5 skips, and the same six file-discovery failures caused by an existing .env above the server checkouts.
  • The changed file passes lint and formatting with the pre-commit Ruff version, v0.12.0.

AI assistance: OpenAI Codex implemented the change and ran the benchmarks and tests above.

Assisted-by: OpenAI Codex <noreply@openai.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant