summaryrefslogtreecommitdiff
path: root/Kernel/Net
diff options
context:
space:
mode:
authorGunnar Beutner <gbeutner@serenityos.org>2021-05-12 17:27:37 +0200
committerAndreas Kling <kling@serenityos.org>2021-05-13 08:27:42 +0200
commit1bb20a255fd0b7196250bb2b984407333e5983dd (patch)
treecedf0efd8d07f07efab69a9938ecd4d821ba3e3d /Kernel/Net
parent76deda802d8b40f39e4818ba26b9a27f9304df18 (diff)
downloadserenity-1bb20a255fd0b7196250bb2b984407333e5983dd.zip
Kernel: Avoid unnecessary allocations in NetworkAdapter::for_each
This avoids allocations for initializing the Function<T> for the NetworkAdapter::for_each callback argument. Applying this patch decreases CPU utilization for NetworkTask from 40% to 28% when receiving TCP packets at a rate of 100Mbit/s.
Diffstat (limited to 'Kernel/Net')
-rw-r--r--Kernel/Net/NetworkAdapter.cpp9
-rw-r--r--Kernel/Net/NetworkAdapter.h11
2 files changed, 11 insertions, 9 deletions
diff --git a/Kernel/Net/NetworkAdapter.cpp b/Kernel/Net/NetworkAdapter.cpp
index 379a740cc8..e050fcc581 100644
--- a/Kernel/Net/NetworkAdapter.cpp
+++ b/Kernel/Net/NetworkAdapter.cpp
@@ -21,18 +21,11 @@ namespace Kernel {
static AK::Singleton<Lockable<HashTable<NetworkAdapter*>>> s_table;
-static Lockable<HashTable<NetworkAdapter*>>& all_adapters()
+Lockable<HashTable<NetworkAdapter*>>& NetworkAdapter::all_adapters()
{
return *s_table;
}
-void NetworkAdapter::for_each(Function<void(NetworkAdapter&)> callback)
-{
- Locker locker(all_adapters().lock());
- for (auto& it : all_adapters().resource())
- callback(*it);
-}
-
RefPtr<NetworkAdapter> NetworkAdapter::from_ipv4_address(const IPv4Address& address)
{
Locker locker(all_adapters().lock());
diff --git a/Kernel/Net/NetworkAdapter.h b/Kernel/Net/NetworkAdapter.h
index 6e6dcf32b0..d25d3a2c30 100644
--- a/Kernel/Net/NetworkAdapter.h
+++ b/Kernel/Net/NetworkAdapter.h
@@ -25,7 +25,14 @@ class NetworkAdapter;
class NetworkAdapter : public RefCounted<NetworkAdapter> {
public:
- static void for_each(Function<void(NetworkAdapter&)>);
+ template<typename Callback>
+ static inline void for_each(Callback callback)
+ {
+ Locker locker(all_adapters().lock());
+ for (auto& it : all_adapters().resource())
+ callback(*it);
+ }
+
static RefPtr<NetworkAdapter> from_ipv4_address(const IPv4Address&);
static RefPtr<NetworkAdapter> lookup_by_name(const StringView&);
virtual ~NetworkAdapter();
@@ -70,6 +77,8 @@ protected:
void did_receive(ReadonlyBytes);
private:
+ static Lockable<HashTable<NetworkAdapter*>>& all_adapters();
+
MACAddress m_mac_address;
IPv4Address m_ipv4_address;
IPv4Address m_ipv4_netmask;