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

Commit 0660c3d

Browse files
author
Ookiineko
committed
web-shims: reduce unnecessary console io sleep
- magiskboot doesn't stop at the middle of a line so those were not actually needed when getting a massive output, lower the delay this should improve terminal perf by a little Signed-off-by: Ookiineko <[email protected]>
1 parent 70dfa34 commit 0660c3d

File tree

1 file changed

+49
-30
lines changed

1 file changed

+49
-30
lines changed

src/web-shims/conio_hack.cc

+49-30
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@
77
#include <emscripten.h>
88
#include <emscripten/bind.h>
99

10+
/* 50 lines */
11+
#define MBB_CONIO_BASE_THRESHOLD (50)
12+
13+
/* 2 lines */
14+
#define MBB_CONIO_LOWER_THRESHOLD (2)
15+
1016
/* 20 ms */
11-
#define MBB_CONIO_DELAY (20)
17+
#define MBB_CONIO_BASE_DELAY (20)
18+
19+
/* 1 ms */
20+
#define MBB_CONIO_LOWEST_DELAY (1)
21+
22+
static char mbb_conio_cnt;
1223

13-
/* 10 characters */
14-
#define MBB_CONOUT_THRESHOLD (10)
24+
static void (*conout_hook_ptr)(const char *s, size_t len);
1525

1626
// used for exit code callback in JS
1727

@@ -95,15 +105,47 @@ extern "C" {
95105
}
96106
}
97107

98-
static void __exit_hook(int status) {
108+
static inline void __exit_hook(int status) {
99109
EM_ASM({
100110
Module['mbb_main_cb']($0);
101111
}, status);
102112
}
103113

114+
static inline bool __check_do_sleep(const char *s, size_t len) {
115+
116+
return !!memchr(s, '\n', len);
117+
}
118+
119+
static void __conout_hook_fast(const char *s, size_t len) {
120+
if (!__check_do_sleep(s, len))
121+
return;
122+
123+
if (++mbb_conio_cnt >= MBB_CONIO_LOWER_THRESHOLD) {
124+
mbb_conio_cnt = 0;
125+
emscripten_sleep(MBB_CONIO_LOWEST_DELAY);
126+
}
127+
}
128+
129+
static void __conout_hook_dflt(const char *s, size_t len) {
130+
if (!__check_do_sleep(s, len))
131+
return;
132+
133+
if (++mbb_conio_cnt >= MBB_CONIO_BASE_THRESHOLD) {
134+
mbb_conio_cnt = 0;
135+
conout_hook_ptr = __conout_hook_fast;
136+
return;
137+
}
138+
139+
emscripten_sleep(MBB_CONIO_BASE_DELAY);
140+
}
141+
104142
static int __main_wrap(int argc, char **argv) {
105143
int res;
106144

145+
// reset conout hook state
146+
conout_hook_ptr = __conout_hook_dflt;
147+
mbb_conio_cnt = 0;
148+
107149
res = __mbb_main(argc, argv);
108150

109151
// asyncify breaks retval, so we do this to inform js side
@@ -117,29 +159,11 @@ __attribute__((noreturn)) static void __exit_wrap(int status) {
117159
__real_exit(status);
118160
}
119161

120-
static void __conout_hook(const char *s, size_t len) {
121-
// when should we update UI?
122-
123-
if (len >= MBB_CONOUT_THRESHOLD)
124-
goto do_sleep; // when the line is long enough
125-
126-
if (memchr(s, '\n', len) || memchr(s, '\r', len))
127-
goto do_sleep; // when there is a newline char in the str
128-
129-
return;
130-
131-
do_sleep:
132-
// sleeping is even more expensive to perf
133-
// so only do this when needed
134-
emscripten_sleep(MBB_CONIO_DELAY); // note this needs -sASYNCIFY in LDFLAGS
135-
}
136-
137-
static void __check_do_conout_hook(int fd, const char *s, size_t len) {
162+
static inline void __check_do_conout_hook(int fd, const char *s, size_t len) {
138163
switch (fd) {
139164
case STDOUT_FILENO:
140165
case STDERR_FILENO:
141-
// give browser a chance to update UI
142-
__conout_hook(s, len);
166+
conout_hook_ptr(s, len);
143167
break;
144168
default:
145169
break;
@@ -162,7 +186,7 @@ static int __vprintf_wrap(const char *format, va_list ap) {
162186
int res;
163187

164188
res = vprintf(format, ap);
165-
__conout_hook(format, strlen(format));
189+
conout_hook_ptr(format, strlen(format));
166190

167191
return res;
168192
}
@@ -193,8 +217,3 @@ static void __enable_conio_hack(void) {
193217
EMSCRIPTEN_BINDINGS(conio_hack) {
194218
emscripten::function("mbb_enable_conio_hack", &__enable_conio_hack);
195219
}
196-
197-
__attribute__((constructor)) static void __init_io_mode(void) {
198-
// disable line buffering
199-
setvbuf(stdout, NULL, _IONBF, BUFSIZ);
200-
}

0 commit comments

Comments
 (0)