Optimize function calls by reducing inherit vars heap allocations and copies

* Convert `function_get_inherit_vars()` to return a reference to the
  (possibly) existing map, rather than a copy;
* Preallocate and reuse a static (read-only) map for the (very) common
  case of no inherited vars;
* Pass references to the inherit vars map around thereafter, never
  triggering the map copy (or even move) constructor.

NB: If it turns out the reference is unsafe, we can switch the inherit vars
to be a shared_ptr and return that instead.
This commit is contained in:
Mahmoud Al-Qudsi 2019-04-13 11:26:10 -05:00
parent 56125f73e4
commit cdce8511a1
4 changed files with 7 additions and 5 deletions

View file

@ -199,7 +199,7 @@ static wcstring functions_def(const wcstring &name) {
} }
// Output any inherited variables as `set -l` lines. // Output any inherited variables as `set -l` lines.
std::map<wcstring, env_var_t> inherit_vars = function_get_inherit_vars(name); const std::map<wcstring, env_var_t> &inherit_vars = function_get_inherit_vars(name);
for (const auto &kv : inherit_vars) { for (const auto &kv : inherit_vars) {
wcstring_list_t lst; wcstring_list_t lst;
kv.second.to_list(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; 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 = function_block_t *fb =
parser.push_block<function_block_t>(p, func_name, props->shadow_scope); parser.push_block<function_block_t>(p, func_name, props->shadow_scope);

View file

@ -257,10 +257,12 @@ bool function_get_definition(const wcstring &name, wcstring &out_definition) {
return func != NULL; return func != NULL;
} }
std::map<wcstring, env_var_t> function_get_inherit_vars(const wcstring &name) { 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;
scoped_rlock locker(functions_lock); scoped_rlock locker(functions_lock);
const function_info_t *func = function_get(name); const function_info_t *func = function_get(name);
return func ? func->inherit_vars : std::map<wcstring, env_var_t>(); return func ? func->inherit_vars : empty_inherit_vars;
} }
bool function_get_desc(const wcstring &name, wcstring &out_desc) { 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 /// Returns a mapping of all variables of the specified function that were inherited from the scope
/// of the function definition to their values. /// of the function definition to their values.
std::map<wcstring, env_var_t> function_get_inherit_vars(const wcstring &name); const 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 /// Creates a new function using the same definition as the specified function. Returns true if copy
/// is successful. /// is successful.