Skip to content

[libc++][test] Speed up input generating functions for benchmark tests #115544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2024

Conversation

winner245
Copy link
Contributor

@winner245 winner245 commented Nov 8, 2024

The input generating functions for benchmark tests in the GenerateInput.h file can be slightly improved by invoking std::vector::reserve before calling push_back. This slight performance improvement could potentially speed-up all benchmark tests for containers and algorithms that use these functions as inputs.

Changes include:

  • Add reserve(N) to the following functions: getSortedIntegerInputs, getSortedLargeIntegerInputs, getReverseSortedIntegerInputs, getRandomIntegerInputs, getRandomStringInputs, and getPrefixedRandomStringInputs.

@winner245 winner245 requested a review from a team as a code owner November 8, 2024 21:10
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Nov 8, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 8, 2024

@llvm/pr-subscribers-libcxx

Author: Peng Liu (winner245)

Changes

The input generating functions for benchmark tests in the GenerateInput.h file can be slightly improved by invoking std::vector::reserve before calling push_back. This slight performance improvement could potentially speed-up all benchmark tests for containers and algorithms that use these functions as inputs.

Changes include:

  • Add reserve(N) to the following functions: getSortedIntegerInputs, getSortedLargeIntegerInputs, getReverseSortedIntegerInputs, getRandomIntegerInputs, getRandomStringInputs, and getPrefixedRandomStringInputs.
  • Replace all unqualifed usages of size_t by std::size_t.

Full diff: https://github.com/llvm/llvm-project/pull/115544.diff

1 Files Affected:

  • (modified) libcxx/test/benchmarks/GenerateInput.h (+26-24)
diff --git a/libcxx/test/benchmarks/GenerateInput.h b/libcxx/test/benchmarks/GenerateInput.h
index cc1694311473ed..db376614382bc4 100644
--- a/libcxx/test/benchmarks/GenerateInput.h
+++ b/libcxx/test/benchmarks/GenerateInput.h
@@ -45,30 +45,31 @@ inline std::string getRandomString(std::size_t Len) {
 }
 
 template <class IntT>
-inline std::vector<IntT> getDuplicateIntegerInputs(size_t N) {
+inline std::vector<IntT> getDuplicateIntegerInputs(std::size_t N) {
   std::vector<IntT> inputs(N, static_cast<IntT>(-1));
   return inputs;
 }
 
 template <class IntT>
-inline std::vector<IntT> getSortedIntegerInputs(size_t N) {
+inline std::vector<IntT> getSortedIntegerInputs(std::size_t N) {
   std::vector<IntT> inputs;
-  for (size_t i = 0; i < N; i += 1)
+  inputs.reserve(N);
+  for (std::size_t i = 0; i < N; i += 1)
     inputs.push_back(i);
   return inputs;
 }
 
 template <class IntT>
-std::vector<IntT> getSortedLargeIntegerInputs(size_t N) {
+std::vector<IntT> getSortedLargeIntegerInputs(std::size_t N) {
   std::vector<IntT> inputs;
-  for (size_t i = 0; i < N; ++i) {
+  inputs.reserve(N);
+  for (std::size_t i = 0; i < N; ++i)
     inputs.push_back(i + N);
-  }
   return inputs;
 }
 
 template <class IntT>
-std::vector<IntT> getSortedTopBitsIntegerInputs(size_t N) {
+std::vector<IntT> getSortedTopBitsIntegerInputs(std::size_t N) {
   std::vector<IntT> inputs = getSortedIntegerInputs<IntT>(N);
   for (auto& E : inputs)
     E <<= ((sizeof(IntT) / 2) * CHAR_BIT);
@@ -76,8 +77,9 @@ std::vector<IntT> getSortedTopBitsIntegerInputs(size_t N) {
 }
 
 template <class IntT>
-inline std::vector<IntT> getReverseSortedIntegerInputs(size_t N) {
+inline std::vector<IntT> getReverseSortedIntegerInputs(std::size_t N) {
   std::vector<IntT> inputs;
+  inputs.reserve(N);
   std::size_t i = N;
   while (i > 0) {
     --i;
@@ -87,61 +89,61 @@ inline std::vector<IntT> getReverseSortedIntegerInputs(size_t N) {
 }
 
 template <class IntT>
-std::vector<IntT> getPipeOrganIntegerInputs(size_t N) {
+std::vector<IntT> getPipeOrganIntegerInputs(std::size_t N) {
   std::vector<IntT> v;
   v.reserve(N);
-  for (size_t i = 0; i < N / 2; ++i)
+  for (std::size_t i = 0; i < N / 2; ++i)
     v.push_back(i);
-  for (size_t i = N / 2; i < N; ++i)
+  for (std::size_t i = N / 2; i < N; ++i)
     v.push_back(N - i);
   return v;
 }
 
 template <class IntT>
-std::vector<IntT> getRandomIntegerInputs(size_t N) {
+std::vector<IntT> getRandomIntegerInputs(std::size_t N) {
   std::vector<IntT> inputs;
-  for (size_t i = 0; i < N; ++i) {
+  inputs.reserve(N);
+  for (std::size_t i = 0; i < N; ++i)
     inputs.push_back(getRandomInteger<IntT>(0, std::numeric_limits<IntT>::max()));
-  }
   return inputs;
 }
 
-inline std::vector<std::string> getDuplicateStringInputs(size_t N) {
+inline std::vector<std::string> getDuplicateStringInputs(std::size_t N) {
   std::vector<std::string> inputs(N, getRandomString(1024));
   return inputs;
 }
 
-inline std::vector<std::string> getRandomStringInputs(size_t N) {
+inline std::vector<std::string> getRandomStringInputs(std::size_t N) {
   std::vector<std::string> inputs;
-  for (size_t i = 0; i < N; ++i) {
+  inputs.reserve(N);
+  for (std::size_t i = 0; i < N; ++i)
     inputs.push_back(getRandomString(1024));
-  }
   return inputs;
 }
 
-inline std::vector<std::string> getPrefixedRandomStringInputs(size_t N) {
+inline std::vector<std::string> getPrefixedRandomStringInputs(std::size_t N) {
   std::vector<std::string> inputs;
+  inputs.reserve(N);
   constexpr int kSuffixLength = 32;
   const std::string prefix    = getRandomString(1024 - kSuffixLength);
-  for (size_t i = 0; i < N; ++i) {
+  for (std::size_t i = 0; i < N; ++i)
     inputs.push_back(prefix + getRandomString(kSuffixLength));
-  }
   return inputs;
 }
 
-inline std::vector<std::string> getSortedStringInputs(size_t N) {
+inline std::vector<std::string> getSortedStringInputs(std::size_t N) {
   std::vector<std::string> inputs = getRandomStringInputs(N);
   std::sort(inputs.begin(), inputs.end());
   return inputs;
 }
 
-inline std::vector<std::string> getReverseSortedStringInputs(size_t N) {
+inline std::vector<std::string> getReverseSortedStringInputs(std::size_t N) {
   std::vector<std::string> inputs = getSortedStringInputs(N);
   std::reverse(inputs.begin(), inputs.end());
   return inputs;
 }
 
-inline std::vector<const char*> getRandomCStringInputs(size_t N) {
+inline std::vector<const char*> getRandomCStringInputs(std::size_t N) {
   static std::vector<std::string> inputs = getRandomStringInputs(N);
   std::vector<const char*> cinputs;
   for (auto const& str : inputs)

@philnik777
Copy link
Contributor

Please split unrelated changes into separate PRs. The std:: qualifications make it really hard to see what relevant parts changed.

@winner245 winner245 force-pushed the winner245/benchmark_input_functions branch from 385eb81 to 6c50f6f Compare November 8, 2024 22:38
@winner245
Copy link
Contributor Author

winner245 commented Nov 8, 2024

Please split unrelated changes into separate PRs. The std:: qualifications make it really hard to see what relevant parts changed.

Thank you for your suggestion. These unrelated changes have been separated in #115560

@ldionne
Copy link
Member

ldionne commented Nov 11, 2024

Please rebase onto main!

@winner245 winner245 force-pushed the winner245/benchmark_input_functions branch from 6c50f6f to 2046f4a Compare November 11, 2024 16:24
@winner245 winner245 force-pushed the winner245/benchmark_input_functions branch from 2046f4a to 3982e19 Compare November 17, 2024 21:01
@ldionne ldionne merged commit c25e09e into llvm:main Nov 18, 2024
61 of 62 checks passed
@winner245 winner245 deleted the winner245/benchmark_input_functions branch November 18, 2024 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants