From a271d45efe4aa04ed04467caa9bfdac7305699ac Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Sun, 9 Nov 2008 21:04:45 +0100 Subject: [PATCH] IO API: use select(2) to check if pipe is readable when updating a view --- NEWS | 1 + tig.c | 35 +++++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/NEWS b/NEWS index 0463230..31ef9d5 100644 --- a/NEWS +++ b/NEWS @@ -18,6 +18,7 @@ Improvements: - Add bash completion for blame. - Tree view: edit files of the current branch. - Run requests: new identifiers %(directory), %(file), and %(ref) + - Improve responsiveness and view loading speed by using select(2). Bug fixes: diff --git a/tig.c b/tig.c index 36c32cd..544fd85 100644 --- a/tig.c +++ b/tig.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -509,6 +510,18 @@ io_strerror(struct io *io) return strerror(io->error); } +static bool +io_can_read(struct io *io) +{ + struct timeval tv = { 0, 500 }; + fd_set fds; + + FD_ZERO(&fds); + FD_SET(io->pipe, &fds); + + return select(io->pipe + 1, &fds, NULL, NULL, &tv) > 0; +} + static ssize_t io_read(struct io *io, void *buf, size_t bufsize) { @@ -2666,24 +2679,20 @@ update_view(struct view *view) { char out_buffer[BUFSIZ * 2]; char *line; - /* The number of lines to read. If too low it will cause too much - * redrawing (and possible flickering), if too high responsiveness - * will suffer. */ - unsigned long lines = view->height; int redraw_from = -1; + bool can_read = TRUE; if (!view->pipe) return TRUE; + if (!io_can_read(view->pipe)) + return TRUE; + /* Only redraw if lines are visible. */ if (view->offset + view->height >= view->lines) redraw_from = view->lines - view->offset; - /* FIXME: This is probably not perfect for backgrounded views. */ - if (!realloc_lines(view, view->lines + lines)) - goto alloc_error; - - while ((line = io_get(view->pipe, '\n', TRUE))) { + for (; (line = io_get(view->pipe, '\n', can_read)); can_read = FALSE) { size_t linelen = strlen(line); if (opt_iconv != ICONV_NONE) { @@ -2702,17 +2711,15 @@ update_view(struct view *view) } } - if (!view->ops->read(view, line)) + if (!realloc_lines(view, view->lines + 1) || + !view->ops->read(view, line)) goto alloc_error; - - if (lines-- == 1) - break; } { + unsigned long lines = view->lines; int digits; - lines = view->lines; for (digits = 0; lines; digits++) lines /= 10; -- 2.32.0.93.g670b81a890