- New code capability: a read-only string cache. Start of solution for
[make.git] / strcache.c
blobb20a511999675da4804fbc7727094dcd781a7034
1 /* Constant string caching for GNU Make.
2 Copyright (C) 2006 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 Boston, MA 02110-1301 USA. */
20 #include "make.h"
22 #include <assert.h>
24 #include "hash.h"
26 /* The size (in bytes) of each cache buffer. */
27 #define CACHE_BUFFER_SIZE (4096)
30 /* A string cached here will never be freed, so we don't need to worry about
31 reference counting. We just store the string, and then remember it in a
32 hash so it can be looked up again. */
34 struct strcache {
35 struct strcache *next; /* The next block of strings. */
36 char *end; /* Pointer to the beginning of the free space. */
37 int count; /* # of strings in this buffer (for stats). */
38 int bytesfree; /* The amount of the buffer that is free. */
39 char buffer[1]; /* The buffer comes after this. */
42 static int bufsize = CACHE_BUFFER_SIZE;
43 static struct strcache *strcache = NULL;
45 static struct strcache *
46 new_cache()
48 struct strcache *new;
49 new = (struct strcache *) xmalloc (sizeof (*new) + bufsize);
50 new->end = new->buffer;
51 new->count = 0;
52 new->bytesfree = bufsize;
54 new->next = strcache;
55 strcache = new;
57 return new;
60 static const char *
61 add_string(const char *str, int len)
63 struct strcache *best = NULL;
64 struct strcache *sp;
65 const char *res;
67 /* If the string we want is too large to fit into a single buffer, then we're
68 screwed; nothing will ever fit! Change the maximum size of the cache to
69 be big enough. */
70 if (len > bufsize)
71 bufsize = len * 2;
73 /* First, find a cache with enough free space. We always look through all
74 the blocks and choose the one with the best fit (the one that leaves the
75 least amount of space free). */
76 for (sp = strcache; sp != NULL; sp = sp->next)
77 if (sp->bytesfree > len && (!best || best->bytesfree > sp->bytesfree))
78 best = sp;
80 /* If nothing is big enough, make a new cache. */
81 if (!best)
82 best = new_cache();
84 assert (best->bytesfree > len);
86 /* Add the string to the best cache. */
87 res = best->end;
88 memcpy (best->end, str, len);
89 best->end += len;
90 *(best->end++) = '\0';
91 best->bytesfree -= len + 1;
92 ++best->count;
94 return res;
98 /* Hash table of strings in the cache. */
100 static unsigned long
101 str_hash_1 (const void *key)
103 return_ISTRING_HASH_1 ((const char *) key);
106 static unsigned long
107 str_hash_2 (const void *key)
109 return_ISTRING_HASH_2 ((const char *) key);
112 static int
113 str_hash_cmp (const void *x, const void *y)
115 return_ISTRING_COMPARE ((const char *) x, (const char *) y);
118 static struct hash_table strings;
120 static const char *
121 add_hash (const char *str, int len)
123 /* Look up the string in the hash. If it's there, return it. */
124 char **slot = (char **) hash_find_slot (&strings, str);
125 const char *key = *slot;
127 if (!HASH_VACANT (key))
128 return key;
130 /* Not there yet so add it to a buffer, then into the hash table. */
131 key = add_string (str, len);
132 hash_insert_at (&strings, key, slot);
133 return key;
136 /* Returns true if the string is in the cache; false if not. */
138 strcache_iscached (const char *str)
140 struct strcache *sp;
142 for (sp = strcache; sp != 0; sp = sp->next)
143 if (str >= sp->buffer && str < sp->end)
144 return 1;
146 return 0;
149 /* If the string is already in the cache, return a pointer to the cached
150 version. If not, add it then return a pointer to the cached version.
151 Note we do NOT take control of the string passed in. */
152 const char *
153 strcache_add (const char *str)
155 return add_hash (str, strlen (str));
158 const char *
159 strcache_add_len (const char *str, int len)
161 char *key = alloca (len + 1);
162 memcpy (key, str, len);
163 key[len] = '\0';
165 return add_hash (key, len);
169 strcache_setbufsize(int size)
171 if (size > bufsize)
172 bufsize = size;
173 return bufsize;
176 void
177 strcache_init (void)
179 hash_init (&strings, 1000, str_hash_1, str_hash_2, str_hash_cmp);
183 /* Generate some stats output. */
185 void
186 strcache_print_stats (const char *prefix)
188 int numbuffs = 0, numstrs = 0;
189 int totsize = 0, avgsize, maxsize = 0, minsize = bufsize;
190 int totfree = 0, avgfree, maxfree = 0, minfree = bufsize;
191 const struct strcache *sp;
193 for (sp = strcache; sp != NULL; sp = sp->next)
195 int bf = sp->bytesfree;
196 int sz = (sp->end - sp->buffer) + bf;
198 ++numbuffs;
199 numstrs += sp->count;
201 totsize += sz;
202 maxsize = (sz > maxsize ? sz : maxsize);
203 minsize = (sz < minsize ? sz : minsize);
205 totfree += bf;
206 maxfree = (bf > maxfree ? bf : maxfree);
207 minfree = (bf < minfree ? bf : minfree);
210 avgsize = numbuffs ? (int)(totsize / numbuffs) : 0;
211 avgfree = numbuffs ? (int)(totfree / numbuffs) : 0;
213 printf ("\n%s # of strings in strcache: %d\n", prefix, numstrs);
214 printf ("%s # of strcache buffers: %d\n", prefix, numbuffs);
215 printf ("%s strcache size: total = %d / max = %d / min = %d / avg = %d\n",
216 prefix, totsize, maxsize, minsize, avgsize);
217 printf ("%s strcache free: total = %d / max = %d / min = %d / avg = %d\n",
218 prefix, totfree, maxfree, minfree, avgfree);