Skip to content
This repository has been archived by the owner on Aug 4, 2024. It is now read-only.

Commit

Permalink
web-shims: only update UI on new line
Browse files Browse the repository at this point in the history
- magiskboot doesn't stop at the middle of a line
  so there were not actually needed

  this should improve terminal perf by a little

Signed-off-by: Ookiineko <chiisaineko@protonmail.com>
  • Loading branch information
Ookiineko committed Apr 19, 2024
1 parent 70dfa34 commit 833fa71
Showing 1 changed file with 6 additions and 25 deletions.
31 changes: 6 additions & 25 deletions src/web-shims/conio_hack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
/* 20 ms */
#define MBB_CONIO_DELAY (20)

/* 10 characters */
#define MBB_CONOUT_THRESHOLD (10)

// used for exit code callback in JS

extern int __mbb_main(int argc, char **argv);
Expand Down Expand Up @@ -95,7 +92,7 @@ extern "C" {
}
}

static void __exit_hook(int status) {
static inline void __exit_hook(int status) {
EM_ASM({
Module['mbb_main_cb']($0);
}, status);
Expand All @@ -117,28 +114,17 @@ __attribute__((noreturn)) static void __exit_wrap(int status) {
__real_exit(status);
}

static void __conout_hook(const char *s, size_t len) {
// when should we update UI?

if (len >= MBB_CONOUT_THRESHOLD)
goto do_sleep; // when the line is long enough

if (memchr(s, '\n', len) || memchr(s, '\r', len))
goto do_sleep; // when there is a newline char in the str
static inline void __conout_hook(const char *s, size_t len) {
// give browser a chance to update UI on each new line

return;

do_sleep:
// sleeping is even more expensive to perf
// so only do this when needed
emscripten_sleep(MBB_CONIO_DELAY); // note this needs -sASYNCIFY in LDFLAGS
if (memchr(s, '\n', len))
emscripten_sleep(MBB_CONIO_DELAY); // note this needs -sASYNCIFY in LDFLAGS
}

static void __check_do_conout_hook(int fd, const char *s, size_t len) {
static inline void __check_do_conout_hook(int fd, const char *s, size_t len) {
switch (fd) {
case STDOUT_FILENO:
case STDERR_FILENO:
// give browser a chance to update UI
__conout_hook(s, len);
break;
default:
Expand Down Expand Up @@ -193,8 +179,3 @@ static void __enable_conio_hack(void) {
EMSCRIPTEN_BINDINGS(conio_hack) {
emscripten::function("mbb_enable_conio_hack", &__enable_conio_hack);
}

__attribute__((constructor)) static void __init_io_mode(void) {
// disable line buffering
setvbuf(stdout, NULL, _IONBF, BUFSIZ);
}

0 comments on commit 833fa71

Please sign in to comment.