net/art: use more intelligible, go-ish state tracking in table.Get

Updates #7781

Signed-off-by: David Anderson <danderson@tailscale.com>
pull/8701/head
David Anderson 1 year ago committed by Dave Anderson
parent 4f14ed2ad6
commit 6afffece8a

@ -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 // 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 // comparisons in the edge case (where M is the number of strideTables in
// the path with a non-nil route of their own). // the path with a non-nil route of their own).
strideIdx := 0 const maxDepth = 16
stridePrefixes := [16]netip.Prefix{} type prefixAndRoute struct {
strideRoutes := [16]*T{} prefix netip.Prefix
route *T
}
strideMatch := make([]prefixAndRoute, 0, maxDepth)
findLeaf: findLeaf:
for { for {
rt, child := st.getValAndChild(bs[i]) rt, child := st.getValAndChild(bs[i])
if rt != nil { if rt != nil {
// This strideTable contains a route that may be relevant to our // This strideTable contains a route that may be relevant to our
// search, remember it. // search, remember it.
stridePrefixes[strideIdx] = st.prefix strideMatch = append(strideMatch, prefixAndRoute{st.prefix, rt})
strideRoutes[strideIdx] = rt
strideIdx++
} }
if child == nil { if child == nil {
// No sub-routes further down, the last thing we recorded // 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 // 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 // return on the first loop iteration because the last route we recorded was
// the correct most-specific route. // the correct most-specific route.
for strideIdx > 0 { for i := len(strideMatch) - 1; i >= 0; i-- {
strideIdx-- if m := strideMatch[i]; m.prefix.Contains(addr) {
if stridePrefixes[strideIdx].Contains(addr) { return m.route
return strideRoutes[strideIdx]
} }
} }

Loading…
Cancel
Save