diff options
author | Chandler Carruth <chandlerc@gmail.com> | 2012-06-17 09:05:09 +0000 |
---|---|---|
committer | Chandler Carruth <chandlerc@gmail.com> | 2012-06-17 09:05:09 +0000 |
commit | dd9d38d57bbd2161e04af90a9e03011afb039b16 (patch) | |
tree | eb47df63f55d6d7cfca226b5b2669f0b83199e20 /unittests/ADT | |
parent | f445be8958014c85eb39e07b1e0a389ea9522230 (diff) |
Introduce a SmallDenseMap container that re-uses the existing DenseMap
implementation.
This type includes an inline bucket array which is used initially. Once
it is exceeded, an array of 64 buckets is allocated on the heap. The
bucket count grows from there as needed. Some highlights of this
implementation:
- The inline buffer is very carefully aligned, and so supports types
with alignment constraints.
- It works hard to avoid aliasing issues.
- Supports types with non-trivial constructors, destructors, copy
constructions, etc. It works reasonably hard to minimize copies and
unnecessary initialization. The most common initialization is to set
keys to the empty key, and so that should be fast if at all possible.
This class has a performance / space trade-off. It tries to optimize for
relatively small maps, and so packs the inline bucket array densely into
the object. It will be marginally slower than a normal DenseMap in a few
use patterns, so it isn't appropriate everywhere.
The unit tests for DenseMap have been generalized a bit to support
running over different map implementations in addition to different
key/value types. They've then been automatically extended to cover the
new container through the magic of GoogleTest's typed tests.
All of this is still a bit rough though. I'm going to be cleaning up
some aspects of the implementation, documenting things better, and
adding tests which include non-trivial types. As soon as I'm comfortable
with the correctness, I plan to switch existing users of SmallMap over
to this class as it is already more correct w.r.t. construction and
destruction of objects iin the map.
Thanks to Benjamin Kramer for all the reviews of this and the lead-up
patches. That said, more review on this would really be appreciated. As
I've noted a few times, I'm quite surprised how hard it is to get the
semantics for a hashtable-based map container with a small buffer
optimization correct. =]
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@158638 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/ADT')
-rw-r--r-- | unittests/ADT/DenseMapTest.cpp | 62 |
1 files changed, 35 insertions, 27 deletions
diff --git a/unittests/ADT/DenseMapTest.cpp b/unittests/ADT/DenseMapTest.cpp index 3fe35c91ed..d61f991bd5 100644 --- a/unittests/ADT/DenseMapTest.cpp +++ b/unittests/ADT/DenseMapTest.cpp @@ -15,43 +15,51 @@ using namespace llvm; namespace { -// Test fixture -template <typename T> -class DenseMapTest : public testing::Test { -protected: - T Map; - - typename T::key_type getKey(int i = 0); - typename T::mapped_type getValue(int i = 0); -}; - -template <> -uint32_t DenseMapTest<DenseMap<uint32_t, uint32_t> >::getKey(int i) { - return i; -} +uint32_t getTestKey(int i, uint32_t *) { return i; } +uint32_t getTestValue(int i, uint32_t *) { return 42 + i; } -template <> -uint32_t DenseMapTest<DenseMap<uint32_t, uint32_t> >::getValue(int i) { - return 42 + i; +uint32_t *getTestKey(int i, uint32_t **) { + static uint32_t dummy_arr1[8192]; + assert(i < 8192 && "Only support 8192 dummy keys."); + return &dummy_arr1[i]; } - -template <> -uint32_t *DenseMapTest<DenseMap<uint32_t *, uint32_t *> >::getKey(int i) { +uint32_t *getTestValue(int i, uint32_t **) { static uint32_t dummy_arr1[8192]; assert(i < 8192 && "Only support 8192 dummy keys."); return &dummy_arr1[i]; } -template <> -uint32_t *DenseMapTest<DenseMap<uint32_t *, uint32_t *> >::getValue(int i) { - static uint32_t dummy_arr2[8192]; - assert(i < 8192 && "Only support 8192 dummy values."); - return &dummy_arr2[i]; -} +// Test fixture, with helper functions implemented by forwarding to global +// function overloads selected by component types of the type parameter. This +// allows all of the map implementations to be tested with shared +// implementations of helper routines. +template <typename T> +class DenseMapTest : public ::testing::Test { +protected: + T Map; + + static typename T::key_type *const dummy_key_ptr; + static typename T::mapped_type *const dummy_value_ptr; + + typename T::key_type getKey(int i = 0) { + return getTestKey(i, dummy_key_ptr); + } + typename T::mapped_type getValue(int i = 0) { + return getTestValue(i, dummy_value_ptr); + } +}; + +template <typename T> +typename T::key_type *const DenseMapTest<T>::dummy_key_ptr = 0; +template <typename T> +typename T::mapped_type *const DenseMapTest<T>::dummy_value_ptr = 0; // Register these types for testing. typedef ::testing::Types<DenseMap<uint32_t, uint32_t>, - DenseMap<uint32_t *, uint32_t *> > DenseMapTestTypes; + DenseMap<uint32_t *, uint32_t *>, + SmallDenseMap<uint32_t, uint32_t>, + SmallDenseMap<uint32_t *, uint32_t *> + > DenseMapTestTypes; TYPED_TEST_CASE(DenseMapTest, DenseMapTestTypes); // Empty map tests |