From 6afffece8a32509aa7a4dc2972415ec58d8316de Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 24 Jul 2023 13:21:21 -0700 Subject: [PATCH] net/art: use more intelligible, go-ish state tracking in table.Get Updates #7781 Signed-off-by: David Anderson --- net/art/table.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/net/art/table.go b/net/art/table.go index 7355b1263..42123704d 100644 --- a/net/art/table.go +++ b/net/art/table.go @@ -81,18 +81,19 @@ func (t *Table[T]) Get(addr netip.Addr) *T { // the number of strideTables in the path), rather than only paying M prefix // comparisons in the edge case (where M is the number of strideTables in // the path with a non-nil route of their own). - strideIdx := 0 - stridePrefixes := [16]netip.Prefix{} - strideRoutes := [16]*T{} + const maxDepth = 16 + type prefixAndRoute struct { + prefix netip.Prefix + route *T + } + strideMatch := make([]prefixAndRoute, 0, maxDepth) findLeaf: for { rt, child := st.getValAndChild(bs[i]) if rt != nil { // This strideTable contains a route that may be relevant to our // search, remember it. - stridePrefixes[strideIdx] = st.prefix - strideRoutes[strideIdx] = rt - strideIdx++ + strideMatch = append(strideMatch, prefixAndRoute{st.prefix, rt}) } if child == nil { // No sub-routes further down, the last thing we recorded @@ -112,10 +113,9 @@ findLeaf: // In the common case where path compression did not mislead us, we'll // return on the first loop iteration because the last route we recorded was // the correct most-specific route. - for strideIdx > 0 { - strideIdx-- - if stridePrefixes[strideIdx].Contains(addr) { - return strideRoutes[strideIdx] + for i := len(strideMatch) - 1; i >= 0; i-- { + if m := strideMatch[i]; m.prefix.Contains(addr) { + return m.route } }