From d7df043eb98b07bcec1ad97a09f50ba8e97af306 Mon Sep 17 00:00:00 2001 From: Ade Attwood Date: Sun, 7 Aug 2022 16:48:48 +0100 Subject: [PATCH] perf: reduce copying when sorting 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) | --- README.md | 4 ++-- cpp/sorter.hpp | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a42055e..c4b5903 100644 --- a/README.md +++ b/README.md @@ -99,8 +99,8 @@ Current benchmark status with 8 CPU(s) Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz | Name | Total | Adverage | Min | Max | | ---------------------------- | ------------- | ------------- | ------------- | ------------- | -| ivy_match(file.lua) 1000000x | 02.351614 (s) | 00.000002 (s) | 00.000002 (s) | 00.000042 (s) | -| ivy_files(kubneties) 100x | 32.704256 (s) | 00.327043 (s) | 00.289397 (s) | 00.344413 (s) | +| ivy_match(file.lua) 1000000x | 02.353386 (s) | 00.000002 (s) | 00.000002 (s) | 00.000049 (s) | +| ivy_files(kubneties) 100x | 24.809576 (s) | 00.248096 (s) | 00.203167 (s) | 00.270263 (s) | ## Other stuff you might like diff --git a/cpp/sorter.hpp b/cpp/sorter.hpp index 506cf02..ae81bcb 100644 --- a/cpp/sorter.hpp +++ b/cpp/sorter.hpp @@ -20,7 +20,7 @@ class Sorter { if (score > -200) { std::unique_lock lock(m_matches_lock); - m_matches.emplace_back(Match{score, file}); + m_matches.emplace_back(Match{score, std::move(file)}); } } @@ -28,9 +28,9 @@ class Sorter { explicit Sorter(std::string_view term) : m_term(term) {} ~Sorter() { m_thread_pool.shutdown(); } - inline std::vector sort(std::vector list) { - for (auto item : list) { - m_thread_pool.push([item, this]() { add_entry(item); }); + inline std::vector sort(const std::vector& list) { + for (const std::string& item : list) { + m_thread_pool.push([&item, this]() { add_entry(item); }); } while (!m_thread_pool.empty()) {