Explorar o código

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.
AnotherTest %!s(int64=4) %!d(string=hai) anos
pai
achega
e1512d5968
Modificáronse 4 ficheiros con 62 adicións e 42 borrados
  1. 3 2
      Userland/Shell/Builtin.cpp
  2. 12 4
      Userland/Shell/Shell.cpp
  3. 1 1
      Userland/Shell/Shell.h
  4. 46 35
      Userland/Shell/main.cpp

+ 3 - 2
Userland/Shell/Builtin.cpp

@@ -299,9 +299,10 @@ int Shell::builtin_exit(int argc, const char** argv)
         }
     }
     stop_all_jobs();
-    m_editor->save_history(get_history_path());
-    if (m_is_interactive)
+    if (m_is_interactive) {
+        m_editor->save_history(get_history_path());
         printf("Good-bye!\n");
+    }
     exit(exit_code);
     return 0;
 }

+ 12 - 4
Userland/Shell/Shell.cpp

@@ -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());
 }
 

+ 1 - 1
Userland/Shell/Shell.h

@@ -264,7 +264,7 @@ public:
 #undef __ENUMERATE_SHELL_OPTION
 
 private:
-    Shell(Line::Editor&);
+    Shell(Line::Editor&, bool attempt_interactive);
     Shell();
     virtual ~Shell() override;
 

+ 46 - 35
Userland/Shell/main.cpp

@@ -60,21 +60,6 @@ int main(int argc, char** argv)
         s_shell->editor()->save_history(s_shell->get_history_path());
     });
 
-    editor = Line::Editor::construct();
-    editor->initialize();
-
-    auto shell = Shell::Shell::construct(*editor);
-    s_shell = shell.ptr();
-
-    s_shell->setup_signals();
-
-#ifndef __serenity__
-    sigset_t blocked;
-    sigemptyset(&blocked);
-    sigaddset(&blocked, SIGTTOU);
-    sigaddset(&blocked, SIGTTIN);
-    pthread_sigmask(SIG_BLOCK, &blocked, nullptr);
-#endif
 #ifdef __serenity__
     if (pledge("stdio rpath wpath cpath proc exec tty accept sigaction unix fattr", nullptr) < 0) {
         perror("pledge");
@@ -82,23 +67,43 @@ int main(int argc, char** argv)
     }
 #endif
 
-    shell->termios = editor->termios();
-    shell->default_termios = editor->default_termios();
-
-    editor->on_display_refresh = [&](auto& editor) {
-        editor.strip_styles();
-        if (shell->should_format_live()) {
-            auto line = editor.line();
-            ssize_t cursor = editor.cursor();
-            editor.clear_line();
-            editor.insert(shell->format(line, cursor));
-            if (cursor >= 0)
-                editor.set_cursor(cursor);
-        }
-        shell->highlight(editor);
-    };
-    editor->on_tab_complete = [&](const Line::Editor&) {
-        return shell->complete();
+    RefPtr<::Shell::Shell> shell;
+    bool attempt_interactive = false;
+
+    auto initialize = [&] {
+        editor = Line::Editor::construct();
+        editor->initialize();
+
+        shell = Shell::Shell::construct(*editor, attempt_interactive);
+        s_shell = shell.ptr();
+
+        s_shell->setup_signals();
+
+#ifndef __serenity__
+        sigset_t blocked;
+        sigemptyset(&blocked);
+        sigaddset(&blocked, SIGTTOU);
+        sigaddset(&blocked, SIGTTIN);
+        pthread_sigmask(SIG_BLOCK, &blocked, nullptr);
+#endif
+        shell->termios = editor->termios();
+        shell->default_termios = editor->default_termios();
+
+        editor->on_display_refresh = [&](auto& editor) {
+            editor.strip_styles();
+            if (shell->should_format_live()) {
+                auto line = editor.line();
+                ssize_t cursor = editor.cursor();
+                editor.clear_line();
+                editor.insert(shell->format(line, cursor));
+                if (cursor >= 0)
+                    editor.set_cursor(cursor);
+            }
+            shell->highlight(editor);
+        };
+        editor->on_tab_complete = [&](const Line::Editor&) {
+            return shell->complete();
+        };
     };
 
     const char* command_to_run = nullptr;
@@ -118,8 +123,6 @@ int main(int argc, char** argv)
 
     parser.parse(argc, argv);
 
-    shell->set_live_formatting(should_format_live);
-
     if (format) {
         auto file = Core::File::open(format, Core::IODevice::ReadOnly);
         if (file.is_error()) {
@@ -127,6 +130,8 @@ int main(int argc, char** argv)
             return 1;
         }
 
+        initialize();
+
         ssize_t cursor = -1;
         puts(shell->format(file.value()->read_all(), cursor).characters());
         return 0;
@@ -152,6 +157,12 @@ int main(int argc, char** argv)
         }
     }
 
+    auto execute_file = file_to_read_from && StringView { "-" } != file_to_read_from;
+    attempt_interactive = !execute_file;
+
+    initialize();
+
+    shell->set_live_formatting(should_format_live);
     shell->current_script = argv[0];
 
     if (!skip_rc_files) {
@@ -179,7 +190,7 @@ int main(int argc, char** argv)
         return shell->run_command(command_to_run);
     }
 
-    if (file_to_read_from && StringView { "-" } != file_to_read_from) {
+    if (execute_file) {
         if (shell->run_file(file_to_read_from))
             return 0;
         return 1;