From 36190e3baf799e6a49ee78b55dc0211178ff5d17 Mon Sep 17 00:00:00 2001 From: Ivan Zuzak Date: Tue, 7 Jan 2025 16:23:23 +0100 Subject: [PATCH] AK: Optimize performance in GenericShorthands via forwarding references Functions in AK/GenericShorthands used pass-by-value which results in copying values when calling those functions. This could be expensive for complex types. To optimize performance, we switch the functions to use forwarding references. --- AK/GenericShorthands.h | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/AK/GenericShorthands.h b/AK/GenericShorthands.h index c2fd328a3d8..57f7f8a32f4 100644 --- a/AK/GenericShorthands.h +++ b/AK/GenericShorthands.h @@ -7,61 +7,62 @@ #pragma once #include +#include namespace AK { template -[[nodiscard]] constexpr bool first_is_one_of(T const to_compare, Ts const... valid_values) +[[nodiscard]] constexpr bool first_is_one_of(T&& to_compare, Ts&&... valid_values) { - return (... || (to_compare == valid_values)); + return (... || (forward(to_compare) == forward(valid_values))); } template -[[nodiscard]] constexpr bool first_is_smaller_than_one_of(T const to_compare, Ts const... valid_values) +[[nodiscard]] constexpr bool first_is_smaller_than_one_of(T&& to_compare, Ts&&... valid_values) { - return (... || (to_compare < valid_values)); + return (... || (forward(to_compare) < forward(valid_values))); } template -[[nodiscard]] constexpr bool first_is_smaller_or_equal_than_one_of(T const to_compare, Ts const... valid_values) +[[nodiscard]] constexpr bool first_is_smaller_or_equal_than_one_of(T&& to_compare, Ts&&... valid_values) { - return (... || (to_compare <= valid_values)); + return (... || (forward(to_compare) <= forward(valid_values))); } template -[[nodiscard]] constexpr bool first_is_larger_than_one_of(T const to_compare, Ts const... valid_values) +[[nodiscard]] constexpr bool first_is_larger_than_one_of(T&& to_compare, Ts&&... valid_values) { - return (... || (to_compare > valid_values)); + return (... || (forward(to_compare) > forward(valid_values))); } template -[[nodiscard]] constexpr bool first_is_larger_or_equal_than_one_of(T const to_compare, Ts const... valid_values) +[[nodiscard]] constexpr bool first_is_larger_or_equal_than_one_of(T&& to_compare, Ts&&... valid_values) { - return (... || (to_compare >= valid_values)); + return (... || (forward(to_compare) >= forward(valid_values))); } template -[[nodiscard]] constexpr bool first_is_smaller_than_all_of(T const to_compare, Ts const... valid_values) +[[nodiscard]] constexpr bool first_is_smaller_than_all_of(T&& to_compare, Ts&&... valid_values) { - return (... && (to_compare < valid_values)); + return (... && (forward(to_compare) < forward(valid_values))); } template -[[nodiscard]] constexpr bool first_is_smaller_or_equal_than_all_of(T const to_compare, Ts const... valid_values) +[[nodiscard]] constexpr bool first_is_smaller_or_equal_than_all_of(T&& to_compare, Ts&&... valid_values) { - return (... && (to_compare <= valid_values)); + return (... && (forward(to_compare) <= forward(valid_values))); } template -[[nodiscard]] constexpr bool first_is_larger_than_all_of(T const to_compare, Ts const... valid_values) +[[nodiscard]] constexpr bool first_is_larger_than_all_of(T&& to_compare, Ts&&... valid_values) { - return (... && (to_compare > valid_values)); + return (... && (forward(to_compare) > forward(valid_values))); } template -[[nodiscard]] constexpr bool first_is_larger_or_equal_than_all_of(T const to_compare, Ts const... valid_values) +[[nodiscard]] constexpr bool first_is_larger_or_equal_than_all_of(T&& to_compare, Ts&&... valid_values) { - return (... && (to_compare >= valid_values)); + return (... && (forward(to_compare) >= forward(valid_values))); } }