Pre-size scores in find_perfect_hash_seed

This improves startup of a small Python file by 1.35% (instruction count
from Callgrind) for me.
This commit is contained in:
Max Bernstein 2023-02-24 10:45:33 -05:00
parent 1ea7c151a3
commit 15758d448d

View File

@ -52,7 +52,7 @@ namespace pkpy{
uint32_t find_perfect_hash_seed(uint32_t capacity, const std::vector<StrName>& keys){
if(keys.empty()) return kHashSeeds[0];
std::set<uint32_t> indices;
std::vector<std::pair<uint32_t, float>> scores;
std::vector<std::pair<uint32_t, float>> scores(kHashSeeds.size());
for(int i=0; i<kHashSeeds.size(); i++){
indices.clear();
for(auto key: keys){
@ -60,7 +60,7 @@ namespace pkpy{
indices.insert(index);
}
float score = indices.size() / (float)keys.size();
scores.push_back({kHashSeeds[i], score});
scores[i] = {kHashSeeds[i], score};
}
std::sort(scores.begin(), scores.end(), [](auto a, auto b){ return a.second > b.second; });
return scores[0].first;