mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-16 23:14:04 +00:00
Reuse the variable event for for-loops
This used to construct a vector, which was then passed down and filled with a new event_t each go around the loop. That's useless - we fire one event here, and it's simply the variable event. This reduces the overhead of a for-loop by ~10%: ```fish for i in (seq 100000) true end ``` runs in about 90% of the time now.
This commit is contained in:
parent
452675d458
commit
0c3c3eaa99
1 changed files with 9 additions and 6 deletions
|
@ -457,13 +457,15 @@ end_execution_reason_t parse_execution_context_t::run_for_statement(
|
||||||
L"You cannot use read-only variable '%ls' in a for loop",
|
L"You cannot use read-only variable '%ls' in a for loop",
|
||||||
for_var_name.c_str());
|
for_var_name.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We fire the same event over and over again, just construct it once.
|
||||||
|
event_t evt = event_t::variable_set(for_var_name);
|
||||||
|
auto &vars = parser->vars();
|
||||||
int retval;
|
int retval;
|
||||||
if (var) {
|
retval = vars.set(for_var_name, ENV_LOCAL | ENV_USER, var ? var->as_list() : wcstring_list_t{});
|
||||||
retval = parser->set_var_and_fire(for_var_name, ENV_LOCAL | ENV_USER, var->as_list());
|
|
||||||
} else {
|
|
||||||
retval = parser->set_empty_var_and_fire(for_var_name, ENV_LOCAL | ENV_USER);
|
|
||||||
}
|
|
||||||
assert(retval == ENV_OK);
|
assert(retval == ENV_OK);
|
||||||
|
// TODO: For historical reasons we fire here as well, I'm not sure that makes sense?
|
||||||
|
event_fire(*parser, evt);
|
||||||
|
|
||||||
trace_if_enabled(*parser, L"for", arguments);
|
trace_if_enabled(*parser, L"for", arguments);
|
||||||
block_t *fb = parser->push_block(block_t::for_block());
|
block_t *fb = parser->push_block(block_t::for_block());
|
||||||
|
@ -475,9 +477,10 @@ end_execution_reason_t parse_execution_context_t::run_for_statement(
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
int retval = parser->set_var_and_fire(for_var_name, ENV_DEFAULT | ENV_USER, val);
|
retval = vars.set(for_var_name, ENV_DEFAULT | ENV_USER, {val});
|
||||||
assert(retval == ENV_OK && "for loop variable should have been successfully set");
|
assert(retval == ENV_OK && "for loop variable should have been successfully set");
|
||||||
(void)retval;
|
(void)retval;
|
||||||
|
event_fire(*parser, evt);
|
||||||
|
|
||||||
auto &ld = parser->libdata();
|
auto &ld = parser->libdata();
|
||||||
ld.loop_status = loop_status_t::normals;
|
ld.loop_status = loop_status_t::normals;
|
||||||
|
|
Loading…
Reference in a new issue