Changeset 1912 in kBuild
- Timestamp:
- Oct 22, 2008 9:17:27 PM (16 years ago)
- Location:
- trunk/src/kmk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/incdep.c
r1909 r1912 773 773 entry->length, 774 774 entry->hash1, 775 #ifdef STRCACHE2_USE_CHAINING776 775 1); 777 #else778 entry->hash2);779 #endif780 776 return (const char *)entry->user; 781 777 } -
trunk/src/kmk/strcache2.c
r1910 r1912 287 287 288 288 MY_INLINE unsigned int 289 strcache2_case_sensitive_hash_2 (const char *str, unsigned int len)290 {291 #ifndef STRCACHE2_USE_CHAINING292 unsigned int hash = 0;293 if (MY_PREDICT_TRUE(len >= 2))294 {295 unsigned int ch0 = *str++;296 hash = 0;297 len--;298 while (len >= 2)299 {300 unsigned int ch1 = *str++;301 hash += ch0 << (ch1 & 0x7);302 303 ch0 = *str++;304 hash += ch1 << (ch0 & 0x7);305 306 len -= 2;307 }308 if (len == 1)309 {310 unsigned ch1 = *str;311 hash += ch0 << (ch1 & 0x7);312 313 hash += ch1;314 }315 else316 hash += ch0;317 }318 else if (len)319 {320 hash = *str;321 hash += hash << (hash & 0x7);322 }323 else324 hash = 1;325 hash |= 1;326 return hash;327 #else /* STRCACHE2_USE_CHAINING */328 return 1;329 #endif /* STRCACHE2_USE_CHAINING */330 }331 332 MY_INLINE unsigned int333 289 strcache2_case_insensitive_hash_1 (const char *str, unsigned int len) 334 290 { … … 371 327 hash = 0; 372 328 return hash; 373 }374 375 MY_INLINE unsigned int376 strcache2_case_insensitive_hash_2 (const char *str, unsigned int len)377 {378 #ifndef STRCACHE2_USE_CHAINING379 unsigned int hash = 0;380 if (MY_PREDICT_TRUE(len >= 2))381 {382 unsigned int ch0 = *str++;383 ch0 = tolower (ch0);384 hash = 0;385 len--;386 while (len >= 2)387 {388 unsigned int ch1 = *str++;389 ch1 = tolower (ch1);390 hash += ch0 << (ch1 & 0x7);391 392 ch0 = *str++;393 ch0 = tolower (ch0);394 hash += ch1 << (ch0 & 0x7);395 396 len -= 2;397 }398 if (len == 1)399 {400 unsigned ch1 = *str;401 ch1 = tolower (ch1);402 hash += ch0 << (ch1 & 0x7);403 404 hash += ch1;405 }406 else407 hash += ch0;408 }409 else if (len)410 {411 hash = *str;412 hash += hash << (hash & 0x7);413 }414 else415 hash = 1;416 hash |= 1;417 return hash;418 #else /* STRCACHE2_USE_CHAINING */419 return 1;420 #endif /* STRCACHE2_USE_CHAINING */421 329 } 422 330 … … 666 574 667 575 /* Copy the entries from the old to the new hash table. */ 668 #ifdef STRCACHE2_USE_CHAINING669 576 cache->collision_count = 0; 670 577 while (src-- > 0) … … 682 589 } 683 590 } 684 #else /* !STRCACHE2_USE_CHAINING */685 while (src-- > 0)686 {687 struct strcache2_entry *entry = src_tab[src];688 if (entry)689 {690 unsigned int dst = STRCACHE2_MOD_IT (cache, entry->hash1);691 if (dst_tab[dst])692 {693 unsigned int hash2 = entry->hash2;694 if (!hash2)695 entry->hash2 = hash2 = cache->case_insensitive696 ? strcache2_case_insensitive_hash_2 ((const char *)(entry + 1), entry->length)697 : strcache2_case_sensitive_hash_2 ((const char *)(entry + 1), entry->length);698 dst += hash2;699 dst = STRCACHE2_MOD_IT (cache, dst);700 while (dst_tab[dst])701 {702 dst += hash2;703 dst = STRCACHE2_MOD_IT (cache, dst);704 }705 }706 dst_tab[dst] = entry;707 }708 }709 #endif /* !STRCACHE2_USE_CHAINING */710 591 711 592 /* That's it, just free the old table and we're done. */ … … 782 663 entry->length = length; 783 664 entry->hash1 = hash1; 784 #ifndef STRCACHE2_USE_CHAINING785 entry->hash2 = hash2;786 #endif787 665 str_copy = (char *) memcpy (entry + 1, str, length); 788 666 str_copy[length] = '\0'; 789 667 790 #ifdef STRCACHE2_USE_CHAINING791 668 if ((entry->next = cache->hash_tab[idx]) != 0) 792 669 cache->collision_count++; 793 #endif /* STRCACHE2_USE_CHAINING */794 670 cache->hash_tab[idx] = entry; 795 671 cache->count++; … … 823 699 cache->collision_1st_count++; 824 700 825 #ifdef STRCACHE2_USE_CHAINING826 701 entry = entry->next; 827 #else /* !STRCACHE2_USE_CHAINING */828 hash2 = strcache2_case_sensitive_hash_2 (str, length);829 idx += hash2;830 idx = STRCACHE2_MOD_IT (cache, idx);831 entry = cache->hash_tab[idx];832 #endif /* !STRCACHE2_USE_CHAINING */833 702 if (!entry) 834 703 return strcache2_enter_string (cache, idx, str, length, hash1, hash2); … … 840 709 for (;;) 841 710 { 842 #ifdef STRCACHE2_USE_CHAINING843 711 entry = entry->next; 844 #else /* !STRCACHE2_USE_CHAINING */845 idx += hash2;846 idx = STRCACHE2_MOD_IT (cache, idx);847 entry = cache->hash_tab[idx];848 #endif /* !STRCACHE2_USE_CHAINING */849 712 if (!entry) 850 713 return strcache2_enter_string (cache, idx, str, length, hash1, hash2); … … 889 752 cache->collision_1st_count++; 890 753 891 #ifdef STRCACHE2_USE_CHAINING892 754 entry = entry->next; 893 #else /* !STRCACHE2_USE_CHAINING */894 if (!hash2)895 hash2 = strcache2_case_sensitive_hash_2 (str, length);896 idx += hash2;897 idx = STRCACHE2_MOD_IT (cache, idx);898 entry = cache->hash_tab[idx];899 #endif /* !STRCACHE2_USE_CHAINING */900 755 if (!entry) 901 756 return strcache2_enter_string (cache, idx, str, length, hash1, hash2); … … 907 762 for (;;) 908 763 { 909 #ifdef STRCACHE2_USE_CHAINING910 764 entry = entry->next; 911 #else /* !STRCACHE2_USE_CHAINING */912 idx += hash2;913 idx = STRCACHE2_MOD_IT (cache, idx);914 entry = cache->hash_tab[idx];915 #endif /* !STRCACHE2_USE_CHAINING */916 765 if (!entry) 917 766 return strcache2_enter_string (cache, idx, str, length, hash1, hash2); … … 929 778 struct strcache2_entry const *entry; 930 779 unsigned int hash1 = strcache2_case_sensitive_hash_1 (str, length); 931 #ifndef STRCACHE2_USE_CHAINING932 unsigned int hash2;933 #endif934 780 unsigned int idx; 935 781 … … 948 794 cache->collision_1st_count++; 949 795 950 #ifdef STRCACHE2_USE_CHAINING951 796 entry = entry->next; 952 #else /* !STRCACHE2_USE_CHAINING */953 hash2 = strcache2_case_sensitive_hash_2 (str, length);954 idx += hash2;955 idx = STRCACHE2_MOD_IT (cache, idx);956 entry = cache->hash_tab[idx];957 #endif /* !STRCACHE2_USE_CHAINING */958 797 if (!entry) 959 798 return NULL; … … 965 804 for (;;) 966 805 { 967 #ifdef STRCACHE2_USE_CHAINING968 806 entry = entry->next; 969 #else /* !STRCACHE2_USE_CHAINING */970 idx += hash2;971 idx = STRCACHE2_MOD_IT (cache, idx);972 entry = cache->hash_tab[idx];973 #endif /* !STRCACHE2_USE_CHAINING */974 807 if (!entry) 975 808 return NULL; … … 1006 839 cache->collision_1st_count++; 1007 840 1008 #ifdef STRCACHE2_USE_CHAINING1009 841 entry = entry->next; 1010 #else /* !STRCACHE2_USE_CHAINING */1011 hash2 = strcache2_case_insensitive_hash_2 (str, length);1012 idx += hash2;1013 idx = STRCACHE2_MOD_IT (cache, idx);1014 entry = cache->hash_tab[idx];1015 #endif /* !STRCACHE2_USE_CHAINING */1016 842 if (!entry) 1017 843 return strcache2_enter_string (cache, idx, str, length, hash1, hash2); … … 1023 849 for (;;) 1024 850 { 1025 #ifdef STRCACHE2_USE_CHAINING1026 851 entry = entry->next; 1027 #else /* !STRCACHE2_USE_CHAINING */1028 idx += hash2;1029 idx = STRCACHE2_MOD_IT (cache, idx);1030 entry = cache->hash_tab[idx];1031 #endif /* !STRCACHE2_USE_CHAINING */1032 852 if (!entry) 1033 853 return strcache2_enter_string (cache, idx, str, length, hash1, hash2); … … 1072 892 cache->collision_1st_count++; 1073 893 1074 #ifdef STRCACHE2_USE_CHAINING1075 894 entry = entry->next; 1076 #else /* !STRCACHE2_USE_CHAINING */1077 if (!hash2)1078 hash2 = strcache2_case_insensitive_hash_2 (str, length);1079 idx += hash2;1080 idx = STRCACHE2_MOD_IT (cache, idx);1081 entry = cache->hash_tab[idx];1082 #endif /* !STRCACHE2_USE_CHAINING */1083 895 if (!entry) 1084 896 return strcache2_enter_string (cache, idx, str, length, hash1, hash2); … … 1090 902 for (;;) 1091 903 { 1092 #ifdef STRCACHE2_USE_CHAINING1093 904 entry = entry->next; 1094 #else /* !STRCACHE2_USE_CHAINING */1095 idx += hash2;1096 idx = STRCACHE2_MOD_IT (cache, idx);1097 entry = cache->hash_tab[idx];1098 #endif /* !STRCACHE2_USE_CHAINING */1099 905 if (!entry) 1100 906 return strcache2_enter_string (cache, idx, str, length, hash1, hash2); … … 1112 918 struct strcache2_entry const *entry; 1113 919 unsigned int hash1 = strcache2_case_insensitive_hash_1 (str, length); 1114 #ifndef STRCACHE2_USE_CHAINING1115 unsigned int hash2;1116 #endif1117 920 unsigned int idx; 1118 921 … … 1131 934 cache->collision_1st_count++; 1132 935 1133 #ifdef STRCACHE2_USE_CHAINING1134 936 entry = entry->next; 1135 #else /* !STRCACHE2_USE_CHAINING */1136 hash2 = strcache2_case_insensitive_hash_2 (str, length);1137 idx += hash2;1138 idx = STRCACHE2_MOD_IT (cache, idx);1139 entry = cache->hash_tab[idx];1140 #endif /* !STRCACHE2_USE_CHAINING */1141 937 if (!entry) 1142 938 return NULL; … … 1148 944 for (;;) 1149 945 { 1150 #ifdef STRCACHE2_USE_CHAINING1151 946 entry = entry->next; 1152 #else /* !STRCACHE2_USE_CHAINING */1153 idx += hash2;1154 idx = STRCACHE2_MOD_IT (cache, idx);1155 entry = cache->hash_tab[idx];1156 #endif /* !STRCACHE2_USE_CHAINING */1157 947 if (!entry) 1158 948 return NULL; … … 1222 1012 } 1223 1013 1224 #ifndef STRCACHE2_USE_CHAINING1225 if (entry->hash2)1226 {1227 hash = cache->case_insensitive1228 ? strcache2_case_insensitive_hash_2 (str, entry->length)1229 : strcache2_case_sensitive_hash_2 (str, entry->length);1230 if (hash != entry->hash2)1231 {1232 fprintf (stderr,1233 "strcache2[%s]: corrupt entry %p, hash#2: %x, expected %x;\nstring: %s\n",1234 cache->name, (void *)entry, hash, entry->hash2, str);1235 return -1;1236 }1237 }1238 #endif1239 1240 1014 return 0; 1241 1015 } 1242 1016 1243 #ifndef STRCACHE2_USE_CHAINING1244 /* Fallback for calculating and returning the 2nd hash. */1245 unsigned int1246 strcache2_get_hash2_fallback (struct strcache2 *cache, const char *str)1247 {1248 struct strcache2_entry *entry = (struct strcache2_entry *) str - 1;1249 unsigned hash2 = cache->case_insensitive1250 ? strcache2_case_insensitive_hash_2 (str, entry->length)1251 : strcache2_case_sensitive_hash_2 (str, entry->length);1252 entry->hash2 = hash2;1253 return hash2;1254 }1255 #endif /* !STRCACHE2_USE_CHAINING */1256 1017 1257 1018 /* Calculates the case sensitive hash values of the string. … … 1259 1020 unsigned int strcache2_hash_str (const char *str, unsigned int length, unsigned int *hash2p) 1260 1021 { 1261 #ifndef STRCACHE2_USE_CHAINING1262 *hash2p = strcache2_case_sensitive_hash_2 (str, length);1263 #else1264 1022 *hash2p = 1; 1265 #endif1266 1023 return strcache2_case_sensitive_hash_1 (str, length); 1267 1024 } … … 1271 1028 unsigned int strcache2_hash_istr (const char *str, unsigned int length, unsigned int *hash2p) 1272 1029 { 1273 #ifndef STRCACHE2_USE_CHAINING1274 *hash2p = strcache2_case_insensitive_hash_2 (str, length);1275 #else1276 1030 *hash2p = 1; 1277 #endif1278 1031 return strcache2_case_insensitive_hash_1 (str, length); 1279 1032 } … … 1316 1069 #endif 1317 1070 cache->count = 0; 1318 #ifdef STRCACHE2_USE_CHAINING1319 1071 cache->collision_count = 0; 1320 #endif1321 1072 cache->lookup_count = 0; 1322 1073 cache->collision_1st_count = 0; … … 1390 1141 unsigned int idx; 1391 1142 unsigned int rehashes; 1392 #ifdef STRCACHE2_USE_CHAINING1393 1143 unsigned int chain_depths[32]; 1394 #endif1395 1144 1396 1145 printf (_("\n%s strcache2: %s\n"), prefix, cache->name); … … 1411 1160 1412 1161 /* String statistics. */ 1413 #ifdef STRCACHE2_USE_CHAINING1414 1162 memset (chain_depths, '\0', sizeof (chain_depths)); 1415 #endif1416 1163 idx = cache->hash_size; 1417 1164 while (idx-- > 0) 1418 1165 { 1419 1166 struct strcache2_entry const *entry = cache->hash_tab[idx]; 1420 #ifdef STRCACHE2_USE_CHAINING1421 1167 unsigned int depth = 0; 1422 1168 for (; entry != 0; entry = entry->next, depth++) 1423 #else1424 if (entry)1425 #endif1426 1169 { 1427 1170 unsigned int length = entry->length; … … 1433 1176 str_count++; 1434 1177 } 1435 #ifdef STRCACHE2_USE_CHAINING1436 1178 chain_depths[depth >= 32 ? 31 : depth]++; 1437 #endif1438 1179 } 1439 1180 str_avg_len = cache->count ? str_total_len / cache->count : 0; … … 1465 1206 cache->collision_2nd_count, (unsigned int)((100.0 * cache->collision_2nd_count) / cache->lookup_count), 1466 1207 cache->collision_3rd_count, (unsigned int)((100.0 * cache->collision_3rd_count) / cache->lookup_count)); 1467 #ifdef STRCACHE2_USE_CHAINING1468 1208 printf (_("%s hash insert collisions = %u (%u%%)\n"), 1469 1209 prefix, cache->collision_count,(unsigned int)((100.0 * cache->collision_count) / cache->count)); … … 1484 1224 strs_at_this_depth, (unsigned int)((100.0 * strs_at_this_depth) / cache->count), idx - 1); 1485 1225 } 1486 #endif1487 1226 } 1488 1227 -
trunk/src/kmk/strcache2.h
r1909 r1912 33 33 34 34 #define STRCACHE2_USE_MASK 1 35 #define STRCACHE2_USE_CHAINING 136 35 37 36 /* string cache memory segment. */ … … 48 47 struct strcache2_entry 49 48 { 50 #ifdef STRCACHE2_USE_CHAINING51 49 struct strcache2_entry *next; /* Collision chain. */ 52 #endif53 50 void *user; 54 51 unsigned int hash1; 55 #ifndef STRCACHE2_USE_CHAINING56 unsigned int hash2;57 #endif58 52 unsigned int length; 59 53 }; … … 89 83 unsigned long collision_3rd_count; /* The number of 3rd level collisions. */ 90 84 unsigned int count; /* Number entries in the cache. */ 91 #ifdef STRCACHE2_USE_CHAINING92 85 unsigned int collision_count; /* Number of entries in chains. */ 93 #endif94 86 unsigned int rehash_count; /* When to rehash the table. */ 95 87 unsigned int init_size; /* The initial hash table size. */ … … 155 147 } 156 148 157 #ifndef STRCACHE2_USE_CHAINING158 /* Get the second hash value for the string. */159 MY_INLINE unsigned int160 strcache2_get_hash2 (struct strcache2 *cache, const char *str)161 {162 unsigned int hash2 = strcache2_get_entry (cache, str)->hash2;163 if (!hash2)164 hash2 = strcache2_get_hash2_fallback (cache, str);165 return hash2;166 }167 #endif168 169 149 /* Get the pointer hash value for the string. 170 150
Note:
See TracChangeset
for help on using the changeset viewer.