mirror of https://github.com/grpc/grpc.git
[metadata] Reduce size by packing (#30474)
* [metadata] Reduce size by packing
* fix
* comment
* Automated change: Fix sanity tests
* Automated change: Fix sanity tests
* windows workaround?
* Revert "fix"
This reverts commit 925211164b
.
* iterate encodables correctly
Co-authored-by: ctiller <ctiller@users.noreply.github.com>
pull/30787/head
parent
9edded2cda
commit
18da150733
16 changed files with 347 additions and 7 deletions
@ -0,0 +1,40 @@ |
||||
// Copyright 2022 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef GRPC_CORE_LIB_GPRPP_PACKED_TABLE_H |
||||
#define GRPC_CORE_LIB_GPRPP_PACKED_TABLE_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/gprpp/sorted_pack.h" |
||||
#include "src/core/lib/gprpp/table.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
namespace packed_table_detail { |
||||
template <typename A, typename B> |
||||
struct Cmp { |
||||
static constexpr bool kValue = alignof(A) > alignof(B) || |
||||
(alignof(A) == alignof(B) && |
||||
sizeof(A) > sizeof(B)); |
||||
}; |
||||
}; // namespace packed_table_detail
|
||||
|
||||
template <typename... T> |
||||
using PackedTable = |
||||
typename WithSortedPack<Table, packed_table_detail::Cmp, T...>::Type; |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif // GRPC_CORE_LIB_GPRPP_PACKED_TABLE_H
|
@ -0,0 +1,98 @@ |
||||
// Copyright 2022 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef GRPC_CORE_LIB_GPRPP_SORTED_PACK_H |
||||
#define GRPC_CORE_LIB_GPRPP_SORTED_PACK_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <type_traits> |
||||
|
||||
namespace grpc_core { |
||||
|
||||
namespace sorted_pack_detail { |
||||
|
||||
// A list of types
|
||||
template <typename... A> |
||||
struct Typelist { |
||||
template <template <typename...> class T> |
||||
using Instantiate = T<A...>; |
||||
|
||||
template <typename C> |
||||
using PushFront = Typelist<C, A...>; |
||||
}; |
||||
|
||||
// Find the smallest element of Args, and the rest of the elements
|
||||
template <template <typename, typename> class Cmp, typename Args> |
||||
struct Smallest; |
||||
|
||||
template <template <typename, typename> class Cmp, typename Arg, |
||||
typename... Args> |
||||
struct Smallest<Cmp, Typelist<Arg, Args...>> { |
||||
using SmallestRest = Smallest<Cmp, Typelist<Args...>>; |
||||
using PrevSmallest = typename SmallestRest::Result; |
||||
using PrevRest = typename SmallestRest::Rest; |
||||
static constexpr bool kCmpResult = Cmp<Arg, PrevSmallest>::kValue; |
||||
using Result = typename std::conditional<kCmpResult, Arg, PrevSmallest>::type; |
||||
using Prefix = typename std::conditional<kCmpResult, PrevSmallest, Arg>::type; |
||||
using Rest = typename PrevRest::template PushFront<Prefix>; |
||||
}; |
||||
|
||||
template <template <typename, typename> class Cmp, typename Arg> |
||||
struct Smallest<Cmp, Typelist<Arg>> { |
||||
using Result = Arg; |
||||
using Rest = Typelist<>; |
||||
}; |
||||
|
||||
// Sort a list of types into a typelist
|
||||
template <template <typename, typename> class Cmp, typename Args> |
||||
struct Sorted; |
||||
|
||||
template <template <typename, typename> class Cmp, typename... Args> |
||||
struct Sorted<Cmp, Typelist<Args...>> { |
||||
using SmallestResult = Smallest<Cmp, Typelist<Args...>>; |
||||
using SmallestType = typename SmallestResult::Result; |
||||
using RestOfTypes = typename SmallestResult::Rest; |
||||
using SortedRestOfTypes = typename Sorted<Cmp, RestOfTypes>::Result; |
||||
using Result = typename SortedRestOfTypes::template PushFront<SmallestType>; |
||||
}; |
||||
|
||||
template <template <typename, typename> class Cmp, typename Arg> |
||||
struct Sorted<Cmp, Typelist<Arg>> { |
||||
using Result = Typelist<Arg>; |
||||
}; |
||||
|
||||
template <template <typename, typename> class Cmp> |
||||
struct Sorted<Cmp, Typelist<>> { |
||||
using Result = Typelist<>; |
||||
}; |
||||
|
||||
} // namespace sorted_pack_detail
|
||||
|
||||
// Given a type T<A...>, and a type comparator Cmp<P,Q>, and some set of types
|
||||
// Args...:
|
||||
// Sort Args... using Cmp into SortedArgs..., then instantiate T<SortedArgs...>
|
||||
// as Type.
|
||||
// Cmp<P,Q> should have a single constant `kValue` that is true if P < Q.
|
||||
template <template <typename...> class T, |
||||
template <typename, typename> class Cmp, typename... Args> |
||||
struct WithSortedPack { |
||||
using Type = typename sorted_pack_detail::Sorted< |
||||
Cmp, |
||||
sorted_pack_detail::Typelist<Args...>>::Result::template Instantiate<T>; |
||||
}; |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif // GRPC_CORE_LIB_GPRPP_SORTED_PACK_H
|
@ -0,0 +1,70 @@ |
||||
// Copyright 2022 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "src/core/lib/gprpp/sorted_pack.h" |
||||
|
||||
#include <type_traits> |
||||
#include <vector> |
||||
|
||||
#include <gtest/gtest.h> |
||||
|
||||
using grpc_core::WithSortedPack; |
||||
|
||||
template <int I> |
||||
struct Int { |
||||
int value() const { return I; } |
||||
}; |
||||
|
||||
template <typename A, typename B> |
||||
struct Cmp; |
||||
template <int A, int B> |
||||
struct Cmp<Int<A>, Int<B>> { |
||||
static constexpr bool kValue = A < B; |
||||
}; |
||||
|
||||
template <typename... Args> |
||||
struct VecMaker { |
||||
static std::vector<int> Make() { return {Args().value()...}; } |
||||
}; |
||||
|
||||
template <int... Args> |
||||
std::vector<int> TestVec() { |
||||
return WithSortedPack<VecMaker, Cmp, Int<Args>...>::Type::Make(); |
||||
} |
||||
|
||||
TEST(SortedPackTest, Empty) { EXPECT_EQ(TestVec<>(), std::vector<int>{}); } |
||||
|
||||
TEST(SortedPackTest, LenOne) { |
||||
EXPECT_EQ((TestVec<1>()), (std::vector<int>{1})); |
||||
EXPECT_EQ((TestVec<2>()), (std::vector<int>{2})); |
||||
} |
||||
|
||||
TEST(SortedPackTest, Len2) { |
||||
EXPECT_EQ((TestVec<1, 2>()), (std::vector<int>{1, 2})); |
||||
EXPECT_EQ((TestVec<2, 1>()), (std::vector<int>{1, 2})); |
||||
} |
||||
|
||||
TEST(SortedPackTest, Len3) { |
||||
EXPECT_EQ((TestVec<1, 2, 3>()), (std::vector<int>{1, 2, 3})); |
||||
EXPECT_EQ((TestVec<1, 3, 2>()), (std::vector<int>{1, 2, 3})); |
||||
EXPECT_EQ((TestVec<2, 1, 3>()), (std::vector<int>{1, 2, 3})); |
||||
EXPECT_EQ((TestVec<2, 3, 1>()), (std::vector<int>{1, 2, 3})); |
||||
EXPECT_EQ((TestVec<3, 1, 2>()), (std::vector<int>{1, 2, 3})); |
||||
EXPECT_EQ((TestVec<3, 2, 1>()), (std::vector<int>{1, 2, 3})); |
||||
} |
||||
|
||||
int main(int argc, char** argv) { |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
return RUN_ALL_TESTS(); |
||||
} |
Loading…
Reference in new issue