1 /* Constant string caching for GNU Make.
2 Copyright (C) 2006, 2007 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 it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
18 #ifndef CONFIG_WITH_STRCACHE2
24 /* The size (in bytes) of each cache buffer.
25 Try to pick something that will map well into the heap. */
26 #define CACHE_BUFFER_SIZE (8192 - 16)
29 /* A string cached here will never be freed, so we don't need to worry about
30 reference counting. We just store the string, and then remember it in a
31 hash so it can be looked up again. */
34 struct strcache
*next
; /* The next block of strings. */
35 char *end
; /* Pointer to the beginning of the free space. */
36 int count
; /* # of strings in this buffer (for stats). */
37 int bytesfree
; /* The amount of the buffer that is free. */
38 char buffer
[1]; /* The buffer comes after this. */
41 static int bufsize
= CACHE_BUFFER_SIZE
;
42 static struct strcache
*strcache
= NULL
;
44 /* Add a new buffer to the cache. Add it at the front to reduce search time.
45 This can also increase the overhead, since it's less likely that older
46 buffers will be filled in. However, GNU make has so many smaller strings
47 that this doesn't seem to be much of an issue in practice.
49 static struct strcache
*
53 new = xmalloc (sizeof (*new) + bufsize
);
54 new->end
= new->buffer
;
56 new->bytesfree
= bufsize
;
65 add_string(const char *str
, int len
)
67 struct strcache
*best
= NULL
;
71 /* If the string we want is too large to fit into a single buffer, then
72 we're screwed; nothing will ever fit! Change the maximum size of the
73 cache to be big enough. */
77 /* First, find a cache with enough free space. We always look through all
78 the blocks and choose the one with the best fit (the one that leaves the
79 least amount of space free). */
80 for (sp
= strcache
; sp
!= NULL
; sp
= sp
->next
)
81 if (sp
->bytesfree
> len
&& (!best
|| best
->bytesfree
> sp
->bytesfree
))
84 /* If nothing is big enough, make a new cache. */
88 assert (best
->bytesfree
> len
);
90 /* Add the string to the best cache. */
92 memcpy (best
->end
, str
, len
);
94 *(best
->end
++) = '\0';
95 best
->bytesfree
-= len
+ 1;
102 /* Hash table of strings in the cache. */
105 str_hash_1 (const void *key
)
107 return_ISTRING_HASH_1 ((const char *) key
);
111 str_hash_2 (const void *key
)
113 return_ISTRING_HASH_2 ((const char *) key
);
117 str_hash_cmp (const void *x
, const void *y
)
119 return_ISTRING_COMPARE ((const char *) x
, (const char *) y
);
122 static struct hash_table strings
;
123 static unsigned long total_adds
= 0;
126 add_hash (const char *str
, int len
)
128 /* Look up the string in the hash. If it's there, return it. */
129 char *const *slot
= (char *const *) hash_find_slot (&strings
, str
);
130 const char *key
= *slot
;
132 /* Count the total number of adds we performed. */
135 if (!HASH_VACANT (key
))
138 /* Not there yet so add it to a buffer, then into the hash table. */
139 key
= add_string (str
, len
);
140 hash_insert_at (&strings
, key
, slot
);
144 /* Returns true if the string is in the cache; false if not. */
146 strcache_iscached (const char *str
)
150 for (sp
= strcache
; sp
!= 0; sp
= sp
->next
)
151 if (str
>= sp
->buffer
&& str
< sp
->end
)
157 /* If the string is already in the cache, return a pointer to the cached
158 version. If not, add it then return a pointer to the cached version.
159 Note we do NOT take control of the string passed in. */
161 strcache_add (const char *str
)
163 return add_hash (str
, strlen (str
));
167 strcache_add_len (const char *str
, int len
)
169 /* If we're not given a nul-terminated string we have to create one, because
170 the hashing functions expect it. */
171 if (str
[len
] != '\0')
173 char *key
= alloca (len
+ 1);
174 memcpy (key
, str
, len
);
179 return add_hash (str
, len
);
183 strcache_setbufsize(int size
)
193 hash_init (&strings
, 8000, str_hash_1
, str_hash_2
, str_hash_cmp
);
197 /* Generate some stats output. */
200 strcache_print_stats (const char *prefix
)
202 int numbuffs
= 0, numstrs
= 0;
203 int totsize
= 0, avgsize
, maxsize
= 0, minsize
= bufsize
;
204 int totfree
= 0, avgfree
, maxfree
= 0, minfree
= bufsize
;
205 int lastused
= 0, lastfree
= 0;
209 const struct strcache
*sp
;
211 /* Count the first buffer separately since it's not full. */
212 lastused
= strcache
->end
- strcache
->buffer
;
213 lastfree
= strcache
->bytesfree
;
215 for (sp
= strcache
->next
; sp
!= NULL
; sp
= sp
->next
)
217 int bf
= sp
->bytesfree
;
218 int sz
= sp
->end
- sp
->buffer
;
221 numstrs
+= sp
->count
;
224 maxsize
= (sz
> maxsize
? sz
: maxsize
);
225 minsize
= (sz
< minsize
? sz
: minsize
);
228 maxfree
= (bf
> maxfree
? bf
: maxfree
);
229 minfree
= (bf
< minfree
? bf
: minfree
);
233 avgsize
= numbuffs
? (int)(totsize
/ numbuffs
) : 0;
234 avgfree
= numbuffs
? (int)(totfree
/ numbuffs
) : 0;
236 printf (_("\n%s # of strings in strcache: %d / lookups = %lu / hits = %lu\n"),
237 prefix
, numstrs
, total_adds
, (total_adds
- numstrs
));
238 printf (_("%s # of strcache buffers: %d (* %d B/buffer = %d B)\n"),
239 prefix
, (numbuffs
+ 1), bufsize
, ((numbuffs
+ 1) * bufsize
));
240 printf (_("%s strcache used: total = %d (%d) / max = %d / min = %d / avg = %d\n"),
241 prefix
, totsize
, lastused
, maxsize
, minsize
, avgsize
);
242 printf (_("%s strcache free: total = %d (%d) / max = %d / min = %d / avg = %d\n"),
243 prefix
, totfree
, lastfree
, maxfree
, minfree
, avgfree
);
245 fputs (_("\n# strcache hash-table stats:\n# "), stdout
);
246 hash_print_stats (&strings
, stdout
);
249 #else /* CONFIG_WITH_STRCACHE2 */
251 #include "strcache2.h"
253 const char *suffixes_strcached
;
255 /* The file string cache. */
256 struct strcache2 file_strcache
;
258 void strcache_init (void)
260 strcache2_init(&file_strcache
,
262 65536, /* hash size */
263 0, /* default segment size*/
264 #ifdef HAVE_CASE_INSENSITIVE_FS
265 1, /* case insensitive */
267 0, /* case insensitive */
269 0); /* thread safe */
271 /* .SUFFIXES is referenced in several loops, keep the added pointer in a
272 global var so these can be optimized. */
274 suffixes_strcached
= strcache_add_len (".SUFFIXES", sizeof (".SUFFIXES")-1);
278 strcache_print_stats (const char *prefix
)
280 strcache2_print_stats (&file_strcache
, prefix
);
283 #endif /* CONFIG_WITH_STRCACHE2 */