Skip to content
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

Resolve shell env API performance issue for single env approach #238488

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

anthonykim1
Copy link
Contributor

@anthonykim1 anthonykim1 commented Jan 22, 2025

Potential root of the performance cause:

  1. Using diff approach where we check for change the environment variables, and only firing an event if there is a difference, should resolve the problem
  2. O(n) - iterating through each environment variable in the process of fetching them (inside shell such as bash) is what is slow
  3. Print statements in the above O(n) is what is causing the slowness (shell side: e.g. bash side)
  4. O(n) - iterating through each environment variable when trying to calculate the diff is what is slow (in the shellEnvDetectionCapability)

Edit: d3ef701 brought back instant bash prompts. I think it was combination of printenv with compgen and tons of printf statements really slowing things down. So Mainly Scenario number 3 was correct

@anthonykim1 anthonykim1 self-assigned this Jan 22, 2025
@anthonykim1
Copy link
Contributor Author

anthonykim1 commented Jan 22, 2025

https://github.com/user-attachments/assets/57167cc9-fd5b-424b-b302-83542592365d
@Tyriar This is for bash, but I think I brought back the perf to normal


for i in "${!vsc_env_keys[@]}"; do
if [[ "${vsc_env_keys[$i]}" == "$key" ]]; then
if [[ "${vsc_env_values[$i]}" != "$value" ]]; then
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this allow us to only printf and send to VS Code if there is a difference in terms of value.

builtin printf '\e]633;EnvSingleEntry;%s;%s;%s\a' "$key" "$(__vsc_escape_value "$value")" "$__vsc_nonce"
below allow us to send to VS Code if it is a brand new variable.

builtin printf '\e]633;EnvSingleEntry;%s;%s;%s\a' "$key" "$(__vsc_escape_value "$value")" "$__vsc_nonce"
}

trackMissingEnvVars() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we now track a cached Map (two array because associate array are only supported in bash version 4.0 or above), we also need to make sure environment variable deletion gets accounted. Both in bash side and VS Code side.

If there is an environment variable that was cached before but not in user's environment variable list anymore, we need to detect that and update the map accordingly, and let VS Code know. (by something like EnvSingleDelete.. etc

# builtin printf "Time taken: $elapsed_time seconds\n"

trackMissingEnvVars
builtin printf "Time taken: $elapsed_time seconds\n"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although we have more loops now in the script, time is actually shorter because we use the least number of printf statement as much as possible, avoid using printenv, and compgen.

Check this by looking at the time calculation and just how things feel.

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