Skip to content

Fix DeploymentConfig::getAllEnvOverrides memoization when no MAGENTO_…#40589

Open
hryvinskyi wants to merge 1 commit intomagento:2.4-developfrom
hryvinskyi:patch-6
Open

Fix DeploymentConfig::getAllEnvOverrides memoization when no MAGENTO_…#40589
hryvinskyi wants to merge 1 commit intomagento:2.4-developfrom
hryvinskyi:patch-6

Conversation

@hryvinskyi
Copy link
Copy Markdown
Member

Description (*)

DeploymentConfig::getAllEnvOverrides() has a broken memoization pattern that causes significant performance degradation on every request.

The method uses empty($this->envOverrides) to determine whether the environment variable scan has already been performed. However, when no MAGENTO_DC_* environment variables exist (which is the common case for most Magento installations), $this->envOverrides remains as [], and empty([]) evaluates to true — causing the full getenv() loop to execute on every single call.

The fix changes the initialization of $envOverrides from [] to null and uses a strict === null check as the guard condition. This ensures the getenv() scan runs exactly once per request lifecycle, regardless of whether any MAGENTO_DC_* variables are found.

Manual testing scenarios (*)

  1. Deploy a Magento instance without any MAGENTO_DC_* environment variables set (default setup).
  2. Profile a frontend page request using SPX, Blackfire, or Xdebug.
  3. Search for getAllEnvOverrides in the profile — before the fix it will show a lot of calls
  4. Apply the patch and profile the same page again.
  5. Confirm getAllEnvOverrides now shows ~1-2 calls with negligible time.
  6. Set a MAGENTO_DC_* environment variable (e.g., MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST=127.0.0.1).
  7. Verify the env override is correctly applied to DeploymentConfig::get('db/connection/default/host').
  8. Verify resetData() still works correctly (e.g., after bin/magento app:config:import).

Questions or comments

This is a one-line logic fix with meaningful performance impact. The root cause is that empty() cannot distinguish between "never scanned" and "scanned but found nothing." Using null as the uninitialized sentinel resolves this cleanly.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

…DC_ env vars exist

`DeploymentConfig::getAllEnvOverrides()` has a broken memoization pattern. It uses `empty($this->envOverrides)` to check if the env scan has already been performed, but when no `MAGENTO_DC_*` environment variables exist (which is the common case), `$this->envOverrides` remains `[]`, and `empty([])` returns `true` causing the full `getenv()` scan to execute on every call.
@m2-assistant
Copy link
Copy Markdown

m2-assistant bot commented Mar 15, 2026

Hi @hryvinskyi. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@ct-prd-pr-scan
Copy link
Copy Markdown

The security team has been informed about this pull request due to the presence of risky security keywords. For security vulnerability reports, please visit Adobe's vulnerability disclosure program on HackerOne or email psirt@adobe.com.

@hryvinskyi
Copy link
Copy Markdown
Member Author

@magento run all tests

@engcom-Bravo engcom-Bravo added Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it Priority: P3 May be fixed according to the position in the backlog. labels Mar 17, 2026
@github-project-automation github-project-automation bot moved this to Pending Review in Pull Requests Dashboard Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Priority: P3 May be fixed according to the position in the backlog. Progress: pending review Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Projects

Status: Pending Review

Development

Successfully merging this pull request may close these issues.

2 participants