SmallSet/SmallPtrSet: Refuse huge Small numbers

These sets do linear searching in small mode; It is not a good idea to
use huge numbers as the small value here, save people from themselves by
adding a static_assert.

Differential Revision: http://reviews.llvm.org/D16706

llvm-svn: 259419
This commit is contained in:
Matthias Braun 2016-02-01 22:05:16 +00:00
parent d8da15f8a9
commit 3f88eabe93
3 changed files with 12 additions and 2 deletions

View File

@ -329,6 +329,11 @@ public:
/// SmallPtrSetImplBase for details of the algorithm.
template<class PtrType, unsigned SmallSize>
class SmallPtrSet : public SmallPtrSetImpl<PtrType> {
// In small mode SmallPtrSet uses linear search for the elements, so it is
// not a good idea to choose this value too high. You may consider using a
// DenseSet<> instead if you expect many elements in the set.
static_assert(SmallSize <= 32, "SmallSize should be small");
typedef SmallPtrSetImpl<PtrType> BaseT;
// Make sure that SmallSize is a power of two, round up if not.

View File

@ -38,6 +38,11 @@ class SmallSet {
typedef typename SmallVector<T, N>::const_iterator VIterator;
typedef typename SmallVector<T, N>::iterator mutable_iterator;
// In small mode SmallPtrSet uses linear search for the elements, so it is
// not a good idea to choose this value too high. You may consider using a
// DenseSet<> instead if you expect many elements in the set.
static_assert(N <= 32, "N should be small");
public:
typedef size_t size_type;
SmallSet() {}

View File

@ -1651,8 +1651,8 @@ void CppWriter::printFunctionUses(const Function* F) {
// Print type definitions for every type referenced by an instruction and
// make a note of any global values or constants that are referenced
SmallPtrSet<GlobalValue*,64> gvs;
SmallPtrSet<Constant*,64> consts;
SmallPtrSet<GlobalValue*,32> gvs;
SmallPtrSet<Constant*,32> consts;
for (Function::const_iterator BB = F->begin(), BE = F->end();
BB != BE; ++BB){
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();