annotate src/filecache.c @ 873:bd3bdceb1230

Optimize file_cache_get() by only moving element to front if needed (most of the time there is no need). Reduce code redundancy in file_cache_put() by calling file_cache_get(). Enhance debugging code.
author zas_
date Wed, 02 Jul 2008 08:38:47 +0000
parents 0c3f6ef17d18
children d8e1e820cee7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
1 /*
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
2 * Geeqie
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
3 * Copyright (C) 2008 The Geeqie Team
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
4 *
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
5 * Author: Vladimir Nadvornik
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
6 *
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
7 * This software is released under the GNU General Public License (GNU GPL).
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
8 * Please read the included file COPYING for more information.
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
9 * This software comes with no warranty of any kind, use at your own risk!
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
10 */
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
11
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
12
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
13 #include "main.h"
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
14 #include "filecache.h"
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
15
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
16 /* Set to TRUE to add file cache dumps to the debug output */
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
17 const gboolean debug_file_cache = FALSE;
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
18
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
19 /* this implements a simple LRU algorithm */
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
20
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
21 struct _FileCacheData {
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
22 FileCacheReleaseFunc release;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
23 GList *list;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
24 gulong max_size;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
25 gulong size;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
26 };
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
27
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
28 typedef struct _FileCacheEntry FileCacheEntry;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
29 struct _FileCacheEntry {
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
30 FileData *fd;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
31 gulong size;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
32 };
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
33
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
34 FileCacheData *file_cache_new(FileCacheReleaseFunc release, gulong max_size)
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
35 {
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
36 FileCacheData *fc = g_new(FileCacheData, 1);
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
37 fc->release = release;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
38 fc->list = NULL;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
39 fc->max_size = max_size;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
40 fc->size = 0;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
41 return fc;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
42 }
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
43
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
44 gboolean file_cache_get(FileCacheData *fc, FileData *fd)
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
45 {
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
46 GList *work;
855
0c3f6ef17d18 Tidy up.
zas_
parents: 847
diff changeset
47
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
48 g_assert(fc && fd);
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
49
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
50 work = fc->list;
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
51 while (work)
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
52 {
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
53 FileCacheEntry *fce = work->data;
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
54 if (fce->fd == fd)
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
55 {
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
56 /* entry exists */
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
57 DEBUG_1("cache hit: fc=%p %s", fc, fd->path);
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
58 if (work == fc->list) return TRUE; /* already at the beginning */
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
59 /* move it to the beginning */
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
60 DEBUG_1("cache move to front: fc=%p %s", fc, fd->path);
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
61 fc->list = g_list_remove_link(fc->list, work);
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
62 fc->list = g_list_concat(work, fc->list);
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
63 if (debug_file_cache) file_cache_dump(fc);
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
64 return TRUE;
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
65 }
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
66 work = work->next;
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
67 }
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
68 DEBUG_1("cache miss: fc=%p %s", fc, fd->path);
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
69 return FALSE;
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
70 }
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
71
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
72 void file_cache_set_size(FileCacheData *fc, gulong size)
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
73 {
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
74 GList *work;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
75 FileCacheEntry *last_fe;
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
76
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
77 if (debug_file_cache) file_cache_dump(fc);
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
78
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
79 work = g_list_last(fc->list);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
80 while (fc->size > size && work)
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
81 {
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
82 GList *prev;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
83 last_fe = work->data;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
84 prev = work->prev;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
85 fc->list = g_list_delete_link(fc->list, work);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
86 work = prev;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
87
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
88 DEBUG_1("cache remove: fc=%p %s", fc, last_fe->fd->path);
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
89 fc->size -= last_fe->size;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
90 fc->release(last_fe->fd);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
91 file_data_unref(last_fe->fd);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
92 g_free(last_fe);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
93 }
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
94 }
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
95
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
96 void file_cache_put(FileCacheData *fc, FileData *fd, gulong size)
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
97 {
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
98 FileCacheEntry *fe;
855
0c3f6ef17d18 Tidy up.
zas_
parents: 847
diff changeset
99
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
100 if (file_cache_get(fc, fd)) return;
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
101
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
102 DEBUG_1("cache add: fc=%p %s", fc, fd->path);
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
103 fe = g_new(FileCacheEntry, 1);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
104 fe->fd = file_data_ref(fd);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
105 fe->size = size;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
106 fc->list = g_list_prepend(fc->list, fe);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
107 fc->size += size;
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
108
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
109 file_cache_set_size(fc, fc->max_size);
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
110 }
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
111
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
112 gulong file_cache_get_max_size(FileCacheData *fc)
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
113 {
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
114 return fc->max_size;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
115 }
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
116
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
117 gulong file_cache_get_size(FileCacheData *fc)
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
118 {
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
119 return fc->size;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
120 }
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
121
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
122 void file_cache_set_max_size(FileCacheData *fc, gulong size)
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
123 {
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
124 fc->max_size = size;
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
125 file_cache_set_size(fc, fc->max_size);
844
efed9a1520d6 implemented generic FileData cache
nadvornik
parents:
diff changeset
126 }
846
8911a4f0e56c simple cache for loaded pixbufs
nadvornik
parents: 844
diff changeset
127
8911a4f0e56c simple cache for loaded pixbufs
nadvornik
parents: 844
diff changeset
128 void file_cache_dump(FileCacheData *fc)
8911a4f0e56c simple cache for loaded pixbufs
nadvornik
parents: 844
diff changeset
129 {
8911a4f0e56c simple cache for loaded pixbufs
nadvornik
parents: 844
diff changeset
130 GList *work;
8911a4f0e56c simple cache for loaded pixbufs
nadvornik
parents: 844
diff changeset
131 work = fc->list;
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
132 guint n = 0;
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
133 DEBUG_1("cache dump: fc=%p max size:%ld size:%ld", fc, fc->max_size, fc->size);
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
134
855
0c3f6ef17d18 Tidy up.
zas_
parents: 847
diff changeset
135 while (work)
846
8911a4f0e56c simple cache for loaded pixbufs
nadvornik
parents: 844
diff changeset
136 {
847
77fc0ea3457d measure pixbuf cache size in bytes
nadvornik
parents: 846
diff changeset
137 FileCacheEntry *fe = work->data;
846
8911a4f0e56c simple cache for loaded pixbufs
nadvornik
parents: 844
diff changeset
138 work = work->next;
873
bd3bdceb1230 Optimize file_cache_get() by only moving element to front if needed
zas_
parents: 855
diff changeset
139 DEBUG_1("cache entry: fc=%p [%lu] %s %ld", fc, ++n, fe->fd->path, fe->size);
846
8911a4f0e56c simple cache for loaded pixbufs
nadvornik
parents: 844
diff changeset
140 }
8911a4f0e56c simple cache for loaded pixbufs
nadvornik
parents: 844
diff changeset
141 }