-
Notifications
You must be signed in to change notification settings - Fork 264
[bin-wrappers] Use shellenv command in binwrappers #876
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
Conversation
internal/impl/devbox.go
Outdated
|
||
// minor optimization. If we've already computed the non-cache version, use | ||
// that instead. | ||
if nixEnvCache == nil { |
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.
Is this supposed to be:
if nixEnvCache == nil { | |
if nixEnvCache != nil { |
internal/impl/devbox.go
Outdated
func addHashToEnv(env map[string]string) map[string]string { | ||
data := []byte{} | ||
for k, v := range env { | ||
data = append(data, []byte(k)...) | ||
data = append(data, []byte(v)...) | ||
} | ||
hash := md5.Sum(data) | ||
env[devboxShellEnvHashVarName] = hex.EncodeToString(hash[:]) | ||
return env | ||
} |
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.
data
can be removed by hashing as you go:
func addHashToEnv(env map[string]string) map[string]string { | |
data := []byte{} | |
for k, v := range env { | |
data = append(data, []byte(k)...) | |
data = append(data, []byte(v)...) | |
} | |
hash := md5.Sum(data) | |
env[devboxShellEnvHashVarName] = hex.EncodeToString(hash[:]) | |
return env | |
} | |
func addHashToEnv(env map[string]string) map[string]string { | |
h := md5.New() | |
for k, v := range env { | |
h.Write([]byte(k)) | |
h.Write([]byte(v)) | |
} | |
env[devboxShellEnvHashVarName] = hex.EncodeToString(md5.Sum(data)) | |
return env | |
} |
If you want, the return value can also be removed since the function is modifying the map directly.
internal/nix/nix.go
Outdated
if _, err := os.Stat(args.PrintDevEnvCachePath); err == nil { | ||
data, err = os.ReadFile(args.PrintDevEnvCachePath) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
if err := json.Unmarshal(data, &out); err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
} |
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.
You can remove the os.Stat
call and just trying reading:
if _, err := os.Stat(args.PrintDevEnvCachePath); err == nil { | |
data, err = os.ReadFile(args.PrintDevEnvCachePath) | |
if err != nil { | |
return nil, errors.WithStack(err) | |
} | |
if err := json.Unmarshal(data, &out); err != nil { | |
return nil, errors.WithStack(err) | |
} | |
} | |
cachedData, err := os.ReadFile(args.PrintDevEnvCachePath) | |
if err == nil { | |
if err := json.Unmarshal(data, &out); err != nil { | |
return nil, errors.WithStack(err) | |
} | |
cachedData = data | |
} else if !errors.Is(err, fs.ErrNotExist) { | |
return nil, errors.WithStack(err) | |
} |
Summary
This changes bin wrappers to use
eval $(devbox shellenv --use-cached-print-dev-env)
Motivation:
For performance reasons we were printing out the shellenv results in the bin wrappers. The problem with this approach is that the env vars in the wrappers overwrite whatever the user sets. For example:
USER=foo node -e "console.log(process.env.USER);"
will print
mike
becauseexport USER="mike"
is in the wrapper.Solution:
eval $(devbox shellenv)
in the wrapper.--use-cached-print-dev-env
flag to shellenv that uses a cached copy ofnix print-dev-env
if it exists. The cache is regenerated every timecomputeNixEnv
is called so it happens on shell, run, adding/removing package, when wrappers are created, and also when shellenv is called without the flag.Latency impact when hash matches: None, or maybe a bit < 1ms faster.
Latency impact when hash doesn't match: ~20ms slower
Potential additional optimization 1: skip launcher when calling
devbox shellenv
. This should reduce about ~10ms of latency.Potential additional optimization 2: when hash matches, instead of saving a wrapper we could save a symlink. This would eliminate extra latency of wrapper.
How was it tested?
hello