From d2b500fbcb741f080450e5d0a48ae4ea9e076de3 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Wed, 20 May 2020 14:59:31 +0300 Subject: [PATCH] AK+Kernel: Help the compiler inline a bunch of trivial methods If these methods get inlined, the compiler is able to statically eliminate most of the assertions. Alas, it doesn't realize this, and believes inlining them to be too expensive. So give it a strong hint that it's not the case. This *decreases* the kernel binary size. --- AK/NonnullRefPtr.h | 44 ++++++++++++++++++++++---------------------- AK/RefCounted.h | 8 ++++---- AK/RefPtr.h | 44 ++++++++++++++++++++++---------------------- Kernel/KResult.h | 4 ++-- Kernel/Thread.cpp | 5 ----- Kernel/Thread.h | 2 +- 6 files changed, 51 insertions(+), 56 deletions(-) diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h index 30a33f66d4..b7cba8f1b6 100644 --- a/AK/NonnullRefPtr.h +++ b/AK/NonnullRefPtr.h @@ -39,14 +39,14 @@ template class RefPtr; template -inline void ref_if_not_null(T* ptr) +ALWAYS_INLINE void ref_if_not_null(T* ptr) { if (ptr) ptr->ref(); } template -inline void unref_if_not_null(T* ptr) +ALWAYS_INLINE void unref_if_not_null(T* ptr) { if (ptr) ptr->unref(); @@ -59,42 +59,42 @@ public: enum AdoptTag { Adopt }; - NonnullRefPtr(const T& object) + ALWAYS_INLINE NonnullRefPtr(const T& object) : m_ptr(const_cast(&object)) { m_ptr->ref(); } template - NonnullRefPtr(const U& object) + ALWAYS_INLINE NonnullRefPtr(const U& object) : m_ptr(&const_cast(object)) { m_ptr->ref(); } - NonnullRefPtr(AdoptTag, T& object) + ALWAYS_INLINE NonnullRefPtr(AdoptTag, T& object) : m_ptr(&object) { } - NonnullRefPtr(NonnullRefPtr&& other) + ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr&& other) : m_ptr(&other.leak_ref()) { } template - NonnullRefPtr(NonnullRefPtr&& other) + ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr&& other) : m_ptr(&other.leak_ref()) { } - NonnullRefPtr(const NonnullRefPtr& other) + ALWAYS_INLINE NonnullRefPtr(const NonnullRefPtr& other) : m_ptr(const_cast(other.ptr())) { m_ptr->ref(); } template - NonnullRefPtr(const NonnullRefPtr& other) + ALWAYS_INLINE NonnullRefPtr(const NonnullRefPtr& other) : m_ptr(const_cast(other.ptr())) { m_ptr->ref(); } - ~NonnullRefPtr() + ALWAYS_INLINE ~NonnullRefPtr() { unref_if_not_null(m_ptr); m_ptr = nullptr; @@ -133,7 +133,7 @@ public: return *this; } - NonnullRefPtr& operator=(NonnullRefPtr&& other) + ALWAYS_INLINE NonnullRefPtr& operator=(NonnullRefPtr&& other) { NonnullRefPtr ptr(move(other)); swap(ptr); @@ -155,62 +155,62 @@ public: return *this; } - [[nodiscard]] T& leak_ref() + [[nodiscard]] ALWAYS_INLINE T& leak_ref() { ASSERT(m_ptr); return *exchange(m_ptr, nullptr); } - T* ptr() + ALWAYS_INLINE T* ptr() { ASSERT(m_ptr); return m_ptr; } - const T* ptr() const + ALWAYS_INLINE const T* ptr() const { ASSERT(m_ptr); return m_ptr; } - T* operator->() + ALWAYS_INLINE T* operator->() { ASSERT(m_ptr); return m_ptr; } - const T* operator->() const + ALWAYS_INLINE const T* operator->() const { ASSERT(m_ptr); return m_ptr; } - T& operator*() + ALWAYS_INLINE T& operator*() { ASSERT(m_ptr); return *m_ptr; } - const T& operator*() const + ALWAYS_INLINE const T& operator*() const { ASSERT(m_ptr); return *m_ptr; } - operator T*() + ALWAYS_INLINE operator T*() { ASSERT(m_ptr); return m_ptr; } - operator const T*() const + ALWAYS_INLINE operator const T*() const { ASSERT(m_ptr); return m_ptr; } - operator T&() + ALWAYS_INLINE operator T&() { ASSERT(m_ptr); return *m_ptr; } - operator const T&() const + ALWAYS_INLINE operator const T&() const { ASSERT(m_ptr); return *m_ptr; diff --git a/AK/RefCounted.h b/AK/RefCounted.h index 12a21cc8cd..0cf359d98a 100644 --- a/AK/RefCounted.h +++ b/AK/RefCounted.h @@ -57,25 +57,25 @@ constexpr auto call_one_ref_left_if_present(...) -> FalseType class RefCountedBase { public: - void ref() const + ALWAYS_INLINE void ref() const { ASSERT(m_ref_count); ++m_ref_count; } - int ref_count() const + ALWAYS_INLINE int ref_count() const { return m_ref_count; } protected: RefCountedBase() {} - ~RefCountedBase() + ALWAYS_INLINE ~RefCountedBase() { ASSERT(!m_ref_count); } - void deref_base() const + ALWAYS_INLINE void deref_base() const { ASSERT(m_ref_count); --m_ref_count; diff --git a/AK/RefPtr.h b/AK/RefPtr.h index 3c69456526..394d786919 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -63,21 +63,21 @@ public: : m_ptr(other.leak_ref()) { } - RefPtr(const NonnullRefPtr& other) + ALWAYS_INLINE RefPtr(const NonnullRefPtr& other) : m_ptr(const_cast(other.ptr())) { ASSERT(m_ptr); m_ptr->ref(); } template - RefPtr(const NonnullRefPtr& other) + ALWAYS_INLINE RefPtr(const NonnullRefPtr& other) : m_ptr(const_cast(other.ptr())) { ASSERT(m_ptr); m_ptr->ref(); } template - RefPtr(NonnullRefPtr&& other) + ALWAYS_INLINE RefPtr(NonnullRefPtr&& other) : m_ptr(&other.leak_ref()) { ASSERT(m_ptr); @@ -98,7 +98,7 @@ public: { ref_if_not_null(m_ptr); } - ~RefPtr() + ALWAYS_INLINE ~RefPtr() { clear(); #ifdef SANITIZE_PTRS @@ -121,7 +121,7 @@ public: ::swap(m_ptr, other.m_ptr); } - RefPtr& operator=(RefPtr&& other) + ALWAYS_INLINE RefPtr& operator=(RefPtr&& other) { RefPtr tmp = move(other); swap(tmp); @@ -129,7 +129,7 @@ public: } template - RefPtr& operator=(RefPtr&& other) + ALWAYS_INLINE RefPtr& operator=(RefPtr&& other) { RefPtr tmp = move(other); swap(tmp); @@ -137,7 +137,7 @@ public: } template - RefPtr& operator=(NonnullRefPtr&& other) + ALWAYS_INLINE RefPtr& operator=(NonnullRefPtr&& other) { RefPtr tmp = move(other); swap(tmp); @@ -145,7 +145,7 @@ public: return *this; } - RefPtr& operator=(const NonnullRefPtr& other) + ALWAYS_INLINE RefPtr& operator=(const NonnullRefPtr& other) { RefPtr tmp = other; swap(tmp); @@ -154,7 +154,7 @@ public: } template - RefPtr& operator=(const NonnullRefPtr& other) + ALWAYS_INLINE RefPtr& operator=(const NonnullRefPtr& other) { RefPtr tmp = other; swap(tmp); @@ -162,7 +162,7 @@ public: return *this; } - RefPtr& operator=(const RefPtr& other) + ALWAYS_INLINE RefPtr& operator=(const RefPtr& other) { RefPtr tmp = other; swap(tmp); @@ -170,21 +170,21 @@ public: } template - RefPtr& operator=(const RefPtr& other) + ALWAYS_INLINE RefPtr& operator=(const RefPtr& other) { RefPtr tmp = other; swap(tmp); return *this; } - RefPtr& operator=(const T* ptr) + ALWAYS_INLINE RefPtr& operator=(const T* ptr) { RefPtr tmp = ptr; swap(tmp); return *this; } - RefPtr& operator=(const T& object) + ALWAYS_INLINE RefPtr& operator=(const T& object) { RefPtr tmp = object; swap(tmp); @@ -197,7 +197,7 @@ public: return *this; } - void clear() + ALWAYS_INLINE void clear() { unref_if_not_null(m_ptr); m_ptr = nullptr; @@ -216,35 +216,35 @@ public: return NonnullRefPtr(NonnullRefPtr::Adopt, *leak_ref()); } - T* ptr() { return m_ptr; } - const T* ptr() const { return m_ptr; } + ALWAYS_INLINE T* ptr() { return m_ptr; } + ALWAYS_INLINE const T* ptr() const { return m_ptr; } - T* operator->() + ALWAYS_INLINE T* operator->() { ASSERT(m_ptr); return m_ptr; } - const T* operator->() const + ALWAYS_INLINE const T* operator->() const { ASSERT(m_ptr); return m_ptr; } - T& operator*() + ALWAYS_INLINE T& operator*() { ASSERT(m_ptr); return *m_ptr; } - const T& operator*() const + ALWAYS_INLINE const T& operator*() const { ASSERT(m_ptr); return *m_ptr; } - operator const T*() const { return m_ptr; } - operator T*() { return m_ptr; } + ALWAYS_INLINE operator const T*() const { return m_ptr; } + ALWAYS_INLINE operator T*() { return m_ptr; } operator bool() { return !!m_ptr; } diff --git a/Kernel/KResult.h b/Kernel/KResult.h index 12c974d172..43831fe258 100644 --- a/Kernel/KResult.h +++ b/Kernel/KResult.h @@ -69,13 +69,13 @@ public: { } - KResultOr(T&& value) + ALWAYS_INLINE KResultOr(T&& value) { new (&m_storage) T(move(value)); } template - KResultOr(U&& value) + ALWAYS_INLINE KResultOr(U&& value) { new (&m_storage) T(move(value)); } diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index 6702d00564..1f7f49a94a 100644 --- a/Kernel/Thread.cpp +++ b/Kernel/Thread.cpp @@ -384,11 +384,6 @@ void Thread::send_urgent_signal_to_self(u8 signal) (void)block(SemiPermanentBlocker::Reason::Signal); } -bool Thread::has_unmasked_pending_signals() const -{ - return m_pending_signals & ~m_signal_mask; -} - ShouldUnblockThread Thread::dispatch_one_pending_signal() { ASSERT_INTERRUPTS_DISABLED(); diff --git a/Kernel/Thread.h b/Kernel/Thread.h index 74ebee2cee..d1959ddc63 100644 --- a/Kernel/Thread.h +++ b/Kernel/Thread.h @@ -362,7 +362,7 @@ public: ShouldUnblockThread dispatch_one_pending_signal(); ShouldUnblockThread dispatch_signal(u8 signal); - bool has_unmasked_pending_signals() const; + bool has_unmasked_pending_signals() const { return m_pending_signals & ~m_signal_mask; } void terminate_due_to_signal(u8 signal); bool should_ignore_signal(u8 signal) const; bool has_signal_handler(u8 signal) const;