SmallSetVector has an inefficiency where it does set insertions
regardless of the number of elements present within it. This contrasts
with other "Small-" containers where they use linear scan up to a
certain size "N", after which they switch to another strategy.
This patch implements this functionality in SetVector, adding a template
parameter "N" which specifies the number of elements upto which the
SetVector follows the "small" strategy. Due to the use of "if
constexpr", there is no "small" code emitted when N is 0 which makes
this a zero overhead change for users using the default behaviour.
This change also allows having SmallSetVector use DenseSet instead of
SmallDenseSet by default, which helps a little with performance.
The reason for implementing this functionality in SetVector instead of
SmallSetVector is that it allows reusing all the code that is already
there and it is just augmented with the "isSmall" checks.
This change gives a good speedup (0.4%):
https://llvm-compile-time-tracker.com/compare.php?from=086601eac266ec253bf313c746390ff3e5656132&to=acd0a72a4d3ee840f7b455d1b35d82b11ffdb3c0&stat=instructions%3Au
It might make sense to replicate this static_assert from SmallPtrSet: https://github.com/llvm/llvm-project/blob/ecbd37d5a336ec8a8adafae5a8e4263bb738718c/llvm/include/llvm/ADT/SmallPtrSet.h#L451-L454
To make sure people don't shoot themselves in the foot with a nice SmallSetVector<T, 512>.
From a quick grep, this is the only usage that would have to be adjusted: https://github.com/llvm/llvm-project/blob/ecbd37d5a336ec8a8adafae5a8e4263bb738718c/flang/lib/Lower/Bridge.cpp#L190