VirtualBox

Changeset 903 in kBuild for trunk/src/gmakenew/strcache.c


Ignore:
Timestamp:
May 23, 2007 5:31:19 AM (18 years ago)
Author:
bird
Message:

Merged with the 2007-05-23 CVS. Added rsort and fixed a couple of windows build issues.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gmakenew/strcache.c

    r503 r903  
    2121#include "hash.h"
    2222
    23 /* The size (in bytes) of each cache buffer.  */
    24 #define CACHE_BUFFER_SIZE   (4096)
     23/* The size (in bytes) of each cache buffer.
     24   Try to pick something that will map well into the heap.  */
     25#define CACHE_BUFFER_SIZE   (8192 - 16)
    2526
    2627
     
    4041static struct strcache *strcache = NULL;
    4142
     43/* Add a new buffer to the cache.  Add it at the front to reduce search time.
     44   This can also increase the overhead, since it's less likely that older
     45   buffers will be filled in.  However, GNU make has so many smaller strings
     46   that this doesn't seem to be much of an issue in practice.
     47 */
    4248static struct strcache *
    4349new_cache()
    4450{
    4551  struct strcache *new;
    46   new = (struct strcache *) xmalloc (sizeof (*new) + bufsize);
     52  new = xmalloc (sizeof (*new) + bufsize);
    4753  new->end = new->buffer;
    4854  new->count = 0;
     
    6268  const char *res;
    6369
    64   /* If the string we want is too large to fit into a single buffer, then we're
    65      screwed; nothing will ever fit!  Change the maximum size of the cache to
    66      be big enough.  */
     70  /* If the string we want is too large to fit into a single buffer, then
     71     we're screwed; nothing will ever fit!  Change the maximum size of the
     72     cache to be big enough.  */
    6773  if (len > bufsize)
    6874    bufsize = len * 2;
     
    114120
    115121static struct hash_table strings;
     122static unsigned long total_adds = 0;
    116123
    117124static const char *
     
    119126{
    120127  /* Look up the string in the hash.  If it's there, return it.  */
    121   char **slot = (char **) hash_find_slot (&strings, str);
     128  char *const *slot = (char *const *) hash_find_slot (&strings, str);
    122129  const char *key = *slot;
     130
     131  /* Count the total number of adds we performed.  */
     132  ++total_adds;
    123133
    124134  if (!HASH_VACANT (key))
     
    156166strcache_add_len (const char *str, int len)
    157167{
    158   char *key = alloca (len + 1);
    159   memcpy (key, str, len);
    160   key[len] = '\0';
    161 
    162   return add_hash (key, len);
     168  /* If we're not given a nul-terminated string we have to create one, because
     169     the hashing functions expect it.  */
     170  if (str[len] != '\0')
     171    {
     172      char *key = alloca (len + 1);
     173      memcpy (key, str, len);
     174      key[len] = '\0';
     175      str = key;
     176    }
     177
     178  return add_hash (str, len);
    163179}
    164180
     
    174190strcache_init (void)
    175191{
    176   hash_init (&strings, 1000, str_hash_1, str_hash_2, str_hash_cmp);
     192  hash_init (&strings, 8000, str_hash_1, str_hash_2, str_hash_cmp);
    177193}
    178194
     
    186202  int totsize = 0, avgsize, maxsize = 0, minsize = bufsize;
    187203  int totfree = 0, avgfree, maxfree = 0, minfree = bufsize;
    188   const struct strcache *sp;
    189 
    190   for (sp = strcache; sp != NULL; sp = sp->next)
     204  int lastused = 0, lastfree = 0;
     205
     206  if (strcache)
    191207    {
    192       int bf = sp->bytesfree;
    193       int sz = (sp->end - sp->buffer) + bf;
    194 
    195       ++numbuffs;
    196       numstrs += sp->count;
    197 
    198       totsize += sz;
    199       maxsize = (sz > maxsize ? sz : maxsize);
    200       minsize = (sz < minsize ? sz : minsize);
    201 
    202       totfree += bf;
    203       maxfree = (bf > maxfree ? bf : maxfree);
    204       minfree = (bf < minfree ? bf : minfree);
     208      const struct strcache *sp;
     209
     210      /* Count the first buffer separately since it's not full.  */
     211      lastused = strcache->end - strcache->buffer;
     212      lastfree = strcache->bytesfree;
     213
     214      for (sp = strcache->next; sp != NULL; sp = sp->next)
     215        {
     216          int bf = sp->bytesfree;
     217          int sz = sp->end - sp->buffer;
     218
     219          ++numbuffs;
     220          numstrs += sp->count;
     221
     222          totsize += sz;
     223          maxsize = (sz > maxsize ? sz : maxsize);
     224          minsize = (sz < minsize ? sz : minsize);
     225
     226          totfree += bf;
     227          maxfree = (bf > maxfree ? bf : maxfree);
     228          minfree = (bf < minfree ? bf : minfree);
     229        }
    205230    }
    206231
     
    208233  avgfree = numbuffs ? (int)(totfree / numbuffs) : 0;
    209234
    210   printf (_("\n%s # of strings in strcache: %d\n"), prefix, numstrs);
    211   printf (_("%s # of strcache buffers: %d\n"), prefix, numbuffs);
    212   printf (_("%s strcache size: total = %d / max = %d / min = %d / avg = %d\n"),
    213           prefix, totsize, maxsize, minsize, avgsize);
    214   printf (_("%s strcache free: total = %d / max = %d / min = %d / avg = %d\n"),
    215           prefix, totfree, maxfree, minfree, avgfree);
    216 }
     235  printf (_("\n%s # of strings in strcache: %d / lookups = %lu / hits = %lu\n"),
     236          prefix, numstrs, total_adds, (total_adds - numstrs));
     237  printf (_("%s # of strcache buffers: %d (* %d B/buffer = %d B)\n"),
     238          prefix, (numbuffs + 1), bufsize, ((numbuffs + 1) * bufsize));
     239  printf (_("%s strcache used: total = %d (%d) / max = %d / min = %d / avg = %d\n"),
     240          prefix, totsize, lastused, maxsize, minsize, avgsize);
     241  printf (_("%s strcache free: total = %d (%d) / max = %d / min = %d / avg = %d\n"),
     242          prefix, totfree, lastfree, maxfree, minfree, avgfree);
     243
     244  fputs (_("\n# strcache hash-table stats:\n# "), stdout);
     245  hash_print_stats (&strings, stdout);
     246}
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette