strongswan/src/libstrongswan/collections/hashtable_profiler.h

120 lines
3.1 KiB
C
Raw Normal View History

hashtable: Maintain insertion order when enumerating With the previous approach we'd require at least an additional pointer per item to store them in a list (15-18% increase in the overhead per item). Instead we switch from handling collisions with overflow lists to an open addressing scheme and store the actual table as variable-sized indices pointing into an array of all inserted items in their original order. This can reduce the memory overhead even compared to the previous implementation (especially for smaller tables), but because the array for items is preallocated whenever the table is resized, it can be worse for certain numbers of items. However, avoiding all the allocations required by the previous design is actually a big advantage. Depending on the usage pattern, the performance can improve quite a bit (in particular when inserting many items). The raw lookup performance is a bit slower as probing lengths increase with open addressing, but there are some caching benefits due to the compact storage. So for general usage the performance should be better. For instance, one test I did was counting the occurrences of words in a list of 1'000'000 randomly selected words from a dictionary of ~58'000 words (i.e. using a counter stored under each word as key). The new implementation was ~8% faster on average while requiring 10% less memory. Since we can't remove items from the array (would change the indices of all items that follow it) we just mark them as removed and remove them once the hash table is resized/rehashed (the cells in the hash table for these may be reused). Due to this the latter may also happen if the number of stored items does not increase e.g. after a series of remove/put operations (each insertion requires storage in the array, no matter if items were removed). So if the capacity is exhausted, the table is resized/rehashed (after lots of removals the size may even be reduced) and all items marked as removed are simply skipped. Compared to the previous implementation the load factor/capacity is lowered to reduce chances of collisions and to avoid primary clustering to some degree. However, the latter in particular, but the open addressing scheme in general, make this implementation completely unsuited for the get_match() functionality (purposefully hashing to the same value and, therefore, increasing the probing length and clustering). And keeping the keys optionally sorted would complicate the code significantly. So we just keep the existing hashlist_t implementation without adding code to maintain the overall insertion order (we could add that feature optionally later, but with the mentioned overhead for one or two pointers). The maximum size is currently not changed. With the new implementation this translates to a hard limit for the maximum number of items that can be held in the table (=CAPACITY(MAX_SIZE)). Since this equals 715'827'882 items with the current settings, this shouldn't be a problem in practice, the table alone would require 20 GiB in memory for that many items. The hashlist_t implementation doesn't have that limitation due to the overflow lists (it can store beyond it's capacity) but it itself would require over 29 GiB of memory to hold that many items.
2020-04-24 13:51:17 +00:00
/*
* Copyright (C) 2020 Tobias Brunner
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#ifndef HASHTABLE_PROFILER_H_
#define HASHTABLE_PROFILER_H_
#ifdef HASHTABLE_PROFILER
#include <time.h>
#include <utils/backtrace.h>
typedef struct hashtable_profile_t hashtable_profile_t;
struct hashtable_profile_t {
/**
* Some stats to profile lookups in the table
*/
struct {
size_t count;
size_t probes;
size_t longest;
} success, failure;
/**
* Stats on the memory usage of the table
*/
struct {
size_t count;
size_t size;
} max;
/**
* Keep track of where the hash table was created
*/
backtrace_t *backtrace;
};
/**
* Print and cleanup profiling data
*/
static inline void profiler_cleanup(hashtable_profile_t *profile, u_int count,
u_int size)
{
if (profile->success.count || profile->failure.count)
{
fprintf(stderr, "%zu elements [max. %zu], %zu buckets [%zu], %zu "
"successful / %zu failed lookups, %.4f [%zu] / %.4f "
"[%zu] avg. probes in table created at:",
count, profile->max.count, size, profile->max.size,
profile->success.count, profile->failure.count,
(double)profile->success.probes/profile->success.count,
profile->success.longest,
(double)profile->failure.probes/profile->failure.count,
profile->failure.longest);
profile->backtrace->log(profile->backtrace, stderr, TRUE);
}
profile->backtrace->destroy(profile->backtrace);
}
/**
* Initialize profiling data
*/
static inline void profiler_init(hashtable_profile_t *profile, int skip)
{
profile->backtrace = backtrace_create(skip);
}
#define lookup_start() \
u_int _lookup_probes = 0;
#define lookup_probing() \
_lookup_probes++;
#define _lookup_done(profile, result) \
(profile)->result.count++; \
(profile)->result.probes += _lookup_probes; \
(profile)->result.longest = max((profile)->result.longest, _lookup_probes);
#define lookup_success(profile) _lookup_done(profile, success);
#define lookup_failure(profile) _lookup_done(profile, failure);
static inline void profile_size(hashtable_profile_t *profile, u_int size)
{
profile->max.size = max(profile->max.size, size);
}
static inline void profile_count(hashtable_profile_t *profile, u_int count)
{
profile->max.count = max(profile->max.count, count);
}
#else /* !HASHTABLE_PROFILER */
#define hashtable_profile_t struct {}
#define profiler_cleanup(...) {}
#define profiler_init(...) {}
#define lookup_start(...) {}
#define lookup_probing(...) {}
#define lookup_success(...) {}
#define lookup_failure(...) {}
#define profile_size(...) {}
#define profile_count(...) {}
#endif /* HASHTABLE_PROFILER */
#endif /* HASHTABLE_PROFILER_H_ */