Revert "Optimize function calls by reducing inherit vars heap allocations and copies"

This reverts commit cdce8511a1.

This change was unsafe. The prior version (now restored) took the lock and
then copied the data. By returning a reference, the caller holds a
reference to data outside of the lock.

This function isn't worth optimizing. Hardly any functions use this
facility, and for those that do, they typically just capture one or two
variables.
This commit is contained in:
ridiculousfish
2019-04-13 11:58:18 -07:00
parent cdce8511a1
commit 13c5f93d63
4 changed files with 5 additions and 7 deletions

View File

@@ -199,7 +199,7 @@ static wcstring functions_def(const wcstring &name) {
}
// Output any inherited variables as `set -l` lines.
const std::map<wcstring, env_var_t> &inherit_vars = function_get_inherit_vars(name);
std::map<wcstring, env_var_t> inherit_vars = function_get_inherit_vars(name);
for (const auto &kv : inherit_vars) {
wcstring_list_t lst;
kv.second.to_list(lst);

View File

@@ -813,7 +813,7 @@ static bool exec_block_or_func_process(parser_t &parser, std::shared_ptr<job_t>
return false;
}
const std::map<wcstring, env_var_t> &inherit_vars = function_get_inherit_vars(func_name);
const std::map<wcstring, env_var_t> inherit_vars = function_get_inherit_vars(func_name);
function_block_t *fb =
parser.push_block<function_block_t>(p, func_name, props->shadow_scope);

View File

@@ -257,12 +257,10 @@ bool function_get_definition(const wcstring &name, wcstring &out_definition) {
return func != NULL;
}
const std::map<wcstring, env_var_t> &function_get_inherit_vars(const wcstring &name) {
static const std::map<wcstring, env_var_t> empty_inherit_vars;
std::map<wcstring, env_var_t> function_get_inherit_vars(const wcstring &name) {
scoped_rlock locker(functions_lock);
const function_info_t *func = function_get(name);
return func ? func->inherit_vars : empty_inherit_vars;
return func ? func->inherit_vars : std::map<wcstring, env_var_t>();
}
bool function_get_desc(const wcstring &name, wcstring &out_desc) {

View File

@@ -102,7 +102,7 @@ int function_get_definition_lineno(const wcstring &name);
/// Returns a mapping of all variables of the specified function that were inherited from the scope
/// of the function definition to their values.
const std::map<wcstring, env_var_t> &function_get_inherit_vars(const wcstring &name);
std::map<wcstring, env_var_t> function_get_inherit_vars(const wcstring &name);
/// Creates a new function using the same definition as the specified function. Returns true if copy
/// is successful.