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