From 750dbe986de0b80772c1bd32627850b1105c0e60 Mon Sep 17 00:00:00 2001 From: Robin Burchell Date: Fri, 19 Jul 2019 09:04:12 +0200 Subject: [PATCH] Kernel: Avoid allocations for Select vectors by using inline capacity Good tip by Andreas :) --- Kernel/Process.cpp | 12 ++++++------ Kernel/Scheduler.cpp | 2 +- Kernel/Thread.h | 10 ++++++---- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index b2e2f7ebcc..07141a7ba3 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -1798,9 +1798,9 @@ int Process::sys$select(const Syscall::SC_select_params* params) select_has_timeout = true; } - Vector rfds; - Vector wfds; - Vector efds; + Thread::SelectBlocker::FDVector rfds; + Thread::SelectBlocker::FDVector wfds; + Thread::SelectBlocker::FDVector efds; auto transfer_fds = [&](auto* fds, auto& vector) -> int { vector.clear_with_capacity(); @@ -1852,8 +1852,8 @@ int Process::sys$poll(pollfd* fds, int nfds, int timeout) if (!validate_read_typed(fds)) return -EFAULT; - Vector rfds; - Vector wfds; + Thread::SelectBlocker::FDVector rfds; + Thread::SelectBlocker::FDVector wfds; for (int i = 0; i < nfds; ++i) { if (fds[i].events & POLLIN) @@ -1882,7 +1882,7 @@ int Process::sys$poll(pollfd* fds, int nfds, int timeout) #endif if (has_timeout|| timeout < 0) - current->block(*new Thread::SelectBlocker(actual_timeout, has_timeout, rfds, wfds, Vector())); + current->block(*new Thread::SelectBlocker(actual_timeout, has_timeout, rfds, wfds, Thread::SelectBlocker::FDVector())); int fds_with_revents = 0; diff --git a/Kernel/Scheduler.cpp b/Kernel/Scheduler.cpp index 070c40584e..60c0819f4d 100644 --- a/Kernel/Scheduler.cpp +++ b/Kernel/Scheduler.cpp @@ -143,7 +143,7 @@ bool Thread::SleepBlocker::should_unblock(Thread&, time_t, long) return m_wakeup_time <= g_uptime; } -Thread::SelectBlocker::SelectBlocker(const timeval& tv, bool select_has_timeout, const Vector& read_fds, const Vector& write_fds, const Vector& except_fds) +Thread::SelectBlocker::SelectBlocker(const timeval& tv, bool select_has_timeout, const FDVector& read_fds, const FDVector& write_fds, const FDVector& except_fds) : m_select_timeout(tv) , m_select_has_timeout(select_has_timeout) , m_select_read_fds(read_fds) diff --git a/Kernel/Thread.h b/Kernel/Thread.h index b5e3998836..6144fb8db2 100644 --- a/Kernel/Thread.h +++ b/Kernel/Thread.h @@ -10,6 +10,7 @@ #include #include #include +#include class Alarm; class FileDescription; @@ -135,15 +136,16 @@ public: class SelectBlocker : public Blocker { public: - SelectBlocker(const timeval& tv, bool select_has_timeout, const Vector& read_fds, const Vector& write_fds, const Vector& except_fds); + typedef Vector FDVector; + SelectBlocker(const timeval& tv, bool select_has_timeout, const FDVector& read_fds, const FDVector& write_fds, const FDVector& except_fds); virtual bool should_unblock(Thread&, time_t, long) override; private: timeval m_select_timeout; bool m_select_has_timeout { false }; - const Vector& m_select_read_fds; - const Vector& m_select_write_fds; - const Vector& m_select_exceptional_fds; + const FDVector& m_select_read_fds; + const FDVector& m_select_write_fds; + const FDVector& m_select_exceptional_fds; }; class WaitBlocker : public Blocker {