-
Notifications
You must be signed in to change notification settings - Fork 31
Optimize environment variable slice allocation in ExecuteHandler #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Summary of ChangesHello @acsoto, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the performance of the environment variable handling within the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a good performance optimization by pre-allocating the slice for environment variables in the ExecuteHandler. This effectively avoids repeated slice reallocations, leading to reduced memory usage and CPU time as demonstrated by the benchmark results. The change is correct and well-contained. I've added one minor suggestion to make the implementation slightly more idiomatic, but the current approach is also valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR optimizes memory allocation in the ExecuteHandler function by pre-allocating the environment variable slice with the exact required capacity before appending values, reducing memory reallocations during command execution.
Changes:
- Pre-allocate
currentEnvslice with capacitylen(environ) + len(req.Env)to avoid dynamic growth - Store
os.Environ()result in intermediate variableenvironbefore creating the slice - Reduce memory allocations and copying during environment variable setup
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #179 +/- ##
==========================================
+ Coverage 35.60% 35.82% +0.21%
==========================================
Files 29 29
Lines 2533 2551 +18
==========================================
+ Hits 902 914 +12
- Misses 1505 1511 +6
Partials 126 126
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Signed-off-by: Zhou Zihang <z@mcac.cc>
e10588b to
7038877
Compare
| // Set environment variables | ||
| if len(req.Env) > 0 { | ||
| currentEnv := os.Environ() | ||
| environ := os.Environ() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to know why do we pass os env to the exec cmd. This may expose os settings to malicious code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the valid security concern.
Currently, picod inherits the host environment (os.Environ()) by design to ensure the availability of the underlying toolchain like Python (e.g., PATH, HOME, LANG), as picod acts as an arbitrary command executor.
We should sanitize the environment (e.g., using a whitelist for PATH and other essentials) to prevent secret leakage.
|
Replying to @hzxuzhonghu: You are right about the security risk. However, simply removing
So we cannot just drop the environment. The proper fix is to implement an allowlist (e.g., PATH, HOME, LANG) instead of passing everything. Since this PR is strictly for performance optimization, I suggest we merge this to fix the memory allocation issue first. I have opened a separate issue #183 to implement the environment allowlist/sanitization to address the security concern properly. |
Pre-allocate the slice capacity when merging environment variables in
ExecuteHandler.Previously,
appendwas used on the result ofos.Environ(), which could trigger multiple allocations as the slice grew. Since we know the final size (len(os.Environ()) + len(req.Env)), pre-allocating the capacity avoids these unnecessary allocations and copy operations.