From 5537a3f97718c893a1cf082983f215026fe9b7c4 Mon Sep 17 00:00:00 2001 From: Miciah Dashiel Butler Masters Date: Sat, 2 Dec 2006 14:48:48 +0000 Subject: [PATCH] Improve performance with textareas + UTF-8 I/O If utf8_char2cells isn't told where the string that contains the given UTF-8 character ends, it computes that itself. Two users of utf8_char2cells, format_textutf8 and split_line, were calling utf8_char2cells in a loop without providing the end of the string, resulting in numerous calls by utf8_char2cells to strlen. With this patch, format_textutf8 and split_line each find the end of the string once and provide it to utf8_char2cells. This particularly improves performance with textareas, since format_textutf8 is called multiple times each time the user interacts with the textarea and when it must be redrawn. Closes: Bug 823 - Big textarea is too slow with CONFIG_UTF8 --- src/bfu/text.c | 5 +++-- src/viewer/text/textarea.c | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/bfu/text.c b/src/bfu/text.c index 583d09fd0..a205f256c 100644 --- a/src/bfu/text.c +++ b/src/bfu/text.c @@ -45,6 +45,7 @@ split_line(unsigned char *text, int max_width, int *cells) #endif /* CONFIG_UTF8 */ { unsigned char *split = text; + unsigned char *text_end = split + strlen(split); int cells_save = *cells; if (max_width <= 0) return 0; @@ -59,7 +60,7 @@ split_line(unsigned char *text, int max_width, int *cells) next_split = split; - *cells += utf8_char2cells(split, NULL); + *cells += utf8_char2cells(split, text_end); while (*next_split && next_split != next_char_begin) next_split++; @@ -70,7 +71,7 @@ split_line(unsigned char *text, int max_width, int *cells) next_split++; continue; } - *cells += utf8_char2cells(next_split, NULL); + *cells += utf8_char2cells(next_split, text_end); next_char_begin += utf8charlen(next_split); } } else diff --git a/src/viewer/text/textarea.c b/src/viewer/text/textarea.c index 07aa69e9a..dcd389597 100644 --- a/src/viewer/text/textarea.c +++ b/src/viewer/text/textarea.c @@ -67,6 +67,7 @@ format_textutf8(unsigned char *text, int width, enum form_wrap wrap, int format) int line_number = 0; int begin = 0; int pos = 0; + unsigned char *text_end; int skip; unsigned char *wrappos=NULL; int chars_cells=0; /* Number of console chars on line */ @@ -78,8 +79,9 @@ format_textutf8(unsigned char *text, int width, enum form_wrap wrap, int format) if (!realloc_line_info(&line, 0)) return NULL; + text_end = text + strlen(text); while (text[pos]) { - int char_cells = utf8_char2cells(&text[pos], NULL); + int char_cells = utf8_char2cells(&text[pos], text_end); if (text[pos] == ' ') wrappos = &text[pos];