From 26a8984d035e1b9e8224315cdbd942ee6bd875e3 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 12 Apr 2020 20:37:51 +0200 Subject: [PATCH] AK: Inline Optional functions more aggressively This turns into much less code in the most common cases, here's why: The normal Optional usage pattern is something like: auto foo = get_me_an_optional(); if (foo.has_value()) do_stuff_with(foo.value()); In this typical scenario, we check has_value() before calling value(). Without inlining, value() will double-check has_value() itself and assert if it fails. Inlining allows the compiler to optimize all of this away. --- AK/Optional.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/AK/Optional.h b/AK/Optional.h index e14f4ecf49..6c713db346 100644 --- a/AK/Optional.h +++ b/AK/Optional.h @@ -105,12 +105,12 @@ public: return *this; } - ~Optional() + [[gnu::always_inline]] inline ~Optional() { clear(); } - void clear() + [[gnu::always_inline]] inline void clear() { if (m_has_value) { value().~T(); @@ -119,17 +119,17 @@ public: } SET_TYPESTATE(consumed) - bool has_value() const { return m_has_value; } + [[gnu::always_inline]] inline bool has_value() const { return m_has_value; } CALLABLE_WHEN(consumed) - T& value() + [[gnu::always_inline]] inline T& value() { ASSERT(m_has_value); return *reinterpret_cast(&m_storage); } CALLABLE_WHEN(consumed) - const T& value() const + [[gnu::always_inline]] inline const T& value() const { return value_without_consume_state(); } @@ -144,7 +144,7 @@ public: return released_value; } - T value_or(const T& fallback) const + [[gnu::always_inline]] inline T value_or(const T& fallback) const { if (m_has_value) return value(); @@ -153,7 +153,7 @@ public: private: // Call when we don't want to alter the consume state - const T& value_without_consume_state() const + [[gnu::always_inline]] inline const T& value_without_consume_state() const { ASSERT(m_has_value); return *reinterpret_cast(&m_storage);