Skip to content
Myr

main: read_cache(): optimize; application list is now sorted

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Since both the application list *and* the cache file are now sorted, we can optimize further, by progressing both lists in lock-step. That is, for each cache line, we can start searching the application list from where we stopped the last time. And we only need to search until we find an application that either matches, or is "past" our cache line.

authored by Daniel Eklöf · committed by Daniel Eklöf · parent d5f93cc · Browse files · 1 file+7 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+7 added−20 removed

  • other:+7 added−20 removed

1 file+7 added−20 removed

modified:main.c+7 added-20 removed
1081 unmodified lines
1082 LOG_ERRNO("%s/f00sel: failed to mmap", path);1083 return;1084 }10851086 struct cache_data {1087 const char *title;1088 unsigned count;1089 };1090 tll(struct cache_data) cache_data = tll_init();1086 size_t app_idx = 0;10871088 /* Loop lines */1089 char *lineptr = NULL;1090 for (char *line = strtok_r(text, "\n", &lineptr);
7 unmodified lines
10981099 int count;1100 sscanf(count_str, "%u", &count);11011106 tll_push_back(1107 cache_data, ((struct cache_data){.title = title, .count = count}));1108 }1102 for (; app_idx < apps->count; app_idx++) {1103 int cmp = strcmp(apps->v[app_idx].title, title);11041110 /* Set 'count' in our application list */1111 for (size_t i = 0; i < apps->count; i++) {1112 struct application *app = &apps->v[i];11131114 /* Note: assumed to be sorted */1115 tll_foreach(cache_data, it) {1116 int cmp = strcmp(it->item.title, app->title);1117 if (cmp > 0)1105 if (cmp == 0) {1106 apps->v[app_idx].count = count;1107 app_idx++;1108 break;1119 else if (cmp == 0) {1120 app->count = it->item.count;1109 } else if (cmp > 0)1110 break;1122 }1111 }1112 }11131126 tll_free(cache_data);1114 munmap(text, st.st_size);1115}11161117static void
456 unmodified lines