Skip to content

Commit

Permalink
perf: simplify export enumeration (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
reubeno authored Jan 26, 2025
1 parent 39a291f commit b237567
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
11 changes: 3 additions & 8 deletions brush-core/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,7 @@ pub(crate) fn compose_std_command<S: AsRef<OsStr>>(
cmd.arg0(argv0);

// Pass through args.
for arg in args {
cmd.arg(arg);
}
cmd.args(args);

// Use the shell's current working dir.
cmd.current_dir(shell.working_dir.as_path());
Expand All @@ -224,11 +222,8 @@ pub(crate) fn compose_std_command<S: AsRef<OsStr>>(

// Add in exported variables.
if !empty_env {
for (name, var) in shell.env.iter() {
if var.is_exported() {
let value_as_str = var.value().to_cow_str(shell);
cmd.env(name, value_as_str.as_ref());
}
for (k, v) in shell.env.iter_exported() {
cmd.env(k.as_str(), v.value().to_cow_str(shell).as_ref());
}
}

Expand Down
19 changes: 19 additions & 0 deletions brush-core/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ impl ShellEnvironment {
// Iterators/Getters
//

/// Returns an iterator over all exported variables defined in the variable.
pub fn iter_exported(&self) -> impl Iterator<Item = (&String, &ShellVariable)> {
// We won't actually need to store all entries, but we expect it should be
// within the same order.
let mut visible_vars: HashMap<&String, &ShellVariable> =
HashMap::with_capacity(self.entry_count);

for (_, var_map) in self.scopes.iter().rev() {
for (name, var) in var_map.iter().filter(|(_, v)| v.is_exported()) {
// Only insert the variable if it hasn't been seen yet.
if let hash_map::Entry::Vacant(entry) = visible_vars.entry(name) {
entry.insert(var);
}
}
}

visible_vars.into_iter()
}

/// Returns an iterator over all the variables defined in the environment.
pub fn iter(&self) -> impl Iterator<Item = (&String, &ShellVariable)> {
self.iter_using_policy(EnvironmentLookup::Anywhere)
Expand Down

0 comments on commit b237567

Please sign in to comment.