Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 1 | /* Hash tables. |
Thomas Koenig | b18a97e | 2021-09-13 19:49:49 +0200 | [diff] [blame] | 2 | Copyright (C) 2000-2021 Free Software Foundation, Inc. |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 3 | |
| 4 | This program is free software; you can redistribute it and/or modify it |
| 5 | under the terms of the GNU General Public License as published by the |
Jakub Jelinek | 748086b | 2009-04-09 17:00:19 +0200 | [diff] [blame] | 6 | Free Software Foundation; either version 3, or (at your option) any |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 7 | later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
Jakub Jelinek | 748086b | 2009-04-09 17:00:19 +0200 | [diff] [blame] | 15 | along with this program; see the file COPYING3. If not see |
| 16 | <http://www.gnu.org/licenses/>. |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 17 | |
| 18 | In other words, you are welcome to use, share and improve this program. |
| 19 | You are forbidden to forbid anyone else to use, share and improve |
| 20 | what you give them. Help stamp out software-hoarding! */ |
| 21 | |
| 22 | #include "config.h" |
| 23 | #include "system.h" |
Paolo Bonzini | 4f4e53dd | 2004-05-24 10:50:45 +0000 | [diff] [blame] | 24 | #include "symtab.h" |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 25 | |
| 26 | /* The code below is a specialization of Vladimir Makarov's expandable |
| 27 | hash tables (see libiberty/hashtab.c). The abstraction penalty was |
| 28 | too high to continue using the generic form. This code knows |
| 29 | intrinsically how to calculate a hash value, and how to compare an |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 30 | existing entry with a potential new one. */ |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 31 | |
Roger Sayle | 7bb3fbb | 2003-08-08 20:23:06 +0000 | [diff] [blame] | 32 | static unsigned int calc_hash (const unsigned char *, size_t); |
Diego Novillo | 0823efe | 2012-08-14 21:56:07 -0400 | [diff] [blame] | 33 | static void ht_expand (cpp_hash_table *); |
Zack Weinberg | a2f7be9 | 2003-07-22 16:24:53 +0000 | [diff] [blame] | 34 | static double approx_sqrt (double); |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 35 | |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 36 | /* A deleted entry. */ |
| 37 | #define DELETED ((hashnode) -1) |
| 38 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 39 | /* Calculate the hash of the string STR of length LEN. */ |
| 40 | |
| 41 | static unsigned int |
Roger Sayle | 7bb3fbb | 2003-08-08 20:23:06 +0000 | [diff] [blame] | 42 | calc_hash (const unsigned char *str, size_t len) |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 43 | { |
Roger Sayle | 7bb3fbb | 2003-08-08 20:23:06 +0000 | [diff] [blame] | 44 | size_t n = len; |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 45 | unsigned int r = 0; |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 46 | |
| 47 | while (n--) |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 48 | r = HT_HASHSTEP (r, *str++); |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 49 | |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 50 | return HT_HASHFINISH (r, len); |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /* Initialize an identifier hashtable. */ |
| 54 | |
Diego Novillo | 0823efe | 2012-08-14 21:56:07 -0400 | [diff] [blame] | 55 | cpp_hash_table * |
Andreas Jaeger | 1d088de | 2003-07-06 08:15:36 +0200 | [diff] [blame] | 56 | ht_create (unsigned int order) |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 57 | { |
| 58 | unsigned int nslots = 1 << order; |
Diego Novillo | 0823efe | 2012-08-14 21:56:07 -0400 | [diff] [blame] | 59 | cpp_hash_table *table; |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 60 | |
Diego Novillo | 0823efe | 2012-08-14 21:56:07 -0400 | [diff] [blame] | 61 | table = XCNEW (cpp_hash_table); |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 62 | |
| 63 | /* Strings need no alignment. */ |
Alan Modra | 19a9ba6 | 2014-10-22 12:11:31 +1030 | [diff] [blame] | 64 | obstack_specify_allocation (&table->stack, 0, 0, xmalloc, free); |
Zack Weinberg | 4383964 | 2003-07-13 17:34:18 +0000 | [diff] [blame] | 65 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 66 | obstack_alignment_mask (&table->stack) = 0; |
| 67 | |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 68 | table->entries = XCNEWVEC (hashnode, nslots); |
Geoffrey Keating | b453c95 | 2004-05-30 00:49:06 +0000 | [diff] [blame] | 69 | table->entries_owned = true; |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 70 | table->nslots = nslots; |
| 71 | return table; |
| 72 | } |
| 73 | |
Neil Booth | bef985f | 2001-08-11 12:37:19 +0000 | [diff] [blame] | 74 | /* Frees all memory associated with a hash table. */ |
| 75 | |
| 76 | void |
Diego Novillo | 0823efe | 2012-08-14 21:56:07 -0400 | [diff] [blame] | 77 | ht_destroy (cpp_hash_table *table) |
Neil Booth | bef985f | 2001-08-11 12:37:19 +0000 | [diff] [blame] | 78 | { |
| 79 | obstack_free (&table->stack, NULL); |
Geoffrey Keating | b453c95 | 2004-05-30 00:49:06 +0000 | [diff] [blame] | 80 | if (table->entries_owned) |
| 81 | free (table->entries); |
Neil Booth | bef985f | 2001-08-11 12:37:19 +0000 | [diff] [blame] | 82 | free (table); |
| 83 | } |
| 84 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 85 | /* Returns the hash entry for the a STR of length LEN. If that string |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 86 | already exists in the table, returns the existing entry. If the |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 87 | identifier hasn't been seen before, and INSERT is CPP_NO_INSERT, |
| 88 | returns NULL. Otherwise insert and returns a new entry. A new |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 89 | string is allocated. */ |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 90 | hashnode |
Diego Novillo | 0823efe | 2012-08-14 21:56:07 -0400 | [diff] [blame] | 91 | ht_lookup (cpp_hash_table *table, const unsigned char *str, size_t len, |
Andreas Jaeger | 1d088de | 2003-07-06 08:15:36 +0200 | [diff] [blame] | 92 | enum ht_lookup_option insert) |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 93 | { |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 94 | return ht_lookup_with_hash (table, str, len, calc_hash (str, len), |
| 95 | insert); |
| 96 | } |
| 97 | |
| 98 | hashnode |
Diego Novillo | 0823efe | 2012-08-14 21:56:07 -0400 | [diff] [blame] | 99 | ht_lookup_with_hash (cpp_hash_table *table, const unsigned char *str, |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 100 | size_t len, unsigned int hash, |
| 101 | enum ht_lookup_option insert) |
| 102 | { |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 103 | unsigned int hash2; |
| 104 | unsigned int index; |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 105 | unsigned int deleted_index = table->nslots; |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 106 | size_t sizemask; |
| 107 | hashnode node; |
| 108 | |
| 109 | sizemask = table->nslots - 1; |
| 110 | index = hash & sizemask; |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 111 | table->searches++; |
| 112 | |
Roger Sayle | 7bb3fbb | 2003-08-08 20:23:06 +0000 | [diff] [blame] | 113 | node = table->entries[index]; |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 114 | |
Roger Sayle | 7bb3fbb | 2003-08-08 20:23:06 +0000 | [diff] [blame] | 115 | if (node != NULL) |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 116 | { |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 117 | if (node == DELETED) |
| 118 | deleted_index = index; |
| 119 | else if (node->hash_value == hash |
| 120 | && HT_LEN (node) == (unsigned int) len |
| 121 | && !memcmp (HT_STR (node), str, len)) |
| 122 | return node; |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 123 | |
Roger Sayle | 7bb3fbb | 2003-08-08 20:23:06 +0000 | [diff] [blame] | 124 | /* hash2 must be odd, so we're guaranteed to visit every possible |
| 125 | location in the table during rehashing. */ |
| 126 | hash2 = ((hash * 17) & sizemask) | 1; |
| 127 | |
| 128 | for (;;) |
| 129 | { |
| 130 | table->collisions++; |
| 131 | index = (index + hash2) & sizemask; |
| 132 | node = table->entries[index]; |
| 133 | if (node == NULL) |
| 134 | break; |
| 135 | |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 136 | if (node == DELETED) |
Roger Sayle | 7bb3fbb | 2003-08-08 20:23:06 +0000 | [diff] [blame] | 137 | { |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 138 | if (deleted_index != table->nslots) |
| 139 | deleted_index = index; |
Roger Sayle | 7bb3fbb | 2003-08-08 20:23:06 +0000 | [diff] [blame] | 140 | } |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 141 | else if (node->hash_value == hash |
| 142 | && HT_LEN (node) == (unsigned int) len |
| 143 | && !memcmp (HT_STR (node), str, len)) |
| 144 | return node; |
Roger Sayle | 7bb3fbb | 2003-08-08 20:23:06 +0000 | [diff] [blame] | 145 | } |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | if (insert == HT_NO_INSERT) |
| 149 | return NULL; |
| 150 | |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 151 | /* We prefer to overwrite the first deleted slot we saw. */ |
| 152 | if (deleted_index != table->nslots) |
| 153 | index = deleted_index; |
| 154 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 155 | node = (*table->alloc_node) (table); |
| 156 | table->entries[index] = node; |
| 157 | |
Roger Sayle | 7bb3fbb | 2003-08-08 20:23:06 +0000 | [diff] [blame] | 158 | HT_LEN (node) = (unsigned int) len; |
Gabriel Dos Reis | 5e0c54e | 2003-05-18 13:40:54 +0000 | [diff] [blame] | 159 | node->hash_value = hash; |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 160 | |
| 161 | if (table->alloc_subobject) |
| 162 | { |
Jerry Quinn | f1bf410 | 2009-07-18 03:22:16 +0000 | [diff] [blame] | 163 | char *chars = (char *) table->alloc_subobject (len + 1); |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 164 | memcpy (chars, str, len); |
| 165 | chars[len] = '\0'; |
| 166 | HT_STR (node) = (const unsigned char *) chars; |
| 167 | } |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 168 | else |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 169 | HT_STR (node) = (const unsigned char *) obstack_copy0 (&table->stack, |
| 170 | str, len); |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 171 | |
| 172 | if (++table->nelements * 4 >= table->nslots * 3) |
| 173 | /* Must expand the string table. */ |
| 174 | ht_expand (table); |
| 175 | |
| 176 | return node; |
| 177 | } |
| 178 | |
| 179 | /* Double the size of a hash table, re-hashing existing entries. */ |
| 180 | |
| 181 | static void |
Diego Novillo | 0823efe | 2012-08-14 21:56:07 -0400 | [diff] [blame] | 182 | ht_expand (cpp_hash_table *table) |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 183 | { |
| 184 | hashnode *nentries, *p, *limit; |
| 185 | unsigned int size, sizemask; |
| 186 | |
| 187 | size = table->nslots * 2; |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 188 | nentries = XCNEWVEC (hashnode, size); |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 189 | sizemask = size - 1; |
| 190 | |
| 191 | p = table->entries; |
| 192 | limit = p + table->nslots; |
| 193 | do |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 194 | if (*p && *p != DELETED) |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 195 | { |
| 196 | unsigned int index, hash, hash2; |
| 197 | |
Gabriel Dos Reis | 5e0c54e | 2003-05-18 13:40:54 +0000 | [diff] [blame] | 198 | hash = (*p)->hash_value; |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 199 | index = hash & sizemask; |
| 200 | |
Roger Sayle | 4ae2e3e | 2003-08-22 22:29:17 +0000 | [diff] [blame] | 201 | if (nentries[index]) |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 202 | { |
Roger Sayle | 4ae2e3e | 2003-08-22 22:29:17 +0000 | [diff] [blame] | 203 | hash2 = ((hash * 17) & sizemask) | 1; |
| 204 | do |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 205 | { |
Roger Sayle | 4ae2e3e | 2003-08-22 22:29:17 +0000 | [diff] [blame] | 206 | index = (index + hash2) & sizemask; |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 207 | } |
Roger Sayle | 4ae2e3e | 2003-08-22 22:29:17 +0000 | [diff] [blame] | 208 | while (nentries[index]); |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 209 | } |
Roger Sayle | 4ae2e3e | 2003-08-22 22:29:17 +0000 | [diff] [blame] | 210 | nentries[index] = *p; |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 211 | } |
| 212 | while (++p < limit); |
| 213 | |
Geoffrey Keating | b453c95 | 2004-05-30 00:49:06 +0000 | [diff] [blame] | 214 | if (table->entries_owned) |
| 215 | free (table->entries); |
| 216 | table->entries_owned = true; |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 217 | table->entries = nentries; |
| 218 | table->nslots = size; |
| 219 | } |
| 220 | |
| 221 | /* For all nodes in TABLE, callback CB with parameters TABLE->PFILE, |
| 222 | the node, and V. */ |
| 223 | void |
Diego Novillo | 0823efe | 2012-08-14 21:56:07 -0400 | [diff] [blame] | 224 | ht_forall (cpp_hash_table *table, ht_cb cb, const void *v) |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 225 | { |
| 226 | hashnode *p, *limit; |
| 227 | |
| 228 | p = table->entries; |
| 229 | limit = p + table->nslots; |
| 230 | do |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 231 | if (*p && *p != DELETED) |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 232 | { |
| 233 | if ((*cb) (table->pfile, *p, v) == 0) |
| 234 | break; |
| 235 | } |
| 236 | while (++p < limit); |
| 237 | } |
| 238 | |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 239 | /* Like ht_forall, but a nonzero return from the callback means that |
| 240 | the entry should be removed from the table. */ |
| 241 | void |
Diego Novillo | 0823efe | 2012-08-14 21:56:07 -0400 | [diff] [blame] | 242 | ht_purge (cpp_hash_table *table, ht_cb cb, const void *v) |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 243 | { |
| 244 | hashnode *p, *limit; |
| 245 | |
| 246 | p = table->entries; |
| 247 | limit = p + table->nslots; |
| 248 | do |
| 249 | if (*p && *p != DELETED) |
| 250 | { |
| 251 | if ((*cb) (table->pfile, *p, v)) |
| 252 | *p = DELETED; |
| 253 | } |
| 254 | while (++p < limit); |
| 255 | } |
| 256 | |
Geoffrey Keating | b453c95 | 2004-05-30 00:49:06 +0000 | [diff] [blame] | 257 | /* Restore the hash table. */ |
| 258 | void |
Diego Novillo | 0823efe | 2012-08-14 21:56:07 -0400 | [diff] [blame] | 259 | ht_load (cpp_hash_table *ht, hashnode *entries, |
Geoffrey Keating | b453c95 | 2004-05-30 00:49:06 +0000 | [diff] [blame] | 260 | unsigned int nslots, unsigned int nelements, |
| 261 | bool own) |
| 262 | { |
| 263 | if (ht->entries_owned) |
| 264 | free (ht->entries); |
| 265 | ht->entries = entries; |
| 266 | ht->nslots = nslots; |
| 267 | ht->nelements = nelements; |
| 268 | ht->entries_owned = own; |
| 269 | } |
| 270 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 271 | /* Dump allocation statistics to stderr. */ |
| 272 | |
| 273 | void |
Diego Novillo | 0823efe | 2012-08-14 21:56:07 -0400 | [diff] [blame] | 274 | ht_dump_statistics (cpp_hash_table *table) |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 275 | { |
| 276 | size_t nelts, nids, overhead, headers; |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 277 | size_t total_bytes, longest, deleted = 0; |
Serge Belyshev | 0fd9e8d | 2004-09-06 13:22:48 +0000 | [diff] [blame] | 278 | double sum_of_squares, exp_len, exp_len2, exp2_len; |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 279 | hashnode *p, *limit; |
| 280 | |
| 281 | #define SCALE(x) ((unsigned long) ((x) < 1024*10 \ |
| 282 | ? (x) \ |
| 283 | : ((x) < 1024*1024*10 \ |
| 284 | ? (x) / 1024 \ |
| 285 | : (x) / (1024*1024)))) |
| 286 | #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M')) |
| 287 | |
| 288 | total_bytes = longest = sum_of_squares = nids = 0; |
| 289 | p = table->entries; |
| 290 | limit = p + table->nslots; |
| 291 | do |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 292 | if (*p == DELETED) |
| 293 | ++deleted; |
| 294 | else if (*p) |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 295 | { |
| 296 | size_t n = HT_LEN (*p); |
| 297 | |
| 298 | total_bytes += n; |
Serge Belyshev | 0fd9e8d | 2004-09-06 13:22:48 +0000 | [diff] [blame] | 299 | sum_of_squares += (double) n * n; |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 300 | if (n > longest) |
| 301 | longest = n; |
| 302 | nids++; |
| 303 | } |
| 304 | while (++p < limit); |
Andreas Jaeger | 1d088de | 2003-07-06 08:15:36 +0200 | [diff] [blame] | 305 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 306 | nelts = table->nelements; |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 307 | headers = table->nslots * sizeof (hashnode); |
| 308 | |
Martin Liska | 6044817 | 2019-02-26 18:27:52 +0100 | [diff] [blame] | 309 | fprintf (stderr, "\nString pool\n%-32s%lu\n", "entries:", |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 310 | (unsigned long) nelts); |
Martin Liska | 6044817 | 2019-02-26 18:27:52 +0100 | [diff] [blame] | 311 | fprintf (stderr, "%-32s%lu (%.2f%%)\n", "identifiers:", |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 312 | (unsigned long) nids, nids * 100.0 / nelts); |
Martin Liska | 6044817 | 2019-02-26 18:27:52 +0100 | [diff] [blame] | 313 | fprintf (stderr, "%-32s%lu\n", "slots:", |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 314 | (unsigned long) table->nslots); |
Martin Liska | 6044817 | 2019-02-26 18:27:52 +0100 | [diff] [blame] | 315 | fprintf (stderr, "%-32s%lu\n", "deleted:", |
Tom Tromey | dae4174 | 2008-05-21 15:00:59 +0000 | [diff] [blame] | 316 | (unsigned long) deleted); |
Martin Liska | 46aeb07 | 2018-11-05 14:35:09 +0100 | [diff] [blame] | 317 | |
| 318 | if (table->alloc_subobject) |
Martin Liska | 6044817 | 2019-02-26 18:27:52 +0100 | [diff] [blame] | 319 | fprintf (stderr, "%-32s%lu%c\n", "GGC bytes:", |
Martin Liska | 46aeb07 | 2018-11-05 14:35:09 +0100 | [diff] [blame] | 320 | SCALE (total_bytes), LABEL (total_bytes)); |
| 321 | else |
| 322 | { |
| 323 | overhead = obstack_memory_used (&table->stack) - total_bytes; |
Martin Liska | 6044817 | 2019-02-26 18:27:52 +0100 | [diff] [blame] | 324 | fprintf (stderr, "%-32s%lu%c (%lu%c overhead)\n", |
| 325 | "obstack bytes:", |
Martin Liska | 037903c | 2018-11-05 15:25:37 +0100 | [diff] [blame] | 326 | SCALE (total_bytes), LABEL (total_bytes), |
| 327 | SCALE (overhead), LABEL (overhead)); |
Martin Liska | 46aeb07 | 2018-11-05 14:35:09 +0100 | [diff] [blame] | 328 | } |
Martin Liska | 6044817 | 2019-02-26 18:27:52 +0100 | [diff] [blame] | 329 | fprintf (stderr, "%-32s%lu%c\n", "table size:", |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 330 | SCALE (headers), LABEL (headers)); |
| 331 | |
| 332 | exp_len = (double)total_bytes / (double)nelts; |
| 333 | exp2_len = exp_len * exp_len; |
| 334 | exp_len2 = (double) sum_of_squares / (double) nelts; |
| 335 | |
Martin Liska | 6044817 | 2019-02-26 18:27:52 +0100 | [diff] [blame] | 336 | fprintf (stderr, "%-32s%.4f\n", "coll/search:", |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 337 | (double) table->collisions / (double) table->searches); |
Martin Liska | 6044817 | 2019-02-26 18:27:52 +0100 | [diff] [blame] | 338 | fprintf (stderr, "%-32s%.4f\n", "ins/search:", |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 339 | (double) nelts / (double) table->searches); |
Martin Liska | 6044817 | 2019-02-26 18:27:52 +0100 | [diff] [blame] | 340 | fprintf (stderr, "%-32s%.2f bytes (+/- %.2f)\n", |
| 341 | "avg. entry:", |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 342 | exp_len, approx_sqrt (exp_len2 - exp2_len)); |
Martin Liska | 6044817 | 2019-02-26 18:27:52 +0100 | [diff] [blame] | 343 | fprintf (stderr, "%-32s%lu\n", "longest entry:", |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 344 | (unsigned long) longest); |
| 345 | #undef SCALE |
| 346 | #undef LABEL |
| 347 | } |
| 348 | |
| 349 | /* Return the approximate positive square root of a number N. This is for |
| 350 | statistical reports, not code generation. */ |
Zack Weinberg | a2f7be9 | 2003-07-22 16:24:53 +0000 | [diff] [blame] | 351 | static double |
Andreas Jaeger | 1d088de | 2003-07-06 08:15:36 +0200 | [diff] [blame] | 352 | approx_sqrt (double x) |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 353 | { |
| 354 | double s, d; |
| 355 | |
| 356 | if (x < 0) |
| 357 | abort (); |
| 358 | if (x == 0) |
| 359 | return 0; |
| 360 | |
| 361 | s = x; |
| 362 | do |
| 363 | { |
| 364 | d = (s * s - x) / (2 * s); |
| 365 | s -= d; |
| 366 | } |
| 367 | while (d > .0001); |
| 368 | return s; |
| 369 | } |