From 08cad808ef8da10f023cb67f63f714721a2122a3 Mon Sep 17 00:00:00 2001 From: bonedaddy Date: Tue, 22 Dec 2020 17:20:58 -0800 Subject: [PATCH] fix slice memory allocation optimization --- network/wifi.go | 6 +++--- network/wifi_ap.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/network/wifi.go b/network/wifi.go index 0a452d20..797e9009 100644 --- a/network/wifi.go +++ b/network/wifi.go @@ -72,7 +72,7 @@ func (w *WiFi) MarshalJSON() ([]byte, error) { doc := wifiJSON{ // we know the length so preallocate to reduce memory allocations - AccessPoints: make([]*AccessPoint, len(w.aps)), + AccessPoints: make([]*AccessPoint, 0, len(w.aps)), } for _, ap := range w.aps { @@ -95,7 +95,7 @@ func (w *WiFi) Stations() (list []*Station) { w.RLock() defer w.RUnlock() - list = make([]*Station, len(w.aps)) + list = make([]*Station, 0, len(w.aps)) for _, ap := range w.aps { list = append(list, ap.Station) @@ -107,7 +107,7 @@ func (w *WiFi) List() (list []*AccessPoint) { w.RLock() defer w.RUnlock() - list = make([]*AccessPoint, len(w.aps)) + list = make([]*AccessPoint, 0, len(w.aps)) for _, ap := range w.aps { list = append(list, ap) diff --git a/network/wifi_ap.go b/network/wifi_ap.go index fc889036..ed17f681 100644 --- a/network/wifi_ap.go +++ b/network/wifi_ap.go @@ -37,7 +37,7 @@ func (ap *AccessPoint) MarshalJSON() ([]byte, error) { doc := apJSON{ Station: ap.Station, - Clients: make([]*Station, len(ap.clients)), + Clients: make([]*Station, 0, len(ap.clients)), Handshake: ap.withKeyMaterial, } @@ -106,7 +106,7 @@ func (ap *AccessPoint) Clients() (list []*Station) { ap.RLock() defer ap.RUnlock() - list = make([]*Station, len(ap.clients)) + list = make([]*Station, 0, len(ap.clients)) for _, c := range ap.clients { list = append(list, c) }