From 1e52d33a3696016a7338d08c1c2bf6ef8f324e75 Mon Sep 17 00:00:00 2001 From: Randy Palamar Date: Sat, 4 Jan 2025 21:04:22 -0700 Subject: [PATCH] remove duplicated read_buffer functions When you take a pointer to a function in C that function is going to appear in full in the final binary. This means that there were 3 sections of the final binary with the exact same code. You could argue that in very high performance programs having that function closer to the current instruction when it is needed will give a performance boost but there are so many other places to gain more significant speed ups in vis before that would be remotely relevant. In fact, removing these allows the buffer_append call to inlined so that buffer_insert can be hopped to directly instead of including a useless hop in the middle. --- buffer.c | 5 +++++ buffer.h | 3 +++ sam.c | 11 ++++------- vis-cmds.c | 2 +- vis-registers.c | 9 ++------- vis.c | 9 ++------- 6 files changed, 17 insertions(+), 22 deletions(-) diff --git a/buffer.c b/buffer.c index ffda00fc0..e9285fe4f 100644 --- a/buffer.c +++ b/buffer.c @@ -182,3 +182,8 @@ char *buffer_move(Buffer *buf) { buffer_init(buf); return data; } + +ssize_t read_into_buffer(void *context, char *data, size_t len) { + buffer_append(context, data, len); + return len; +} diff --git a/buffer.h b/buffer.h index 321d54aad..d9e2b8566 100644 --- a/buffer.h +++ b/buffer.h @@ -80,4 +80,7 @@ const char *buffer_content(Buffer*); */ char *buffer_move(Buffer*); +/** ``read(3p)`` like interface for reading into a Buffer (``context``) */ +ssize_t read_into_buffer(void *context, char *data, size_t len); + #endif diff --git a/sam.c b/sam.c index 90d19c297..e5aead3b0 100644 --- a/sam.c +++ b/sam.c @@ -1743,11 +1743,6 @@ static bool cmd_write(Vis *vis, Win *win, Command *cmd, const char *argv[], Sele return true; } -static ssize_t read_buffer(void *context, char *data, size_t len) { - buffer_append(context, data, len); - return len; -} - static bool cmd_filter(Vis *vis, Win *win, Command *cmd, const char *argv[], Selection *sel, Filerange *range) { if (!win) return false; @@ -1756,7 +1751,8 @@ static bool cmd_filter(Vis *vis, Win *win, Command *cmd, const char *argv[], Sel buffer_init(&bufout); buffer_init(&buferr); - int status = vis_pipe(vis, win->file, range, &argv[1], &bufout, read_buffer, &buferr, read_buffer, false); + int status = vis_pipe(vis, win->file, range, &argv[1], &bufout, read_into_buffer, &buferr, + read_into_buffer, false); if (vis->interrupted) { vis_info_show(vis, "Command cancelled"); @@ -1796,7 +1792,8 @@ static bool cmd_pipeout(Vis *vis, Win *win, Command *cmd, const char *argv[], Se Buffer buferr; buffer_init(&buferr); - int status = vis_pipe(vis, win->file, range, (const char*[]){ argv[1], NULL }, NULL, NULL, &buferr, read_buffer, false); + int status = vis_pipe(vis, win->file, range, (const char*[]){ argv[1], NULL }, NULL, NULL, + &buferr, read_into_buffer, false); if (vis->interrupted) vis_info_show(vis, "Command cancelled"); diff --git a/vis-cmds.c b/vis-cmds.c index 4cfb3bd5d..b195013ac 100644 --- a/vis-cmds.c +++ b/vis-cmds.c @@ -413,7 +413,7 @@ static const char *file_open_dialog(Vis *vis, const char *pattern) { Filerange empty = text_range_new(0,0); int status = vis_pipe(vis, vis->win->file, &empty, (const char*[]){ buffer_content0(&bufcmd), NULL }, - &bufout, read_buffer, &buferr, read_buffer, false); + &bufout, read_into_buffer, &buferr, read_into_buffer, false); if (status == 0) strncpy(name, buffer_content0(&bufout), sizeof(name)-1); diff --git a/vis-registers.c b/vis-registers.c index 6e21a5840..caecbd308 100644 --- a/vis-registers.c +++ b/vis-registers.c @@ -21,11 +21,6 @@ static Buffer *register_buffer(Register *reg, size_t slot) { return array_get(®->values, slot); } -static ssize_t read_buffer(void *context, char *data, size_t len) { - buffer_append(context, data, len); - return len; -} - bool register_init(Register *reg) { Buffer buf; buffer_init(&buf); @@ -83,7 +78,7 @@ const char *register_slot_get(Vis *vis, Register *reg, size_t slot, size_t *len) cmd[3] = "clipboard"; int status = vis_pipe(vis, vis->win->file, &(Filerange){ .start = 0, .end = 0 }, - cmd, buf, read_buffer, &buferr, read_buffer, false); + cmd, buf, read_into_buffer, &buferr, read_into_buffer, false); if (status != 0) vis_info_show(vis, "Command failed %s", buffer_content0(&buferr)); @@ -167,7 +162,7 @@ bool register_slot_put_range(Vis *vis, Register *reg, size_t slot, Text *txt, Fi cmd[3] = "clipboard"; int status = vis_pipe(vis, vis->win->file, range, - cmd, NULL, NULL, &buferr, read_buffer, false); + cmd, NULL, NULL, &buferr, read_into_buffer, false); if (status != 0) vis_info_show(vis, "Command failed %s", buffer_content0(&buferr)); diff --git a/vis.c b/vis.c index d1c0dab08..c059a482c 100644 --- a/vis.c +++ b/vis.c @@ -1854,18 +1854,13 @@ int vis_pipe_buf(Vis *vis, const char* buf, const char *argv[], return _vis_pipe(vis, NULL, NULL, buf, argv, stdout_context, read_stdout, stderr_context, read_stderr, fullscreen); } -static ssize_t read_buffer(void *context, char *data, size_t len) { - buffer_append(context, data, len); - return len; -} - static int _vis_pipe_collect(Vis *vis, File *file, Filerange *range, const char* buf, const char *argv[], char **out, char **err, bool fullscreen) { Buffer bufout, buferr; buffer_init(&bufout); buffer_init(&buferr); int status = _vis_pipe(vis, file, range, buf, argv, - &bufout, out ? read_buffer : NULL, - &buferr, err ? read_buffer : NULL, + &bufout, out ? read_into_buffer : NULL, + &buferr, err ? read_into_buffer : NULL, fullscreen); buffer_terminate(&bufout); buffer_terminate(&buferr);