diff options
| author | swarmer <[email protected]> | 2025-12-25 03:21:48 +0300 |
|---|---|---|
| committer | swarmer <[email protected]> | 2025-12-25 03:42:08 +0300 |
| commit | 81519b4a5aa5bdfc06e4ff9e80e24919f5f9b96d (patch) | |
| tree | 447cdc1cd1375f70b78e1970294bcd53ddd6e673 /library/cpp/iterator | |
| parent | 4dbf62fd2f8cc5ece53cc1446561cf71476bdd12 (diff) | |
make mapped iterator smaller by using the no_unique_address optimization
commit_hash:42520e745ec2135e405740d2591c55b70b0cba43
Diffstat (limited to 'library/cpp/iterator')
| -rw-r--r-- | library/cpp/iterator/filtering.h | 5 | ||||
| -rw-r--r-- | library/cpp/iterator/mapped.h | 5 | ||||
| -rw-r--r-- | library/cpp/iterator/ut/mapped_ut.cpp | 17 |
3 files changed, 23 insertions, 4 deletions
diff --git a/library/cpp/iterator/filtering.h b/library/cpp/iterator/filtering.h index c28e3bc6c44..23cfef6d70e 100644 --- a/library/cpp/iterator/filtering.h +++ b/library/cpp/iterator/filtering.h @@ -2,6 +2,7 @@ #include <util/generic/iterator_range.h> #include <util/generic/store_policy.h> +#include <util/system/compiler.h> #include <iterator> @@ -53,7 +54,7 @@ private: } TIterator Iter; TIterator Last; - TCondition Condition; + Y_NO_UNIQUE_ADDRESS TCondition Condition; }; @@ -87,7 +88,7 @@ public: private: mutable TContainerStorage Container; - mutable TConditionStorage Condition; + Y_NO_UNIQUE_ADDRESS mutable TConditionStorage Condition; }; diff --git a/library/cpp/iterator/mapped.h b/library/cpp/iterator/mapped.h index d78371a9e5d..348aa200bf2 100644 --- a/library/cpp/iterator/mapped.h +++ b/library/cpp/iterator/mapped.h @@ -2,6 +2,7 @@ #include <util/generic/iterator_range.h> #include <util/generic/store_policy.h> +#include <util/system/compiler.h> #include <iterator> @@ -91,7 +92,7 @@ public: private: TIterator Iter; - TMapper Mapper; + Y_NO_UNIQUE_ADDRESS TMapper Mapper; }; @@ -130,7 +131,7 @@ public: protected: mutable TContainerStorage Container; - mutable TMapperStorage Mapper; + Y_NO_UNIQUE_ADDRESS mutable TMapperStorage Mapper; }; diff --git a/library/cpp/iterator/ut/mapped_ut.cpp b/library/cpp/iterator/ut/mapped_ut.cpp index 440cd37945a..15a8196eded 100644 --- a/library/cpp/iterator/ut/mapped_ut.cpp +++ b/library/cpp/iterator/ut/mapped_ut.cpp @@ -7,12 +7,29 @@ using namespace testing; +namespace { + struct TSelectFirst { + const auto& operator()(const auto& pair) const { + return pair.first; + } + }; +} + TEST(TIterator, TMappedIteratorTest) { TVector<int> x = {1, 2, 3, 4, 5}; auto it = MakeMappedIterator(x.begin(), [](int x) { return x + 7; }); EXPECT_EQ(*it, 8); EXPECT_EQ(it[2], 10); + + TVector<std::pair<int, int>> pairs = {{1, 2}, {3, 4}, {5, 6}}; + auto firstIt = MakeMappedIterator(pairs.begin(), TSelectFirst{}); + EXPECT_EQ(*std::next(firstIt, 0), 1); + EXPECT_EQ(*std::next(firstIt, 1), 3); + EXPECT_EQ(*std::next(firstIt, 2), 5); +#if defined(_compiler_clang_) && defined(_linux_) + static_assert(sizeof(pairs.begin()) == sizeof(firstIt), "empty mapper should not add size overhead"); // this check expected to hold, but not guaranteed to +#endif } TEST(TIterator, TMappedRangeTest) { |
