Passes values by reference when sorting to reduce memory copying. The main saving on this is preventing copying of the large vector of files getting passed into the `sort` function and then copying each item in the vector in the for. This results in about a 27% performance gain reducing the full benchmark of the kubernetes benchmark by 7.89 sec Before | Name | Total | Average | Min | Max | | ---------------------------- | ------------- | ------------- | ------------- | ------------- | | ivy_match(file.lua) 1000000x | 02.351614 (s) | 00.000002 (s) | 00.000002 (s) | 00.000042 (s) | | ivy_files(kubernetes) 100x | 32.704256 (s) | 00.327043 (s) | 00.289397 (s) | 00.344413 (s) | After | Name | Total | Average | Min | Max | | ---------------------------- | ------------- | ------------- | ------------- | ------------- | | ivy_match(file.lua) 1000000x | 02.353386 (s) | 00.000002 (s) | 00.000002 (s) | 00.000049 (s) | | ivy_files(kubernetes) 100x | 24.809576 (s) | 00.248096 (s) | 00.203167 (s) | 00.270263 (s) |
45 lines
1 KiB
C++
45 lines
1 KiB
C++
#pragma once
|
|
|
|
#include "./fuzzy_match.hpp"
|
|
#include "./match.hpp"
|
|
#include "./thread_pool.hpp"
|
|
|
|
namespace ivy {
|
|
|
|
class Sorter {
|
|
ivy::ThreadPool m_thread_pool;
|
|
|
|
std::string_view m_term;
|
|
|
|
std::mutex m_matches_lock;
|
|
std::vector<Match> m_matches;
|
|
|
|
inline void add_entry(const std::string& file) {
|
|
ivy::FuzzyMatcher matcher(m_term, 0);
|
|
int score = matcher.match(file, false);
|
|
|
|
if (score > -200) {
|
|
std::unique_lock<std::mutex> lock(m_matches_lock);
|
|
m_matches.emplace_back(Match{score, std::move(file)});
|
|
}
|
|
}
|
|
|
|
public:
|
|
explicit Sorter(std::string_view term) : m_term(term) {}
|
|
~Sorter() { m_thread_pool.shutdown(); }
|
|
|
|
inline std::vector<Match> sort(const std::vector<std::string>& list) {
|
|
for (const std::string& item : list) {
|
|
m_thread_pool.push([&item, this]() { add_entry(item); });
|
|
}
|
|
|
|
while (!m_thread_pool.empty()) {
|
|
// Wait for all of the jobs to be finished
|
|
}
|
|
|
|
std::sort(m_matches.begin(), m_matches.end(), sort_match);
|
|
return m_matches;
|
|
}
|
|
};
|
|
|
|
} // namespace ivy
|