[Mesa-dev] [PATCH 1/3] util/disc_cache: use LRU eviction rather than random eviction
Grazvydas Ignotas
notasas at gmail.com
Mon Mar 6 23:15:10 UTC 2017
On Mon, Mar 6, 2017 at 6:17 PM, Alan Swanson <reiver at improbability.net> wrote:
> Still using random selection of two-character subdirectory in which
> to check cache files rather than scanning entire cache.
> ---
> src/util/disk_cache.c | 77 +++++++++++++++++++++++----------------------------
> 1 file changed, 34 insertions(+), 43 deletions(-)
>
> diff --git a/src/util/disk_cache.c b/src/util/disk_cache.c
> index 31a9336582..28f8ea4054 100644
> --- a/src/util/disk_cache.c
> +++ b/src/util/disk_cache.c
> @@ -438,30 +438,30 @@ make_cache_file_directory(struct disk_cache *cache, const cache_key key)
> free(dir);
> }
>
> -/* Given a directory path and predicate function, count all entries in
> - * that directory for which the predicate returns true. Then choose a
> - * random entry from among those counted.
> +/* Given a directory path and predicate function, find the entry with
> + * the oldest access time in that directory for which the predicate
> + * returns true.
> *
> * Returns: A malloc'ed string for the path to the chosen file, (or
> * NULL on any error). The caller should free the string when
> * finished.
> */
> static char *
> -choose_random_file_matching(const char *dir_path,
> - bool (*predicate)(const struct dirent *,
> - const char *dir_path))
> +choose_lru_file_matching(const char *dir_path,
> + bool (*predicate)(const struct dirent *,
> + const char *dir_path))
> {
> DIR *dir;
> struct dirent *entry;
> - unsigned int count, victim;
> + struct stat sb;
> + char *tmp, *lru_name = NULL;
> + time_t lru_atime = 0;
> char *filename;
>
> dir = opendir(dir_path);
> if (dir == NULL)
> return NULL;
>
> - count = 0;
> -
> while (1) {
> entry = readdir(dir);
> if (entry == NULL)
> @@ -469,39 +469,28 @@ choose_random_file_matching(const char *dir_path,
> if (!predicate(entry, dir_path))
> continue;
>
> - count++;
> - }
> -
> - if (count == 0) {
> - closedir(dir);
> - return NULL;
> - }
> -
> - victim = rand() % count;
> -
> - rewinddir(dir);
> - count = 0;
> -
> - while (1) {
> - entry = readdir(dir);
> - if (entry == NULL)
> - break;
> - if (!predicate(entry, dir_path))
> - continue;
> - if (count == victim)
> - break;
> -
> - count++;
> + if (fstatat(dirfd(dir), entry->d_name, &sb, 0) == 0) {
> + if (!lru_atime || (sb.st_atime < lru_atime)) {
> + tmp = realloc(lru_name, strlen(entry->d_name) + 1);
> + if (tmp) {
> + lru_name = tmp;
> + memcpy(lru_name, entry->d_name,
> + strlen(entry->d_name) + 1);
Maybe avoid the double strlen()?
Either way, FWIW:
Reviewed-by: Grazvydas Ignotas <notasas at gmail.com>
GraÅžvydas
More information about the mesa-dev
mailing list