diff options
author | Karsten Blees <karsten.blees@gmail.com> | 2014-07-03 02:22:54 +0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2014-07-08 00:56:38 +0400 |
commit | 7b64d42d22206d9995a8f0cb3b515e623cac4702 (patch) | |
tree | 26895c5fde113b1d84f52409f951feb37f451412 /test-hashmap.c | |
parent | ab73a9d119240b0b908ccb9edd19b8e536ce29b9 (diff) |
hashmap: add string interning API
Interning short strings with high probability of duplicates can reduce the
memory footprint and speed up comparisons.
Add strintern() and memintern() APIs that use a hashmap to manage the pool
of unique, interned strings.
Note: strintern(getenv()) could be used to sanitize git's use of getenv(),
in case we ever encounter a platform where a call to getenv() invalidates
previous getenv() results (which is allowed by POSIX).
Signed-off-by: Karsten Blees <blees@dcon.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'test-hashmap.c')
-rw-r--r-- | test-hashmap.c | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/test-hashmap.c b/test-hashmap.c index 3c9f67bcb27..07aa7ecdeed 100644 --- a/test-hashmap.c +++ b/test-hashmap.c @@ -234,6 +234,20 @@ int main(int argc, char *argv[]) /* print table sizes */ printf("%u %u\n", map.tablesize, map.size); + } else if (!strcmp("intern", cmd) && l1) { + + /* 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); + } else if (!strcmp("perfhashmap", cmd) && l1 && l2) { perf_hashmap(atoi(p1), atoi(p2)); |