diff --git a/common/wivrn_serialization.h b/common/wivrn_serialization.h index e5636d17..79ecd22a 100644 --- a/common/wivrn_serialization.h +++ b/common/wivrn_serialization.h @@ -254,6 +254,11 @@ struct serialization_traits>> packet.read((char *)&value, sizeof(value)); return value; } + + static bool consteval is_trivially_serializable() + { + return true; + } }; template @@ -276,6 +281,10 @@ struct serialization_traits>> packet.read((char *)&value, sizeof(value)); return value; } + static bool consteval is_trivially_serializable() + { + return true; + } }; template @@ -315,18 +324,61 @@ struct serialization_traits && !is_st static void serialize(const T & value, serialization_packet & packet) { - boost::pfr::for_each_field(value, [&](const auto & x) { packet.serialize(x); }); + if constexpr (serialization_traits::is_trivially_serializable()) + { + packet.write((void *)&value, sizeof(T)); + } + else + { + boost::pfr::for_each_field(value, [&](const auto & x) { packet.serialize(x); }); + } } static T deserialize(deserialization_packet & packet) { T value; - boost::pfr::for_each_field( - value, [&](auto & x) { x = packet.deserialize>(); }); + if constexpr (serialization_traits::is_trivially_serializable()) + { + packet.read((void *)&value, sizeof(T)); + } + else + { + boost::pfr::for_each_field( + value, [&](auto & x) { x = packet.deserialize>(); }); + } return value; } + + template + static consteval size_t ts_aux_size_2() + { + return sizeof(boost::pfr::tuple_element_t); + } + + template + static constexpr size_t ts_aux_size_1(std::index_sequence) + { + return (ts_aux_size_2() + ... + 0); + } + + template + static consteval bool ts_aux_trivial_2() + { + return serialization_traits>::is_trivially_serializable(); + } + + template + static constexpr bool ts_aux_trivial_1(std::index_sequence) + { + return (ts_aux_trivial_2() and ...); + } + + static bool consteval is_trivially_serializable() + { + return sizeof(T) == ts_aux_size_1(std::make_index_sequence>()) and ts_aux_trivial_1(std::make_index_sequence>()); + } }; template <> @@ -354,6 +406,11 @@ struct serialization_traits packet.read(value.data(), size); return value; } + + static bool consteval is_trivially_serializable() + { + return false; + } }; template @@ -370,7 +427,7 @@ struct serialization_traits> { packet.serialize(value.size()); - if constexpr (std::is_arithmetic_v) + if constexpr (serialization_traits::is_trivially_serializable()) { packet.write(value.data(), value.size() * sizeof(T)); } @@ -386,7 +443,7 @@ struct serialization_traits> std::vector value; size_t size = packet.deserialize(); - if constexpr (std::is_arithmetic_v) + if constexpr (serialization_traits::is_trivially_serializable()) { packet.check_remaining_size(size * sizeof(T)); value.resize(size); @@ -403,6 +460,10 @@ struct serialization_traits> return value; } + static bool consteval is_trivially_serializable() + { + return false; + } }; template @@ -435,6 +496,10 @@ struct serialization_traits> else return std::nullopt; } + static bool consteval is_trivially_serializable() + { + return false; + } }; template @@ -451,21 +516,40 @@ struct serialization_traits> static void serialize(const std::array & value, serialization_packet & packet) { - for (const T & i: value) - packet.serialize(i); + if constexpr (serialization_traits::is_trivially_serializable()) + { + packet.write(value.data(), value.size() * sizeof(T)); + } + else + { + for (const T & i: value) + packet.serialize(i); + } } static std::array deserialize(deserialization_packet & packet) { std::array value; - - for (size_t i = 0; i < N; i++) + if constexpr (serialization_traits::is_trivially_serializable()) { - value[i] = packet.deserialize(); + packet.check_remaining_size(N * sizeof(T)); + packet.read(value.data(), N * sizeof(T)); + } + else + { + for (size_t i = 0; i < N; i++) + { + value[i] = packet.deserialize(); + } } return value; } + + static bool consteval is_trivially_serializable() + { + return serialization_traits::is_trivially_serializable() and sizeof(std::array) == sizeof(T) * N; + } }; template @@ -515,6 +599,10 @@ struct serialization_traits> return deserialize_aux(packet, type_index, std::make_index_sequence()); } + static bool consteval is_trivially_serializable() + { + return false; + } }; template @@ -541,6 +629,11 @@ struct serialization_traits> Rep nsec = packet.deserialize(); return std::chrono::duration{nsec}; } + + static bool consteval is_trivially_serializable() + { + return sizeof(std::chrono::duration) == sizeof(Rep); + } }; template <> @@ -562,6 +655,10 @@ struct serialization_traits> size_t size = packet.deserialize(); return packet.read_span(size); } + static bool consteval is_trivially_serializable() + { + return false; + } }; template <> @@ -582,6 +679,10 @@ struct serialization_traits std::tie(size, value.c) = packet.steal_buffer(); return value; } + static bool consteval is_trivially_serializable() + { + return false; + } }; template