Cleanup: Fix macOS build error, move set performance test out of header

Fixes, similar to a5a72019a910952ac082e9613e95d0c672cf2964

Pull Request: https://projects.blender.org/blender/blender/pulls/110017
This commit is contained in:
Hans Goudey 2023-07-12 18:04:13 +02:00 committed by Hans Goudey
parent 75322803df
commit 9a879b27b3
3 changed files with 82 additions and 85 deletions

@ -9,6 +9,7 @@
#include <algorithm>
#include <cassert>
#include <unordered_map>
#ifdef WITH_TBB
# include <tbb/parallel_for.h>

@ -45,8 +45,6 @@
* memory usage of the set.
* - The method names don't follow the std::unordered_set names in many cases. Searching for such
* names in this file will usually let you discover the new name.
* - There is a #StdUnorderedSetWrapper class, that wraps std::unordered_set and gives it the same
* interface as blender::Set. This is useful for bench-marking.
*
* Possible Improvements:
* - Use a branch-less loop over slots in grow function (measured ~10% performance improvement when
@ -57,8 +55,6 @@
* to make a nice interface for this functionality.
*/
#include <unordered_set>
#include "BLI_array.hh"
#include "BLI_hash.hh"
#include "BLI_hash_tables.hh"
@ -889,87 +885,6 @@ class Set {
}
};
/**
* A wrapper for std::unordered_set with the API of blender::Set. This can be used for
* benchmarking.
*/
template<typename Key> class StdUnorderedSetWrapper {
private:
using SetType = std::unordered_set<Key, blender::DefaultHash<Key>>;
SetType set_;
public:
int64_t size() const
{
return int64_t(set_.size());
}
bool is_empty() const
{
return set_.empty();
}
void reserve(int64_t n)
{
set_.reserve(n);
}
void add_new(const Key &key)
{
set_.insert(key);
}
void add_new(Key &&key)
{
set_.insert(std::move(key));
}
bool add(const Key &key)
{
return set_.insert(key).second;
}
bool add(Key &&key)
{
return set_.insert(std::move(key)).second;
}
void add_multiple(Span<Key> keys)
{
for (const Key &key : keys) {
set_.insert(key);
}
}
bool contains(const Key &key) const
{
return set_.find(key) != set_.end();
}
bool remove(const Key &key)
{
return bool(set_.erase(key));
}
void remove_contained(const Key &key)
{
return set_.erase(key);
}
void clear()
{
set_.clear();
}
typename SetType::iterator begin() const
{
return set_.begin();
}
typename SetType::iterator end() const
{
return set_.end();
}
};
/**
* Same as a normal Set, but does not use Blender's guarded allocator. This is useful when
* allocating memory with static storage duration.

@ -663,6 +663,87 @@ BLI_NOINLINE void benchmark_random_ints(StringRef name, int amount, int factor)
std::cout << "Count: " << count << "\n";
}
/**
* A wrapper for std::unordered_set with the API of blender::Set. This can be used for
* benchmarking.
*/
template<typename Key> class StdUnorderedSetWrapper {
private:
using SetType = std::unordered_set<Key, blender::DefaultHash<Key>>;
SetType set_;
public:
int64_t size() const
{
return int64_t(set_.size());
}
bool is_empty() const
{
return set_.empty();
}
void reserve(int64_t n)
{
set_.reserve(n);
}
void add_new(const Key &key)
{
set_.insert(key);
}
void add_new(Key &&key)
{
set_.insert(std::move(key));
}
bool add(const Key &key)
{
return set_.insert(key).second;
}
bool add(Key &&key)
{
return set_.insert(std::move(key)).second;
}
void add_multiple(Span<Key> keys)
{
for (const Key &key : keys) {
set_.insert(key);
}
}
bool contains(const Key &key) const
{
return set_.find(key) != set_.end();
}
bool remove(const Key &key)
{
return bool(set_.erase(key));
}
void remove_contained(const Key &key)
{
return set_.erase(key);
}
void clear()
{
set_.clear();
}
typename SetType::iterator begin() const
{
return set_.begin();
}
typename SetType::iterator end() const
{
return set_.end();
}
};
TEST(set, Benchmark)
{
for (int i = 0; i < 3; i++) {