From 4278d90cb6761b7d840e7d53042c1fe4b752826d Mon Sep 17 00:00:00 2001 From: Oliver Geiselhardt-Herms Date: Fri, 18 Aug 2023 10:24:05 +0200 Subject: [PATCH] Improve performance of interface name lookup --- get.go | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/get.go b/get.go index 24c0a99..c467fd7 100644 --- a/get.go +++ b/get.go @@ -177,7 +177,7 @@ func getQdiscMsgs(c *netlink.Conn) ([]netlink.Message, error) { } // See https://tools.ietf.org/html/rfc3549#section-3.1.3 -func parseMessage(msg netlink.Message) (QdiscInfo, error) { +func parseMessage(msg netlink.Message, ifaceNamesByID map[int]string) (QdiscInfo, error) { var m QdiscInfo var s TC_Stats var s2 TC_Stats2 @@ -257,13 +257,23 @@ func parseMessage(msg netlink.Message) (QdiscInfo, error) { } } - iface, err := net.InterfaceByIndex(int(ifaceIdx)) + m.IfaceName = ifaceNamesByID[int(ifaceIdx)] - if err == nil { - m.IfaceName = iface.Name + return m, err +} + +func getInterfaceNames() (map[int]string, error) { + ifas, err := net.Interfaces() + if err != nil { + return nil, err } - return m, err + ifNamesByID := make(map[int]string) + for _, ifa := range ifas { + ifNamesByID[ifa.Index] = ifa.Name + } + + return ifNamesByID, nil } func getAndParse(c *netlink.Conn) ([]QdiscInfo, error) { @@ -275,8 +285,13 @@ func getAndParse(c *netlink.Conn) ([]QdiscInfo, error) { return nil, err } + ifNamesByID, err := getInterfaceNames() + if err != nil { + return nil, err + } + for _, msg := range msgs { - m, err := parseMessage(msg) + m, err := parseMessage(msg, ifNamesByID) if err != nil { return nil, err