From 4cdccf26659e2463f0c1e06da20cb21ea612b391 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Tue, 6 Sep 2011 10:22:21 +0000 Subject: [PATCH] re PR libstdc++/50257 ([C++0x] unordered_map slow initialization due to huge __prime_list) 2011-09-06 Paolo Carlini PR libstdc++/50257 * include/bits/hashtable_policy.h (_Prime_rehash_policy:: _M_next_bkt): Optimize for small argument. From-SVN: r178581 --- libstdc++-v3/ChangeLog | 6 ++++++ libstdc++-v3/include/bits/hashtable_policy.h | 11 +++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 5d34b2b419f..d0c1b11c4eb 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,9 @@ +2011-09-06 Paolo Carlini + + PR libstdc++/50257 + * include/bits/hashtable_policy.h (_Prime_rehash_policy:: + _M_next_bkt): Optimize for small argument. + 2011-09-02 François Dumont * testsuite/util/testsuite_allocator.h (tracker_allocator_counter:: diff --git a/libstdc++-v3/include/bits/hashtable_policy.h b/libstdc++-v3/include/bits/hashtable_policy.h index ab34463475b..08a6dcd39a2 100644 --- a/libstdc++-v3/include/bits/hashtable_policy.h +++ b/libstdc++-v3/include/bits/hashtable_policy.h @@ -427,8 +427,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Prime_rehash_policy:: _M_next_bkt(std::size_t __n) const { - const unsigned long __p = *std::lower_bound(__prime_list, __prime_list - + _S_n_primes, __n); + // Optimize lookups involving the first elements of __prime_list. + // (useful to speed-up, eg, constructors) + static const unsigned char __fastbkt[12] + = { 2, 2, 2, 3, 5, 5, 7, 7, 11, 11, 11, 11 }; + + const unsigned long __p + = __n <= 11 ? __fastbkt[__n] + : *std::lower_bound(__prime_list + 5, + __prime_list + _S_n_primes, __n); _M_next_resize = static_cast(__builtin_floor(__p * _M_max_load_factor)); return __p;