diff options
author | Abseil Team <absl-team@google.com> | 2024-03-05 10:36:18 (GMT) |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-03-05 10:37:09 (GMT) |
commit | b9059aaa4cb93b3fb177670cbfd9c6d0690af37e (patch) | |
tree | 4e7561407a284cb66b326fb0a40b5ac0ec13c65d /googletest/src/gtest-internal-inl.h | |
parent | 926f681a902cbfb84a863dafb77175f4a25be6ea (diff) | |
download | googletest-b9059aaa4cb93b3fb177670cbfd9c6d0690af37e.zip googletest-b9059aaa4cb93b3fb177670cbfd9c6d0690af37e.tar.gz googletest-b9059aaa4cb93b3fb177670cbfd9c6d0690af37e.tar.bz2 |
Optimize Google Test process startup
Google Test performs hidden test registration during process
startup. For test binaries that contain a large number of tests, this
registration can be costly. In this CL, we reduce the overhead of
registration via several tactics:
- Treat CodeLocation and FilePath as value types, using std::move to
pass them around.
- Reduce string copies in various places by either passing std::string
values via std::move, or passing const-refs to std::string instances.
- Use std::to_string to stringify an int in DefaultParamName rather than
a std::stringstream.
- Pull some std::string instances out of nested loops in
ParameterizedTestSuiteInfo::RegisterTests so as to reuse some
allocations, and replace stringstream with ordinary string appends.
- Use std::unordered_map in UnitTestImpl::GetTestSuite and
ParameterizedTestSuiteRegistry::GetTestSuitePatternHolder to spend a
little memory to turn O(N) lookups into constant time lookpus.
- Use range-based for loops in various places.
- Use emplace-ish methods to add to containers where appropriate.
All together, these changes reduce the overall runtime of a series of 50
death tests in a single Chromium test executable by ~38% due to the
fact that the registration costs are paid in every death test's child
process.
PiperOrigin-RevId: 612763676
Change-Id: I1f46e012ccb9004c009e1027e4f7c38780ffb9e2
Diffstat (limited to 'googletest/src/gtest-internal-inl.h')
-rw-r--r-- | googletest/src/gtest-internal-inl.h | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/googletest/src/gtest-internal-inl.h b/googletest/src/gtest-internal-inl.h index 6dea34f..6a7f4dd 100644 --- a/googletest/src/gtest-internal-inl.h +++ b/googletest/src/gtest-internal-inl.h @@ -46,6 +46,7 @@ #include <memory> #include <set> #include <string> +#include <unordered_map> #include <vector> #include "gtest/internal/gtest-port.h" @@ -649,13 +650,15 @@ class GTEST_API_ UnitTestImpl { // this is not a typed or a type-parameterized test. // set_up_tc: pointer to the function that sets up the test suite // tear_down_tc: pointer to the function that tears down the test suite - TestSuite* GetTestSuite(const char* test_suite_name, const char* type_param, + TestSuite* GetTestSuite(const std::string& test_suite_name, + const char* type_param, internal::SetUpTestSuiteFunc set_up_tc, internal::TearDownTestSuiteFunc tear_down_tc); // Legacy API is deprecated but still available #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - TestCase* GetTestCase(const char* test_case_name, const char* type_param, + TestCase* GetTestCase(const std::string& test_case_name, + const char* type_param, internal::SetUpTestSuiteFunc set_up_tc, internal::TearDownTestSuiteFunc tear_down_tc) { return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc); @@ -681,13 +684,13 @@ class GTEST_API_ UnitTestImpl { // AddTestInfo(), which is called to register a TEST or TEST_F // before main() is reached. if (original_working_dir_.IsEmpty()) { - original_working_dir_.Set(FilePath::GetCurrentDir()); + original_working_dir_ = FilePath::GetCurrentDir(); GTEST_CHECK_(!original_working_dir_.IsEmpty()) << "Failed to get the current working directory."; } #endif // GTEST_HAS_FILE_SYSTEM - GetTestSuite(test_info->test_suite_name(), test_info->type_param(), + GetTestSuite(test_info->test_suite_name_, test_info->type_param(), set_up_tc, tear_down_tc) ->AddTestInfo(test_info); } @@ -823,6 +826,12 @@ class GTEST_API_ UnitTestImpl { bool catch_exceptions() const { return catch_exceptions_; } private: + struct CompareTestSuitesByPointer { + bool operator()(const TestSuite* lhs, const TestSuite* rhs) const { + return lhs->name_ < rhs->name_; + }; + }; + friend class ::testing::UnitTest; // Used by UnitTest::Run() to capture the state of @@ -873,6 +882,9 @@ class GTEST_API_ UnitTestImpl { // elements in the vector. std::vector<TestSuite*> test_suites_; + // The set of TestSuites by name. + std::unordered_map<std::string, TestSuite*> test_suites_by_name_; + // Provides a level of indirection for the test suite list to allow // easy shuffling and restoring the test suite order. The i-th // element of this vector is the index of the i-th test suite in the |