diff options
| author | Martin Hořeňovský | 2023-01-27 22:12:57 +0100 |
|---|---|---|
| committer | Martin Hořeňovský | 2023-01-29 10:14:20 +0100 |
| commit | 43f02027e47310e200f20f5c3a1de6fe281e2f81 (patch) | |
| tree | d0a0f722915ee180eaed42d6d4fac553f758cee1 /src/catch2/internal/catch_test_case_tracker.hpp | |
| parent | 906552f8c831d0dd1917484b53dd080537f19acd (diff) | |
| download | catch3-43f02027e47310e200f20f5c3a1de6fe281e2f81.tar.gz | |
Avoid allocations when looking for trackers
Now we delay allocating owning `NameAndLocation` instances until
we construct a new tracker (because a tracker's lifetime can be
significantly different from the underlying tracked-thing's name).
This saves 4239 allocations (436948 -> 432709) when running
`./tests/SelfTest -o /dev/null`, at some cost to code clarity
due to introducing a new ref type, `NameAndLocationRef`.
Diffstat (limited to 'src/catch2/internal/catch_test_case_tracker.hpp')
| -rw-r--r-- | src/catch2/internal/catch_test_case_tracker.hpp | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/src/catch2/internal/catch_test_case_tracker.hpp b/src/catch2/internal/catch_test_case_tracker.hpp index 1e31e425..0021d254 100644 --- a/src/catch2/internal/catch_test_case_tracker.hpp +++ b/src/catch2/internal/catch_test_case_tracker.hpp @@ -22,7 +22,7 @@ namespace TestCaseTracking { std::string name; SourceLineInfo location; - NameAndLocation( std::string const& _name, SourceLineInfo const& _location ); + NameAndLocation( std::string&& _name, SourceLineInfo const& _location ); friend bool operator==(NameAndLocation const& lhs, NameAndLocation const& rhs) { return lhs.name == rhs.name && lhs.location == rhs.location; @@ -33,6 +33,32 @@ namespace TestCaseTracking { } }; + /** + * This is a variant of `NameAndLocation` that does not own the name string + * + * This avoids extra allocations when trying to locate a tracker by its + * name and location, as long as we make sure that trackers only keep + * around the owning variant. + */ + struct NameAndLocationRef { + StringRef name; + SourceLineInfo location; + + constexpr NameAndLocationRef( StringRef name_, + SourceLineInfo location_ ): + name( name_ ), location( location_ ) {} + + friend bool operator==( NameAndLocation const& lhs, + NameAndLocationRef rhs ) { + return StringRef( lhs.name ) == rhs.name && + lhs.location == rhs.location; + } + friend bool operator==( NameAndLocationRef lhs, + NameAndLocation const& rhs ) { + return rhs == lhs; + } + }; + class ITracker; using ITrackerPtr = Catch::Detail::unique_ptr<ITracker>; @@ -57,8 +83,8 @@ namespace TestCaseTracking { CycleState m_runState = NotStarted; public: - ITracker( NameAndLocation const& nameAndLoc, ITracker* parent ): - m_nameAndLocation( nameAndLoc ), + ITracker( NameAndLocation&& nameAndLoc, ITracker* parent ): + m_nameAndLocation( CATCH_MOVE(nameAndLoc) ), m_parent( parent ) {} @@ -97,7 +123,7 @@ namespace TestCaseTracking { * * Returns nullptr if not found. */ - ITracker* findChild( NameAndLocation const& nameAndLocation ); + ITracker* findChild( NameAndLocationRef nameAndLocation ); //! Have any children been added? bool hasChildren() const { return !m_children.empty(); @@ -154,7 +180,7 @@ namespace TestCaseTracking { TrackerContext& m_ctx; public: - TrackerBase( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ); + TrackerBase( NameAndLocation&& nameAndLocation, TrackerContext& ctx, ITracker* parent ); bool isComplete() const override; @@ -172,13 +198,13 @@ namespace TestCaseTracking { std::vector<StringRef> m_filters; std::string m_trimmed_name; public: - SectionTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ); + SectionTracker( NameAndLocation&& nameAndLocation, TrackerContext& ctx, ITracker* parent ); bool isSectionTracker() const override; bool isComplete() const override; - static SectionTracker& acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation ); + static SectionTracker& acquire( TrackerContext& ctx, NameAndLocationRef nameAndLocation ); void tryOpen(); |