perf(python-dotenv): replace dict merge with ChainMap in resolve_variables#628
Open
matheusvir wants to merge 11 commits intotheskumar:mainfrom
Open
perf(python-dotenv): replace dict merge with ChainMap in resolve_variables#628matheusvir wants to merge 11 commits intotheskumar:mainfrom
matheusvir wants to merge 11 commits intotheskumar:mainfrom
Conversation
Co-authored-by: Matheus Virgolino <matheus.virgolino.abilio.da.silva@ccc.ufcg.edu.br> Co-authored-by: Manoel Netto <manoel.da.nobrega.eustaqueo.netto@ccc.ufcg.edu.br> Co-authored-by: Pedro <pedroalmeida1896@gmail.com> Co-authored-by: Lucaslg7 <lucasmoizinholg7@gmail.com> Co-authored-by: RailtonDantas <railtondantas.code@gmail.com> Co-authored-by: João Pereira <joao.pereira.de.oliveira@ccc.ufcg.edu.br>
Co-authored-by: Matheus Virgolino <matheus.virgolino.abilio.da.silva@ccc.ufcg.edu.br> Co-authored-by: Manoel Netto <manoel.da.nobrega.eustaqueo.netto@ccc.ufcg.edu.br> Co-authored-by: Pedro <pedroalmeida1896@gmail.com> Co-authored-by: Lucaslg7 <lucasmoizinholg7@gmail.com> Co-authored-by: RailtonDantas <railtondantas.code@gmail.com> Co-authored-by: João Pereira <joao.pereira.de.oliveira@ccc.ufcg.edu.br>
8a3d287 to
9343989
Compare
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 was done
Changes were made to
resolve_variables()insrc/dotenv/main.py.Previously, python-dotenv built the resolved environment by repeatedly merging dictionaries, creating full copies of all key-value pairs at each interpolation pass.
This is replaced with a
ChainMap, which composes the same logical view without copying: lookups traverse the chain at read time, eliminating the merge step entirely.No new test files were added. The change is covered by the existing suite —
tests/test_main.pyexercisesresolve_variables()throughdotenv_values()with bothinterpolate=Trueandinterpolate=False, andtests/test_variables.pycoversparse_variables()directly. All existing tests pass with no regressions.Performance
All benchmarks were executed inside Docker containers to isolate the runtime environment and eliminate host-specific variance from CPU scheduling, OS caching, and library versions.
Methodology
dotenv_values()called withinterpolate=False.dotenv_values()called withinterpolate=True(default behavior).time.perf_counter_ns()with GC disabled during measurement.Rationale
The previous implementation of
resolve_variables()builds the resolved environment by repeatedly merging dictionaries, creating full copies of all key-value pairs at each interpolation pass. For large.envfiles this results in O(N) memory allocation and copying work that grows with the number of variables.ChainMapcomposes the same logical view without copying: lookups traverse the chain at read time, so the merge step is eliminated entirely.Results — without interpolation
Results — with interpolation
Analysis
The benefit is negligible at small sizes (below ~50 variables) but grows consistently from there. At 25,000 variables, the optimization reduces load time by ~76% without interpolation and ~72% with interpolation. The standard deviation also decreases in optimized runs, indicating more predictable latency.
The fact that both interpolation modes show the same trend confirms that the gain comes from eliminating the merge overhead in
resolve_variables(), not from any interpolation-specific code path.Reproducing the benchmark
The full benchmark infrastructure is available in the research repository at matheusvir/eda-oss-performance.
Relevant files:
setup/python-dotenv/Dockerfileexperiments/python-dotenv/chainmap_load_test/generate_load.pyexperiments/python-dotenv/chainmap_load_test/run_load_test_no_interpolation.pyexperiments/python-dotenv/chainmap_load_test/run_load_test_interpolation.pyexperiments/python-dotenv/chainmap_load_test/run.shTo run inside Docker:
# From the root of eda-oss-performance docker build -t dotenv-perf ./setup/python-dotenv/ docker run --rm -e EXPERIMENT=chainmap_load_test dotenv-perfResults are written to
results/python-dotenv/result_no_interpolation_dotenv_chainmap.jsonandresults/python-dotenv/result_interpolation_dotenv_chainmap.json.Feedback on the
ChainMapintegration, edge cases with nested interpolation, and compatibility with existing override semantics is welcome.Relates to #504.