summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorDaniel Bertalan <dani@danielbertalan.dev>2021-12-29 18:37:31 +0100
committerAndreas Kling <kling@serenityos.org>2021-12-30 14:26:29 +0100
commit6b39c6b1bf62218edf6478c8f55a543e069ea51a (patch)
treee3d051362b6e3d1d11c64dc8e9cdb190021bde50 /Userland
parentc19632128c84e1190b95d1b18baf56ffe306e192 (diff)
downloadserenity-6b39c6b1bf62218edf6478c8f55a543e069ea51a.zip
Shell: Avoid many single byte write() syscalls when printing the prompt
Whenever the prompt is printed, we write a line's worth of space characters to the terminal to ensure that the prompt ends up on a new line even if there is dangling output on the current line. We write these to the stderr, which is unbuffered, so each putc() call would come with the overhead of a system call. Let's use a buffer + fwrite() instead, since heap allocation is much faster.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Shell/Shell.cpp7
1 files changed, 5 insertions, 2 deletions
diff --git a/Userland/Shell/Shell.cpp b/Userland/Shell/Shell.cpp
index 0b31ed99f6..d0630ffe7d 100644
--- a/Userland/Shell/Shell.cpp
+++ b/Userland/Shell/Shell.cpp
@@ -1660,8 +1660,11 @@ void Shell::bring_cursor_to_beginning_of_a_line() const
fputs(eol_mark.characters(), stderr);
- for (auto i = eol_mark_length; i < ws.ws_col; ++i)
- putc(' ', stderr);
+ // We write a line's worth of whitespace to the terminal. This way, we ensure that
+ // the prompt ends up on a new line even if there is dangling output on the current line.
+ size_t fill_count = ws.ws_col - eol_mark_length;
+ auto fill_buffer = String::repeated(' ', fill_count);
+ fwrite(fill_buffer.characters(), 1, fill_count, stderr);
putc('\r', stderr);
}