2018-03-24 07:44:43 +00:00
|
|
|
#include "test-tool.h"
|
2013-12-14 02:06:40 +00:00
|
|
|
#include "git-compat-util.h"
|
2013-11-14 19:17:54 +00:00
|
|
|
#include "hashmap.h"
|
2018-02-14 18:07:19 +00:00
|
|
|
#include "strbuf.h"
|
2023-04-24 22:20:17 +00:00
|
|
|
#include "string-list.h"
|
2013-11-14 19:17:54 +00:00
|
|
|
|
|
|
|
struct test_entry
|
|
|
|
{
|
2019-10-06 23:30:43 +00:00
|
|
|
int padding; /* hashmap entry no longer needs to be the first member */
|
2013-11-14 19:17:54 +00:00
|
|
|
struct hashmap_entry ent;
|
|
|
|
/* key and value as two \0-terminated strings */
|
|
|
|
char key[FLEX_ARRAY];
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *get_value(const struct test_entry *e)
|
|
|
|
{
|
|
|
|
return e->key + strlen(e->key) + 1;
|
|
|
|
}
|
|
|
|
|
2017-07-01 00:28:38 +00:00
|
|
|
static int test_entry_cmp(const void *cmp_data,
|
2019-10-06 23:30:37 +00:00
|
|
|
const struct hashmap_entry *eptr,
|
|
|
|
const struct hashmap_entry *entry_or_key,
|
2017-07-01 00:28:38 +00:00
|
|
|
const void *keydata)
|
2013-11-14 19:17:54 +00:00
|
|
|
{
|
2017-07-01 00:28:38 +00:00
|
|
|
const int ignore_case = cmp_data ? *((int *)cmp_data) : 0;
|
2019-10-06 23:30:37 +00:00
|
|
|
const struct test_entry *e1, *e2;
|
2017-07-01 00:28:38 +00:00
|
|
|
const char *key = keydata;
|
|
|
|
|
2019-10-06 23:30:37 +00:00
|
|
|
e1 = container_of(eptr, const struct test_entry, ent);
|
|
|
|
e2 = container_of(entry_or_key, const struct test_entry, ent);
|
|
|
|
|
2017-07-01 00:28:38 +00:00
|
|
|
if (ignore_case)
|
|
|
|
return strcasecmp(e1->key, key ? key : e2->key);
|
|
|
|
else
|
|
|
|
return strcmp(e1->key, key ? key : e2->key);
|
2013-11-14 19:17:54 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 18:08:57 +00:00
|
|
|
static struct test_entry *alloc_test_entry(unsigned int hash,
|
|
|
|
char *key, char *value)
|
2013-11-14 19:17:54 +00:00
|
|
|
{
|
2018-02-14 18:08:20 +00:00
|
|
|
size_t klen = strlen(key);
|
|
|
|
size_t vlen = strlen(value);
|
2018-02-14 18:06:34 +00:00
|
|
|
struct test_entry *entry = xmalloc(st_add4(sizeof(*entry), klen, vlen, 2));
|
2019-10-06 23:30:27 +00:00
|
|
|
hashmap_entry_init(&entry->ent, hash);
|
2013-11-14 19:17:54 +00:00
|
|
|
memcpy(entry->key, key, klen + 1);
|
|
|
|
memcpy(entry->key + klen + 1, value, vlen + 1);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HASH_METHOD_FNV 0
|
|
|
|
#define HASH_METHOD_I 1
|
|
|
|
#define HASH_METHOD_IDIV10 2
|
|
|
|
#define HASH_METHOD_0 3
|
|
|
|
#define HASH_METHOD_X2 4
|
|
|
|
#define TEST_SPARSE 8
|
|
|
|
#define TEST_ADD 16
|
|
|
|
#define TEST_SIZE 100000
|
|
|
|
|
|
|
|
static unsigned int hash(unsigned int method, unsigned int i, const char *key)
|
|
|
|
{
|
2014-12-09 09:48:36 +00:00
|
|
|
unsigned int hash = 0;
|
2013-11-14 19:17:54 +00:00
|
|
|
switch (method & 3)
|
|
|
|
{
|
|
|
|
case HASH_METHOD_FNV:
|
|
|
|
hash = strhash(key);
|
|
|
|
break;
|
|
|
|
case HASH_METHOD_I:
|
|
|
|
hash = i;
|
|
|
|
break;
|
|
|
|
case HASH_METHOD_IDIV10:
|
|
|
|
hash = i / 10;
|
|
|
|
break;
|
|
|
|
case HASH_METHOD_0:
|
|
|
|
hash = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (method & HASH_METHOD_X2)
|
|
|
|
hash = 2 * hash;
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test performance of hashmap.[ch]
|
2018-03-24 07:44:43 +00:00
|
|
|
* Usage: time echo "perfhashmap method rounds" | test-tool hashmap
|
2013-11-14 19:17:54 +00:00
|
|
|
*/
|
|
|
|
static void perf_hashmap(unsigned int method, unsigned int rounds)
|
|
|
|
{
|
|
|
|
struct hashmap map;
|
|
|
|
char buf[16];
|
|
|
|
struct test_entry **entries;
|
|
|
|
unsigned int *hashes;
|
|
|
|
unsigned int i, j;
|
|
|
|
|
2018-02-14 18:05:46 +00:00
|
|
|
ALLOC_ARRAY(entries, TEST_SIZE);
|
|
|
|
ALLOC_ARRAY(hashes, TEST_SIZE);
|
2013-11-14 19:17:54 +00:00
|
|
|
for (i = 0; i < TEST_SIZE; i++) {
|
2018-02-14 18:06:57 +00:00
|
|
|
xsnprintf(buf, sizeof(buf), "%i", i);
|
2018-02-14 18:08:20 +00:00
|
|
|
entries[i] = alloc_test_entry(0, buf, "");
|
2013-11-14 19:17:54 +00:00
|
|
|
hashes[i] = hash(method, i, entries[i]->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (method & TEST_ADD) {
|
|
|
|
/* test adding to the map */
|
|
|
|
for (j = 0; j < rounds; j++) {
|
2017-07-01 00:28:38 +00:00
|
|
|
hashmap_init(&map, test_entry_cmp, NULL, 0);
|
2013-11-14 19:17:54 +00:00
|
|
|
|
|
|
|
/* add entries */
|
|
|
|
for (i = 0; i < TEST_SIZE; i++) {
|
2019-10-06 23:30:27 +00:00
|
|
|
hashmap_entry_init(&entries[i]->ent, hashes[i]);
|
2019-10-06 23:30:29 +00:00
|
|
|
hashmap_add(&map, &entries[i]->ent);
|
2013-11-14 19:17:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 18:55:05 +00:00
|
|
|
hashmap_clear(&map);
|
2013-11-14 19:17:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* test map lookups */
|
2017-07-01 00:28:38 +00:00
|
|
|
hashmap_init(&map, test_entry_cmp, NULL, 0);
|
2013-11-14 19:17:54 +00:00
|
|
|
|
|
|
|
/* fill the map (sparsely if specified) */
|
|
|
|
j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
|
|
|
|
for (i = 0; i < j; i++) {
|
2019-10-06 23:30:27 +00:00
|
|
|
hashmap_entry_init(&entries[i]->ent, hashes[i]);
|
2019-10-06 23:30:29 +00:00
|
|
|
hashmap_add(&map, &entries[i]->ent);
|
2013-11-14 19:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < rounds; j++) {
|
|
|
|
for (i = 0; i < TEST_SIZE; i++) {
|
2014-07-02 22:22:11 +00:00
|
|
|
hashmap_get_from_hash(&map, hashes[i],
|
|
|
|
entries[i]->key);
|
2013-11-14 19:17:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 18:55:05 +00:00
|
|
|
hashmap_clear(&map);
|
2013-11-14 19:17:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DELIM " \t\r\n"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read stdin line by line and print result of commands to stdout:
|
|
|
|
*
|
|
|
|
* hash key -> strhash(key) memhash(key) strihash(key) memihash(key)
|
|
|
|
* put key value -> NULL / old value
|
|
|
|
* get key -> NULL / value
|
|
|
|
* remove key -> NULL / old value
|
|
|
|
* iterate -> key1 value1\nkey2 value2\n...
|
|
|
|
* size -> tablesize numentries
|
|
|
|
*
|
|
|
|
* perfhashmap method rounds -> test hashmap.[ch] performance
|
|
|
|
*/
|
2018-03-24 07:44:43 +00:00
|
|
|
int cmd__hashmap(int argc, const char **argv)
|
2013-11-14 19:17:54 +00:00
|
|
|
{
|
2023-04-24 22:20:17 +00:00
|
|
|
struct string_list parts = STRING_LIST_INIT_NODUP;
|
2018-02-14 18:07:19 +00:00
|
|
|
struct strbuf line = STRBUF_INIT;
|
2013-11-14 19:17:54 +00:00
|
|
|
int icase;
|
2020-11-11 20:02:20 +00:00
|
|
|
struct hashmap map = HASHMAP_INIT(test_entry_cmp, &icase);
|
2013-11-14 19:17:54 +00:00
|
|
|
|
|
|
|
/* init hash map */
|
|
|
|
icase = argc > 1 && !strcmp("ignorecase", argv[1]);
|
|
|
|
|
|
|
|
/* process commands from stdin */
|
2018-02-14 18:07:19 +00:00
|
|
|
while (strbuf_getline(&line, stdin) != EOF) {
|
2023-04-24 22:20:17 +00:00
|
|
|
char *cmd, *p1, *p2;
|
2018-02-14 18:08:57 +00:00
|
|
|
unsigned int hash = 0;
|
2013-11-14 19:17:54 +00:00
|
|
|
struct test_entry *entry;
|
|
|
|
|
|
|
|
/* break line into command and up to two parameters */
|
2023-04-24 22:20:17 +00:00
|
|
|
string_list_setlen(&parts, 0);
|
|
|
|
string_list_split_in_place(&parts, line.buf, DELIM, 2);
|
|
|
|
string_list_remove_empty_items(&parts, 0);
|
|
|
|
|
2013-11-14 19:17:54 +00:00
|
|
|
/* ignore empty lines */
|
2023-04-24 22:20:17 +00:00
|
|
|
if (!parts.nr)
|
|
|
|
continue;
|
|
|
|
if (!*parts.items[0].string || *parts.items[0].string == '#')
|
2013-11-14 19:17:54 +00:00
|
|
|
continue;
|
|
|
|
|
2023-04-24 22:20:17 +00:00
|
|
|
cmd = parts.items[0].string;
|
|
|
|
p1 = parts.nr >= 1 ? parts.items[1].string : NULL;
|
|
|
|
p2 = parts.nr >= 2 ? parts.items[2].string : NULL;
|
|
|
|
if (p1)
|
2013-11-14 19:17:54 +00:00
|
|
|
hash = icase ? strihash(p1) : strhash(p1);
|
|
|
|
|
2019-06-15 10:07:02 +00:00
|
|
|
if (!strcmp("add", cmd) && p1 && p2) {
|
2013-11-14 19:17:54 +00:00
|
|
|
|
|
|
|
/* create entry with key = p1, value = p2 */
|
2018-02-14 18:08:20 +00:00
|
|
|
entry = alloc_test_entry(hash, p1, p2);
|
2013-11-14 19:17:54 +00:00
|
|
|
|
|
|
|
/* add to hashmap */
|
2019-10-06 23:30:29 +00:00
|
|
|
hashmap_add(&map, &entry->ent);
|
2013-11-14 19:17:54 +00:00
|
|
|
|
2018-02-14 18:08:20 +00:00
|
|
|
} else if (!strcmp("put", cmd) && p1 && p2) {
|
2013-11-14 19:17:54 +00:00
|
|
|
|
|
|
|
/* create entry with key = p1, value = p2 */
|
2018-02-14 18:08:20 +00:00
|
|
|
entry = alloc_test_entry(hash, p1, p2);
|
2013-11-14 19:17:54 +00:00
|
|
|
|
|
|
|
/* add / replace entry */
|
2019-10-06 23:30:42 +00:00
|
|
|
entry = hashmap_put_entry(&map, entry, ent);
|
2013-11-14 19:17:54 +00:00
|
|
|
|
|
|
|
/* print and free replaced entry, if any */
|
|
|
|
puts(entry ? get_value(entry) : "NULL");
|
|
|
|
free(entry);
|
|
|
|
|
2018-02-14 18:08:20 +00:00
|
|
|
} else if (!strcmp("get", cmd) && p1) {
|
2013-11-14 19:17:54 +00:00
|
|
|
/* lookup entry in hashmap */
|
2019-10-06 23:30:35 +00:00
|
|
|
entry = hashmap_get_entry_from_hash(&map, hash, p1,
|
|
|
|
struct test_entry, ent);
|
2013-11-14 19:17:54 +00:00
|
|
|
|
|
|
|
/* print result */
|
2019-10-06 23:30:35 +00:00
|
|
|
if (!entry)
|
2013-11-14 19:17:54 +00:00
|
|
|
puts("NULL");
|
2019-10-06 23:30:41 +00:00
|
|
|
hashmap_for_each_entry_from(&map, entry, ent)
|
2013-11-14 19:17:54 +00:00
|
|
|
puts(get_value(entry));
|
|
|
|
|
2018-02-14 18:08:20 +00:00
|
|
|
} else if (!strcmp("remove", cmd) && p1) {
|
2013-11-14 19:17:54 +00:00
|
|
|
|
|
|
|
/* setup static key */
|
|
|
|
struct hashmap_entry key;
|
2019-10-06 23:30:39 +00:00
|
|
|
struct hashmap_entry *rm;
|
2013-11-14 19:17:54 +00:00
|
|
|
hashmap_entry_init(&key, hash);
|
|
|
|
|
|
|
|
/* remove entry from hashmap */
|
2019-10-06 23:30:39 +00:00
|
|
|
rm = hashmap_remove(&map, &key, p1);
|
|
|
|
entry = rm ? container_of(rm, struct test_entry, ent)
|
|
|
|
: NULL;
|
2013-11-14 19:17:54 +00:00
|
|
|
|
|
|
|
/* print result and free entry*/
|
|
|
|
puts(entry ? get_value(entry) : "NULL");
|
|
|
|
free(entry);
|
|
|
|
|
|
|
|
} else if (!strcmp("iterate", cmd)) {
|
|
|
|
struct hashmap_iter iter;
|
2019-10-06 23:30:38 +00:00
|
|
|
|
|
|
|
hashmap_for_each_entry(&map, &iter, entry,
|
|
|
|
ent /* member name */)
|
2013-11-14 19:17:54 +00:00
|
|
|
printf("%s %s\n", entry->key, get_value(entry));
|
|
|
|
|
|
|
|
} else if (!strcmp("size", cmd)) {
|
|
|
|
|
|
|
|
/* print table sizes */
|
2017-09-06 15:43:48 +00:00
|
|
|
printf("%u %u\n", map.tablesize,
|
|
|
|
hashmap_get_size(&map));
|
2013-11-14 19:17:54 +00:00
|
|
|
|
2018-02-14 18:08:20 +00:00
|
|
|
} else if (!strcmp("intern", cmd) && p1) {
|
2014-07-02 22:22:54 +00:00
|
|
|
|
|
|
|
/* test that strintern works */
|
|
|
|
const char *i1 = strintern(p1);
|
|
|
|
const char *i2 = strintern(p1);
|
|
|
|
if (strcmp(i1, p1))
|
|
|
|
printf("strintern(%s) returns %s\n", p1, i1);
|
|
|
|
else if (i1 == p1)
|
|
|
|
printf("strintern(%s) returns input pointer\n", p1);
|
|
|
|
else if (i1 != i2)
|
|
|
|
printf("strintern(%s) != strintern(%s)", i1, i2);
|
|
|
|
else
|
|
|
|
printf("%s\n", i1);
|
|
|
|
|
2018-02-14 18:08:20 +00:00
|
|
|
} else if (!strcmp("perfhashmap", cmd) && p1 && p2) {
|
2013-11-14 19:17:54 +00:00
|
|
|
|
|
|
|
perf_hashmap(atoi(p1), atoi(p2));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
printf("Unknown command %s\n", cmd);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 22:20:17 +00:00
|
|
|
string_list_clear(&parts, 0);
|
2018-02-14 18:07:19 +00:00
|
|
|
strbuf_release(&line);
|
2020-11-02 18:55:05 +00:00
|
|
|
hashmap_clear_and_free(&map, struct test_entry, ent);
|
2013-11-14 19:17:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|