Optimize get_deferred_process() traversal

This commit is contained in:
Mahmoud Al-Qudsi 2019-03-31 13:20:49 -05:00
parent aafd706a34
commit 3c537bfa65

View file

@ -993,21 +993,20 @@ static bool exec_process_in_job(parser_t &parser, process_t *p, std::shared_ptr<
// This should show the output as it comes, not buffer until the end.
// Any such process (only one per job) will be called the "deferred" process.
static process_t *get_deferred_process(const shared_ptr<job_t> &j) {
process_t *last_internal = nullptr;
for (const auto &p : j->processes) {
// By enumerating in reverse order, we can avoid walking the entire list
for (auto i = j->processes.rbegin(); i != j->processes.rend(); ++i) {
const auto &p = *i;
if (p->type == process_type_t::exec) {
// No tail reordering for execs.
return nullptr;
} else if (p->type != process_type_t::external) {
last_internal = p.get();
}
}
if (last_internal && !last_internal->is_last_in_job) {
// This is the last internal process, and it pipes to an external process.
return last_internal;
} else {
if (p->is_last_in_job) {
return nullptr;
}
return p.get();
}
}
return nullptr;
}
bool exec_job(parser_t &parser, shared_ptr<job_t> j) {