Skip to content

Commit 9188e6d

Browse files
committed
Cache key strings calculated during help_open() so that help_draw() does not need to recalculate them
1. help.c a. struct help Add a key field to cache key strings calculated during help_open() so that help_draw() does not need to recalculate. b. help_keys_visitor() Cache the available key string in help.key. c. help_draw(), help_grep() Use cached help.key string instead of recalculating the key string. d. help_open() Free any key strings that have been cached in the help struct to prevent memory leak during a refresh of help screen.
1 parent 687db35 commit 9188e6d

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/help.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct help_state {
2828
struct help {
2929
struct keymap *keymap;
3030
enum request request;
31+
const char *key;
3132
union {
3233
const char *text;
3334
const struct request_info *req_info;
@@ -50,9 +51,8 @@ help_draw(struct view *view, struct line *line, unsigned int lineno)
5051

5152
} else if (help->request > REQ_RUN_REQUESTS) {
5253
struct run_request *req = get_run_request(help->request);
53-
const char *key = get_keys(keymap, help->request, true);
5454

55-
if (draw_field(view, LINE_DEFAULT, key, state->keys_width + 2, ALIGN_RIGHT, false))
55+
if (draw_field(view, LINE_DEFAULT, help->key, state->keys_width + 2, ALIGN_RIGHT, false))
5656
return true;
5757

5858
/* If there is req->help text to draw, then first draw req->name as a fixed-width field */
@@ -67,9 +67,8 @@ help_draw(struct view *view, struct line *line, unsigned int lineno)
6767

6868
} else {
6969
const struct request_info *req_info = help->data.req_info;
70-
const char *key = get_keys(keymap, req_info->request, true);
7170

72-
if (draw_field(view, LINE_DEFAULT, key, state->keys_width + 2, ALIGN_RIGHT, false))
71+
if (draw_field(view, LINE_DEFAULT, help->key, state->keys_width + 2, ALIGN_RIGHT, false))
7372
return true;
7473

7574
/* If there is req_info->help text to draw, then first draw req_info->name as a fixed-width field */
@@ -104,16 +103,13 @@ help_grep(struct view *view, struct line *line)
104103

105104
} else if (help->request > REQ_RUN_REQUESTS) {
106105
struct run_request *req = get_run_request(help->request);
107-
const char *key = get_keys(keymap, help->request, true);
108-
109-
const char *text[] = { key, req->name, req->help, NULL };
106+
const char *text[] = { help->key, req->name, req->help, NULL };
110107

111108
return grep_text(view, text);
112109

113110
} else {
114111
const struct request_info *req_info = help->data.req_info;
115-
const char *key = get_keys(keymap, req_info->request, true);
116-
const char *text[] = { key, enum_name(req_info->name), req_info->help, NULL };
112+
const char *text[] = { help->key, enum_name(req_info->name), req_info->help, NULL };
117113

118114
return grep_text(view, text);
119115
}
@@ -165,7 +161,11 @@ help_keys_visitor(void *data, const char *group, struct keymap *keymap,
165161
if (!add_help_line(view, &help, keymap, LINE_DEFAULT))
166162
return false;
167163

168-
state->keys_width = MAX(state->keys_width, strlen(key));
164+
int len = strlen(key);
165+
state->keys_width = MAX(state->keys_width, len);
166+
/* Since the key string is available, cache it for when help lines need to be drawn */
167+
help->key = len > 0 ? strdup(key) : NULL;
168+
169169
help->request = request;
170170

171171
if (req_info) {
@@ -190,6 +190,12 @@ help_open(struct view *view, enum open_flags flags)
190190
struct help_request_iterator iterator = { view };
191191
struct help *help;
192192

193+
/* Need to free any key strings that have been cached */
194+
int i; for (i = 0; i < view->lines; i++) {
195+
const struct help *h = view->line[i].data;
196+
free((void *)h->key);
197+
}
198+
193199
reset_view(view);
194200

195201
if (!add_help_line(view, &help, NULL, LINE_HEADER))

0 commit comments

Comments
 (0)