1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:58:11 +00:00

Shell: Skip caching PATH and history load/save when not interactive

Non-interactive shells (i.e. when running scripts) do not need this
functionality, so they are a boatload of wasted time.
This significantly reduces the script startup and shutdown times when
there are lots of executables in PATH or lots of entries in the history.
This commit is contained in:
AnotherTest 2021-03-07 09:42:24 +03:30 committed by Andreas Kling
parent 48347fb1c7
commit e1512d5968
4 changed files with 61 additions and 41 deletions

View file

@ -1222,6 +1222,9 @@ String Shell::unescape_token(const String& token)
void Shell::cache_path()
{
if (!m_is_interactive)
return;
if (!cached_path.is_empty())
cached_path.clear_with_capacity();
@ -1691,7 +1694,7 @@ Shell::Shell()
cache_path();
}
Shell::Shell(Line::Editor& editor)
Shell::Shell(Line::Editor& editor, bool attempt_interactive)
: m_editor(editor)
{
uid = getuid();
@ -1705,7 +1708,7 @@ Shell::Shell(Line::Editor& editor)
perror("gethostname");
auto istty = isatty(STDIN_FILENO);
m_is_interactive = istty;
m_is_interactive = attempt_interactive && istty;
if (istty) {
rc = ttyname_r(0, ttyname, Shell::TTYNameSize);
@ -1733,8 +1736,10 @@ Shell::Shell(Line::Editor& editor)
}
directory_stack.append(cwd);
m_editor->load_history(get_history_path());
cache_path();
if (m_is_interactive) {
m_editor->load_history(get_history_path());
cache_path();
}
m_editor->register_key_input_callback('\n', [](Line::Editor& editor) {
auto ast = Parser(editor.line()).parse();
@ -1751,6 +1756,9 @@ Shell::~Shell()
return;
stop_all_jobs();
if (!m_is_interactive)
return;
m_editor->save_history(get_history_path());
}