]>
Commit | Line | Data |
---|---|---|
e8fa59b9 | 1 | #include "git-compat-util.h" |
6a364ced | 2 | #include "hashmap.h" |
7e8089c9 | 3 | #include "strbuf.h" |
6a364ced KB |
4 | |
5 | struct test_entry | |
6 | { | |
7 | struct hashmap_entry ent; | |
8 | /* key and value as two \0-terminated strings */ | |
9 | char key[FLEX_ARRAY]; | |
10 | }; | |
11 | ||
12 | static const char *get_value(const struct test_entry *e) | |
13 | { | |
14 | return e->key + strlen(e->key) + 1; | |
15 | } | |
16 | ||
6815d114 SB |
17 | static int test_entry_cmp(const void *cmp_data, |
18 | const void *entry, | |
19 | const void *entry_or_key, | |
20 | const void *keydata) | |
6a364ced | 21 | { |
6815d114 SB |
22 | const int ignore_case = cmp_data ? *((int *)cmp_data) : 0; |
23 | const struct test_entry *e1 = entry; | |
24 | const struct test_entry *e2 = entry_or_key; | |
25 | const char *key = keydata; | |
26 | ||
27 | if (ignore_case) | |
28 | return strcasecmp(e1->key, key ? key : e2->key); | |
29 | else | |
30 | return strcmp(e1->key, key ? key : e2->key); | |
6a364ced KB |
31 | } |
32 | ||
a6119f82 JK |
33 | static struct test_entry *alloc_test_entry(unsigned int hash, |
34 | char *key, char *value) | |
6a364ced | 35 | { |
7daa825d JK |
36 | size_t klen = strlen(key); |
37 | size_t vlen = strlen(value); | |
b6c4380d | 38 | struct test_entry *entry = xmalloc(st_add4(sizeof(*entry), klen, vlen, 2)); |
6a364ced KB |
39 | hashmap_entry_init(entry, hash); |
40 | memcpy(entry->key, key, klen + 1); | |
41 | memcpy(entry->key + klen + 1, value, vlen + 1); | |
42 | return entry; | |
43 | } | |
44 | ||
45 | #define HASH_METHOD_FNV 0 | |
46 | #define HASH_METHOD_I 1 | |
47 | #define HASH_METHOD_IDIV10 2 | |
48 | #define HASH_METHOD_0 3 | |
49 | #define HASH_METHOD_X2 4 | |
50 | #define TEST_SPARSE 8 | |
51 | #define TEST_ADD 16 | |
52 | #define TEST_SIZE 100000 | |
53 | ||
54 | static unsigned int hash(unsigned int method, unsigned int i, const char *key) | |
55 | { | |
3b9a2b07 | 56 | unsigned int hash = 0; |
6a364ced KB |
57 | switch (method & 3) |
58 | { | |
59 | case HASH_METHOD_FNV: | |
60 | hash = strhash(key); | |
61 | break; | |
62 | case HASH_METHOD_I: | |
63 | hash = i; | |
64 | break; | |
65 | case HASH_METHOD_IDIV10: | |
66 | hash = i / 10; | |
67 | break; | |
68 | case HASH_METHOD_0: | |
69 | hash = 0; | |
70 | break; | |
71 | } | |
72 | ||
73 | if (method & HASH_METHOD_X2) | |
74 | hash = 2 * hash; | |
75 | return hash; | |
76 | } | |
77 | ||
78 | /* | |
79 | * Test performance of hashmap.[ch] | |
80 | * Usage: time echo "perfhashmap method rounds" | test-hashmap | |
81 | */ | |
82 | static void perf_hashmap(unsigned int method, unsigned int rounds) | |
83 | { | |
84 | struct hashmap map; | |
85 | char buf[16]; | |
86 | struct test_entry **entries; | |
87 | unsigned int *hashes; | |
88 | unsigned int i, j; | |
89 | ||
aef6cf1e JK |
90 | ALLOC_ARRAY(entries, TEST_SIZE); |
91 | ALLOC_ARRAY(hashes, TEST_SIZE); | |
6a364ced | 92 | for (i = 0; i < TEST_SIZE; i++) { |
cbadf0ee | 93 | xsnprintf(buf, sizeof(buf), "%i", i); |
7daa825d | 94 | entries[i] = alloc_test_entry(0, buf, ""); |
6a364ced KB |
95 | hashes[i] = hash(method, i, entries[i]->key); |
96 | } | |
97 | ||
98 | if (method & TEST_ADD) { | |
99 | /* test adding to the map */ | |
100 | for (j = 0; j < rounds; j++) { | |
6815d114 | 101 | hashmap_init(&map, test_entry_cmp, NULL, 0); |
6a364ced KB |
102 | |
103 | /* add entries */ | |
104 | for (i = 0; i < TEST_SIZE; i++) { | |
105 | hashmap_entry_init(entries[i], hashes[i]); | |
106 | hashmap_add(&map, entries[i]); | |
107 | } | |
108 | ||
109 | hashmap_free(&map, 0); | |
110 | } | |
111 | } else { | |
112 | /* test map lookups */ | |
6815d114 | 113 | hashmap_init(&map, test_entry_cmp, NULL, 0); |
6a364ced KB |
114 | |
115 | /* fill the map (sparsely if specified) */ | |
116 | j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE; | |
117 | for (i = 0; i < j; i++) { | |
118 | hashmap_entry_init(entries[i], hashes[i]); | |
119 | hashmap_add(&map, entries[i]); | |
120 | } | |
121 | ||
122 | for (j = 0; j < rounds; j++) { | |
123 | for (i = 0; i < TEST_SIZE; i++) { | |
ab73a9d1 KB |
124 | hashmap_get_from_hash(&map, hashes[i], |
125 | entries[i]->key); | |
6a364ced KB |
126 | } |
127 | } | |
128 | ||
129 | hashmap_free(&map, 0); | |
130 | } | |
131 | } | |
132 | ||
6a364ced KB |
133 | #define DELIM " \t\r\n" |
134 | ||
135 | /* | |
136 | * Read stdin line by line and print result of commands to stdout: | |
137 | * | |
138 | * hash key -> strhash(key) memhash(key) strihash(key) memihash(key) | |
139 | * put key value -> NULL / old value | |
140 | * get key -> NULL / value | |
141 | * remove key -> NULL / old value | |
142 | * iterate -> key1 value1\nkey2 value2\n... | |
143 | * size -> tablesize numentries | |
144 | * | |
145 | * perfhashmap method rounds -> test hashmap.[ch] performance | |
6a364ced | 146 | */ |
3f2e2297 | 147 | int cmd_main(int argc, const char **argv) |
6a364ced | 148 | { |
7e8089c9 | 149 | struct strbuf line = STRBUF_INIT; |
6a364ced KB |
150 | struct hashmap map; |
151 | int icase; | |
152 | ||
153 | /* init hash map */ | |
154 | icase = argc > 1 && !strcmp("ignorecase", argv[1]); | |
6815d114 | 155 | hashmap_init(&map, test_entry_cmp, &icase, 0); |
6a364ced KB |
156 | |
157 | /* process commands from stdin */ | |
7e8089c9 | 158 | while (strbuf_getline(&line, stdin) != EOF) { |
6a364ced | 159 | char *cmd, *p1 = NULL, *p2 = NULL; |
a6119f82 | 160 | unsigned int hash = 0; |
6a364ced KB |
161 | struct test_entry *entry; |
162 | ||
163 | /* break line into command and up to two parameters */ | |
7e8089c9 | 164 | cmd = strtok(line.buf, DELIM); |
6a364ced KB |
165 | /* ignore empty lines */ |
166 | if (!cmd || *cmd == '#') | |
167 | continue; | |
168 | ||
169 | p1 = strtok(NULL, DELIM); | |
170 | if (p1) { | |
6a364ced KB |
171 | hash = icase ? strihash(p1) : strhash(p1); |
172 | p2 = strtok(NULL, DELIM); | |
6a364ced KB |
173 | } |
174 | ||
7daa825d | 175 | if (!strcmp("hash", cmd) && p1) { |
6a364ced KB |
176 | |
177 | /* print results of different hash functions */ | |
7daa825d JK |
178 | printf("%u %u %u %u\n", |
179 | strhash(p1), memhash(p1, strlen(p1)), | |
180 | strihash(p1), memihash(p1, strlen(p1))); | |
6a364ced | 181 | |
7daa825d | 182 | } else if (!strcmp("add", cmd) && p1 && p2) { |
6a364ced KB |
183 | |
184 | /* create entry with key = p1, value = p2 */ | |
7daa825d | 185 | entry = alloc_test_entry(hash, p1, p2); |
6a364ced KB |
186 | |
187 | /* add to hashmap */ | |
188 | hashmap_add(&map, entry); | |
189 | ||
7daa825d | 190 | } else if (!strcmp("put", cmd) && p1 && p2) { |
6a364ced KB |
191 | |
192 | /* create entry with key = p1, value = p2 */ | |
7daa825d | 193 | entry = alloc_test_entry(hash, p1, p2); |
6a364ced KB |
194 | |
195 | /* add / replace entry */ | |
196 | entry = hashmap_put(&map, entry); | |
197 | ||
198 | /* print and free replaced entry, if any */ | |
199 | puts(entry ? get_value(entry) : "NULL"); | |
200 | free(entry); | |
201 | ||
7daa825d | 202 | } else if (!strcmp("get", cmd) && p1) { |
6a364ced | 203 | |
6a364ced | 204 | /* lookup entry in hashmap */ |
ab73a9d1 | 205 | entry = hashmap_get_from_hash(&map, hash, p1); |
6a364ced KB |
206 | |
207 | /* print result */ | |
208 | if (!entry) | |
209 | puts("NULL"); | |
210 | while (entry) { | |
211 | puts(get_value(entry)); | |
212 | entry = hashmap_get_next(&map, entry); | |
213 | } | |
214 | ||
7daa825d | 215 | } else if (!strcmp("remove", cmd) && p1) { |
6a364ced KB |
216 | |
217 | /* setup static key */ | |
218 | struct hashmap_entry key; | |
219 | hashmap_entry_init(&key, hash); | |
220 | ||
221 | /* remove entry from hashmap */ | |
222 | entry = hashmap_remove(&map, &key, p1); | |
223 | ||
224 | /* print result and free entry*/ | |
225 | puts(entry ? get_value(entry) : "NULL"); | |
226 | free(entry); | |
227 | ||
228 | } else if (!strcmp("iterate", cmd)) { | |
229 | ||
230 | struct hashmap_iter iter; | |
231 | hashmap_iter_init(&map, &iter); | |
232 | while ((entry = hashmap_iter_next(&iter))) | |
233 | printf("%s %s\n", entry->key, get_value(entry)); | |
234 | ||
235 | } else if (!strcmp("size", cmd)) { | |
236 | ||
237 | /* print table sizes */ | |
8b604d19 JH |
238 | printf("%u %u\n", map.tablesize, |
239 | hashmap_get_size(&map)); | |
6a364ced | 240 | |
7daa825d | 241 | } else if (!strcmp("intern", cmd) && p1) { |
7b64d42d KB |
242 | |
243 | /* test that strintern works */ | |
244 | const char *i1 = strintern(p1); | |
245 | const char *i2 = strintern(p1); | |
246 | if (strcmp(i1, p1)) | |
247 | printf("strintern(%s) returns %s\n", p1, i1); | |
248 | else if (i1 == p1) | |
249 | printf("strintern(%s) returns input pointer\n", p1); | |
250 | else if (i1 != i2) | |
251 | printf("strintern(%s) != strintern(%s)", i1, i2); | |
252 | else | |
253 | printf("%s\n", i1); | |
254 | ||
7daa825d | 255 | } else if (!strcmp("perfhashmap", cmd) && p1 && p2) { |
6a364ced KB |
256 | |
257 | perf_hashmap(atoi(p1), atoi(p2)); | |
258 | ||
6a364ced KB |
259 | } else { |
260 | ||
261 | printf("Unknown command %s\n", cmd); | |
262 | ||
263 | } | |
264 | } | |
265 | ||
7e8089c9 | 266 | strbuf_release(&line); |
6a364ced KB |
267 | hashmap_free(&map, 1); |
268 | return 0; | |
269 | } |