From 3372e17d51b6ecab1695de9e41d26a9216a62a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Sun, 17 Mar 2024 12:56:58 +0100 Subject: [PATCH] win32: optimize mp_vfprintf a little - remove redundant strlen/wcslen - reuse allocated temporary buffers The difference is not big, but it satisfies me to remove those redundancies. --- osdep/io.c | 18 +++--------------- osdep/terminal-dummy.c | 5 ++++- osdep/terminal-win.c | 30 +++++++++++++++++++++++++----- osdep/terminal.h | 6 ++++-- test/test_utils.c | 2 +- 5 files changed, 37 insertions(+), 24 deletions(-) diff --git a/osdep/io.c b/osdep/io.c index 1d99e4d207..0006bb54ab 100644 --- a/osdep/io.c +++ b/osdep/io.c @@ -307,8 +307,6 @@ static int mp_vfprintf(FILE *stream, const char *format, va_list args) static int mp_vfprintf(FILE *stream, const char *format, va_list args) { - int done = 0; - HANDLE wstream = INVALID_HANDLE_VALUE; if (stream == stdout || stream == stderr) { @@ -316,20 +314,10 @@ static int mp_vfprintf(FILE *stream, const char *format, va_list args) STD_OUTPUT_HANDLE : STD_ERROR_HANDLE); } - if (mp_check_console(wstream)) { - size_t len = vsnprintf(NULL, 0, format, args) + 1; - char *buf = talloc_array(NULL, char, len); + if (mp_check_console(wstream)) + return mp_write_console_ansi(wstream, format, args); - if (buf) { - done = vsnprintf(buf, len, format, args); - mp_write_console_ansi(wstream, buf); - } - talloc_free(buf); - } else { - done = vfprintf(stream, format, args); - } - - return done; + return vfprintf(stream, format, args); } #endif diff --git a/osdep/terminal-dummy.c b/osdep/terminal-dummy.c index 4b33d786a8..0c861740e5 100644 --- a/osdep/terminal-dummy.c +++ b/osdep/terminal-dummy.c @@ -1,5 +1,7 @@ #include "terminal.h" +#include "misc/bstr.h" + void terminal_init(void) { } @@ -25,8 +27,9 @@ void terminal_get_size2(int *rows, int *cols, int *px_width, int *px_height) { } -void mp_write_console_ansi(void *wstream, char *buf) +int mp_write_console_ansi(void *wstream, const char *format, va_list args) { + return 0; } bool terminal_try_attach(void) diff --git a/osdep/terminal-win.c b/osdep/terminal-win.c index d4647faefd..08aa878906 100644 --- a/osdep/terminal-win.c +++ b/osdep/terminal-win.c @@ -204,6 +204,12 @@ void terminal_setup_getch(struct input_ctx *ictx) } } +DWORD tmp_buffers_key = FLS_OUT_OF_INDEXES; +struct tmp_buffers { + bstr write_console_buf; + wchar_t *write_console_wbuf; +}; + void terminal_uninit(void) { if (running) { @@ -212,6 +218,7 @@ void terminal_uninit(void) input_ctx = NULL; running = false; } + FlsFree(tmp_buffers_key); } bool terminal_in_background(void) @@ -219,14 +226,20 @@ bool terminal_in_background(void) return false; } -void mp_write_console_ansi(HANDLE wstream, char *buf) +int mp_write_console_ansi(HANDLE wstream, const char *format, va_list args) { - wchar_t *wbuf = mp_from_utf8(NULL, buf); - wchar_t *pos = wbuf; + struct tmp_buffers *buffers = FlsGetValue(tmp_buffers_key); + if (!buffers) + buffers = talloc_zero(NULL, struct tmp_buffers); + + buffers->write_console_buf.len = 0; + bstr_xappend_vasprintf(buffers, &buffers->write_console_buf, format, args); + int wlen = bstr_to_wchar(buffers, buffers->write_console_buf, &buffers->write_console_wbuf); + wchar_t *pos = buffers->write_console_wbuf; while (*pos) { if (is_native_out_vt(wstream)) { - WriteConsoleW(wstream, pos, wcslen(pos), NULL, NULL); + WriteConsoleW(wstream, pos, wlen, NULL, NULL); break; } wchar_t *next = wcschr(pos, '\033'); @@ -365,7 +378,12 @@ void mp_write_console_ansi(HANDLE wstream, char *buf) pos = next; } - talloc_free(wbuf); + int ret = buffers->write_console_buf.len; + + if (!FlsSetValue(tmp_buffers_key, buffers)) + talloc_free(buffers); + + return ret; } static bool is_a_console(HANDLE h) @@ -455,4 +473,6 @@ void terminal_init(void) GetConsoleScreenBufferInfo(hSTDOUT, &cinfo); stdoutAttrs = cinfo.wAttributes; + + tmp_buffers_key = FlsAlloc((PFLS_CALLBACK_FUNCTION)talloc_free); } diff --git a/osdep/terminal.h b/osdep/terminal.h index 5cc516fe29..d3d0daa5db 100644 --- a/osdep/terminal.h +++ b/osdep/terminal.h @@ -20,8 +20,10 @@ #ifndef MPLAYER_GETCH2_H #define MPLAYER_GETCH2_H +#include #include -#include + +#include "misc/bstr.h" #define TERM_ESC_GOTO_YX "\033[%d;%df" #define TERM_ESC_HIDE_CURSOR "\033[?25l" @@ -52,7 +54,7 @@ void terminal_get_size(int *w, int *h); void terminal_get_size2(int *rows, int *cols, int *px_width, int *px_height); // Windows only. -void mp_write_console_ansi(void *wstream, char *buf); +int mp_write_console_ansi(void *wstream, const char *format, va_list args); bool mp_check_console(void *handle); /* Windows-only function to attach to the parent process's console */ diff --git a/test/test_utils.c b/test/test_utils.c index 7e5f2e6ce7..1673ef97ba 100644 --- a/test/test_utils.c +++ b/test/test_utils.c @@ -107,7 +107,7 @@ void mp_msg(struct mp_log *log, int lev, const char *format, ...) {}; int mp_msg_find_level(const char *s) {return 0;}; int mp_msg_level(struct mp_log *log) {return 0;}; void mp_msg_set_max_level(struct mp_log *log, int lev) {}; -void mp_write_console_ansi(void *wstream, char *buf) {}; +int mp_write_console_ansi(void *wstream, const char *format, va_list args) {return 0;}; bool mp_check_console(void *handle) { return false; }; void mp_set_avdict(AVDictionary **dict, char **kv) {}; struct mp_log *mp_log_new(void *talloc_ctx, struct mp_log *parent,