Cache the expanded prompt for editline
PATCH 1/1 Cache the expanded prompt for editline
Previously, the prompt would be expanded every time editline called the getprompt callback. I think the code may have been written assuming that editline only calls getprompt once per prompt, but it may actually call it many times, for instance every time you type backspace. This results not only in slower editing from expanding complex prompts repeatedly, it also consumes more and more stack memory each time getprompt is called. This can be seen by setting PS1 to some command substitution, typing many characters at the prompt, then holding backspace and observing memory usage. Thankfully all this stack memory is freed between prompts by the stackmark calls around el_gets.
This change causes prompt expansion to always happen in the setprompt call, as it would when editline is disabled, and a cached copy of the prompt is saved for getprompt to return every time editline calls it. Since getprompt is no longer doing expansion, the stackmark calls surrounding el_gets can be removed.
--- src/input.c | 6 +---- src/parser.c | 64 ++++++++++++++++++++++++++++------------------------ 2 files changed, 35 insertions(+), 35 deletions(-)
src/input.c +1 -5
@@ -152,12 +152,8 @@ retry:static const char *rl_cp;static int el_len; -if (rl_cp ==NULL ) { -struct stackmark smark; -pushstackmark (&smark,stackblocksize ()); +if (rl_cp ==NULL ) rl_cp =el_gets (el, &el_len); -popstackmark (&smark); - }if (rl_cp ==NULL ) nr =0 ;else {
src/parser.c +34 -30
96 lines not on this page · raw message
Signature · 1 line
2.29.2
PATCH v2 1/1 Cache the expanded prompt for editline
Previously, the prompt would be expanded every time editline called the getprompt callback. I think the code may have been written assuming that editline only calls getprompt once per prompt, but it may actually call it many times, for instance every time you type backspace. This results not only in slower editing from expanding complex prompts repeatedly, it also consumes more and more stack memory each time getprompt is called. This can be seen by setting PS1 to some command substitution, typing many characters at the prompt, then holding backspace and observing memory usage. Thankfully all this stack memory is freed between prompts by the stackmark calls around el_gets.
This change causes prompt expansion to always happen in the setprompt call, as it would when editline is disabled, and a cached copy of the prompt is saved for getprompt to return every time editline calls it. Since getprompt is no longer doing expansion, the stackmark calls surrounding el_gets can be removed.
---
v2: Missed adding breaks when replacing returns with assignments.
src/input.c | 6 +---- src/parser.c | 66 ++++++++++++++++++++++++++++------------------------ 2 files changed, 37 insertions(+), 35 deletions(-)
src/input.c +1 -5
@@ -152,12 +152,8 @@ retry:static const char *rl_cp;static int el_len; -if (rl_cp ==NULL ) { -struct stackmark smark; -pushstackmark (&smark,stackblocksize ()); +if (rl_cp ==NULL ) rl_cp =el_gets (el, &el_len); -popstackmark (&smark); - }if (rl_cp ==NULL ) nr =0 ;else {
src/parser.c +36 -30
98 lines not on this page · raw message
Signature · 1 line
2.29.2
Quoted 11 lines · C. McEnroe
Previously, the prompt would be expanded every time editline called the getprompt callback. I think the code may have been written assuming that editline only calls getprompt once per prompt, but it may actually call it many times, for instance every time you type backspace. This results not only in slower editing from expanding complex prompts repeatedly, it also consumes more and more stack memory each time getprompt is called. This can be seen by setting PS1 to some command substitution, typing many characters at the prompt, then holding backspace and observing memory usage. Thankfully all this stack memory is freed between prompts by the stackmark calls around
el_gets.
Quoted 8 lines
This change causes prompt expansion to always happen in the setprompt call, as it would when editline is disabled, and a cached copy of the prompt is saved for getprompt to return every time editline calls it. Since getprompt is no longer doing expansion, the stackmark calls surrounding
el_getscan be removed.
---v2: Missed adding breaks when replacing returns with assignments.
What if someone actually wanted the prompt to change?
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Quoted 3 lines · Herbert Xu
What if someone actually wanted the prompt to change?
It seems unlikely given the lack of consistency of when libedit calls the prompt callback. I only noticed it was happening when I patched in support for EL_RPROMPT (right-aligned prompts), which when active causes libedit to call the prompt callback on *every* keypress. It seemed not to happen with only a regular prompt and it took some experimenting to notice typing backspaces causes it. The behaviour is completely undocumented in libedit.
Anecdotally, typing at the prompt becomes painful when it is expanded over and over and does anything non-trivial, even just a command substitution that runs no external commands.
Quoted 9 lines · June Bug
It seems unlikely given the lack of consistency of when libedit calls the prompt callback. I only noticed it was happening when I patched in support for
EL_RPROMPT(right-aligned prompts), which when active causes libedit to call the prompt callback on *every* keypress. It seemed not to happen with only a regular prompt and it took some experimenting to notice typing backspaces causes it. The behaviour is completely undocumented in libedit.
Quoted 3 lines
Anecdotally, typing at the prompt becomes painful when it is expanded over and over and does anything non-trivial, even just a command substitution that runs no external commands.
OK, in that case I think it's best to fix this in libedit instead.
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Quoted 3 lines · Herbert Xu
OK, in that case I think it's best to fix this in libedit instead.
Huh? There’s no issue in libedit, only unclear documentation. The application is the right place to store the prompt: it can be in a static buffer, or a constant string, in which case libedit has no need to save its own copy.