0

I quite like a customized PS1 variable that is computed fast. Hence, I have a line in my Bash-script where I set the PS1 variable via

PROMPT_COMMAND='PS1="$(/home/username/.config/bash/ps1 ${PWD})"'

Here ps1 is my own executable for obtaining the PS1 given the current working directory PWD.

Hitting enter with this setup, a new line appears without the prompt (I mainly notice this because the cursor is at the beginning of the line). This rarely happens in the regular sh.

Let us assume that I am always standing in the same directory, and simply holding enter such that the same process is run over and over again. Then I still see the cursor at the beginning of the line. However, hyperfine says that

~/.c/bash $ hyperfine -N --warmup=1000 "/home/username/.config/bash/ps1 $PWD"
Benchmark 1: /home/username/.config/bash/ps1 /home/username/.config/bash
  Time (mean ± σ):       0.5 ms ±   0.1 ms    [User: 0.4 ms, System: 0.0 ms]
  Range (min … max):     0.4 ms …   2.1 ms    5335 runs

and so with a computer screen at 60 Hz (i.e. ~16 ms between each frame), the prompt should be generated almost instantaneous, and a blank line should rarely be drawn.

Why is this happening even though the executable is very fast? As you can see from the results, the executable is pretty optimized, but it does contain a few fopen and one or two fread and fclose, as well as one printf.

Can I generate/print PS1 even faster? If I can, how?

2
  • Are you sure the behaviour of the command is the same when run in hyperfine?
    – muru
    Nov 15, 2022 at 17:36
  • @muru Yes, I am. However, I am sure that Bash is an overhead I cannot avoid (but perhaps I can?), and so this might not be fixable. The best would be to not print PS1, but rather to alter it inside the executable.
    – aahlback
    Nov 15, 2022 at 17:51

1 Answer 1

0

I did manage to fix this. I downloaded the source code of Bash and took the source code of /home/username/.config/bash/ps1 and merged them into the Bash source code. The key was to alter the function static void execute_prompt_command() in eval.c. You probably have to learn some internals of Bash, but I was done in about 15 minutes after downloading the source code. For example, I had to obtaining the PWD via pwd = get_string_value("PWD") instead of getting it from argv. It works as intended and is blazing fast!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .