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.
This commit is contained in:
Ivan Zuzak 2025-01-07 16:23:23 +01:00 committed by Jelle Raaijmakers
parent 49fc569136
commit 36190e3baf
Notes: github-actions[bot] 2025-01-09 08:04:50 +00:00

View file

@ -7,61 +7,62 @@
#pragma once #pragma once
#include <AK/Platform.h> #include <AK/Platform.h>
#include <AK/StdLibExtras.h>
namespace AK { namespace AK {
template<typename T, typename... Ts> template<typename T, typename... Ts>
[[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<T>(to_compare) == forward<Ts>(valid_values)));
} }
template<typename T, typename... Ts> template<typename T, typename... Ts>
[[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<T>(to_compare) < forward<Ts>(valid_values)));
} }
template<typename T, typename... Ts> template<typename T, typename... Ts>
[[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<T>(to_compare) <= forward<Ts>(valid_values)));
} }
template<typename T, typename... Ts> template<typename T, typename... Ts>
[[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<T>(to_compare) > forward<Ts>(valid_values)));
} }
template<typename T, typename... Ts> template<typename T, typename... Ts>
[[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<T>(to_compare) >= forward<Ts>(valid_values)));
} }
template<typename T, typename... Ts> template<typename T, typename... Ts>
[[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<T>(to_compare) < forward<Ts>(valid_values)));
} }
template<typename T, typename... Ts> template<typename T, typename... Ts>
[[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<T>(to_compare) <= forward<Ts>(valid_values)));
} }
template<typename T, typename... Ts> template<typename T, typename... Ts>
[[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<T>(to_compare) > forward<Ts>(valid_values)));
} }
template<typename T, typename... Ts> template<typename T, typename... Ts>
[[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<T>(to_compare) >= forward<Ts>(valid_values)));
} }
} }