Skip to content

Commit

Permalink
revert lookup tbl for prefixToBaseIndex
Browse files Browse the repository at this point in the history
- reduce mem size for baseIdxLookupTbl
- simplify code even further for LookupPrefix
  • Loading branch information
gaissmai committed Jun 4, 2024
1 parent 3123746 commit f168a09
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 298 deletions.
296 changes: 17 additions & 279 deletions base_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ const (
// prefixToBaseIndex, maps a prefix table as a 'complete binary tree'.
// This is the so-called baseIndex a.k.a heapFunc:
func prefixToBaseIndex(octet byte, prefixLen int) uint {
// uint(octet>>(strideLen-prefixLen)) + (1 << prefixLen)
//
// Use the pre computed lookup table.
//
return prefixIdxLookupTbl[octet][prefixLen]
return uint(octet>>(strideLen-prefixLen)) + (1 << prefixLen)
}

// octetToBaseIndex, just prefixToBaseIndex(octet, 8), a.k.a host routes
Expand Down Expand Up @@ -65,7 +61,7 @@ func baseIndexToPrefixMask(baseIdx uint, depth int) int {
// }
func hostRoutesByIndex(idx uint) (uint, uint) {
item := baseIdxLookupTbl[idx]
return item.lower, item.upper
return uint(item.lower), uint(item.upper)
}

// baseIndexToPrefix returns the octet and prefix len of baseIdx.
Expand All @@ -81,22 +77,28 @@ func hostRoutesByIndex(idx uint) (uint, uint) {
// }
func baseIndexToPrefix(baseIdx uint) (octet byte, pfxLen int) {
item := baseIdxLookupTbl[baseIdx]
return item.octet, item.bits
return item.octet, int(item.bits)
}

// prefixSortRankByIndex, get the prefix sort rank for baseIndex
// prefixSortRankByIndex, get the prefix sort rank for baseIndex.
// Use the pre computed lookup table.
func prefixSortRankByIndex(baseIdx uint) int {
return baseIdxLookupTbl[baseIdx].rank
return int(baseIdxLookupTbl[baseIdx].rank)
}

// baseIdxLookupTbl,
// octet, bits and host route boundaries of baseIdx as lookup table
// baseIdxLookupTbl
//
// octet, bits,
// host route boundaries,
// prefix sort rank
//
// as lookup table.
var baseIdxLookupTbl = [512]struct {
octet byte
bits int
lower uint // host route lower bound
upper uint // host route upper bound
rank int // prefix sort order rank
bits int8
lower uint16 // host route lower bound
upper uint16 // host route upper bound
rank uint16 // prefix sort rank
}{
{0, -1, 0, 0, 0}, // idx == 0 invalid!
{0, 0, 256, 511, 1}, // idx == 1
Expand Down Expand Up @@ -611,267 +613,3 @@ var baseIdxLookupTbl = [512]struct {
{254, 8, 510, 510, 510}, // idx == 510
{255, 8, 511, 511, 511}, // idx == 511
}

const maxPrefix int = 256
const maxBits int = 9

// prefixIdxLookupTbl, pre calculated lookup table
// [prefix][bits] -> baseIndex
var prefixIdxLookupTbl = [maxPrefix][maxBits]uint{
[9]uint{1, 2, 4, 8, 16, 32, 64, 128, 256}, // pfx == 0
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 257}, // pfx == 1
[9]uint{0, 0, 0, 0, 0, 0, 0, 129, 258}, // pfx == 2
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 259}, // pfx == 3
[9]uint{0, 0, 0, 0, 0, 0, 65, 130, 260}, // pfx == 4
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 261}, // pfx == 5
[9]uint{0, 0, 0, 0, 0, 0, 0, 131, 262}, // pfx == 6
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 263}, // pfx == 7
[9]uint{0, 0, 0, 0, 0, 33, 66, 132, 264}, // pfx == 8
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 265}, // pfx == 9
[9]uint{0, 0, 0, 0, 0, 0, 0, 133, 266}, // pfx == 10
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 267}, // pfx == 11
[9]uint{0, 0, 0, 0, 0, 0, 67, 134, 268}, // pfx == 12
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 269}, // pfx == 13
[9]uint{0, 0, 0, 0, 0, 0, 0, 135, 270}, // pfx == 14
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 271}, // pfx == 15
[9]uint{0, 0, 0, 0, 17, 34, 68, 136, 272}, // pfx == 16
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 273}, // pfx == 17
[9]uint{0, 0, 0, 0, 0, 0, 0, 137, 274}, // pfx == 18
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 275}, // pfx == 19
[9]uint{0, 0, 0, 0, 0, 0, 69, 138, 276}, // pfx == 20
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 277}, // pfx == 21
[9]uint{0, 0, 0, 0, 0, 0, 0, 139, 278}, // pfx == 22
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 279}, // pfx == 23
[9]uint{0, 0, 0, 0, 0, 35, 70, 140, 280}, // pfx == 24
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 281}, // pfx == 25
[9]uint{0, 0, 0, 0, 0, 0, 0, 141, 282}, // pfx == 26
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 283}, // pfx == 27
[9]uint{0, 0, 0, 0, 0, 0, 71, 142, 284}, // pfx == 28
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 285}, // pfx == 29
[9]uint{0, 0, 0, 0, 0, 0, 0, 143, 286}, // pfx == 30
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 287}, // pfx == 31
[9]uint{0, 0, 0, 9, 18, 36, 72, 144, 288}, // pfx == 32
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 289}, // pfx == 33
[9]uint{0, 0, 0, 0, 0, 0, 0, 145, 290}, // pfx == 34
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 291}, // pfx == 35
[9]uint{0, 0, 0, 0, 0, 0, 73, 146, 292}, // pfx == 36
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 293}, // pfx == 37
[9]uint{0, 0, 0, 0, 0, 0, 0, 147, 294}, // pfx == 38
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 295}, // pfx == 39
[9]uint{0, 0, 0, 0, 0, 37, 74, 148, 296}, // pfx == 40
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 297}, // pfx == 41
[9]uint{0, 0, 0, 0, 0, 0, 0, 149, 298}, // pfx == 42
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 299}, // pfx == 43
[9]uint{0, 0, 0, 0, 0, 0, 75, 150, 300}, // pfx == 44
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 301}, // pfx == 45
[9]uint{0, 0, 0, 0, 0, 0, 0, 151, 302}, // pfx == 46
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 303}, // pfx == 47
[9]uint{0, 0, 0, 0, 19, 38, 76, 152, 304}, // pfx == 48
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 305}, // pfx == 49
[9]uint{0, 0, 0, 0, 0, 0, 0, 153, 306}, // pfx == 50
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 307}, // pfx == 51
[9]uint{0, 0, 0, 0, 0, 0, 77, 154, 308}, // pfx == 52
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 309}, // pfx == 53
[9]uint{0, 0, 0, 0, 0, 0, 0, 155, 310}, // pfx == 54
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 311}, // pfx == 55
[9]uint{0, 0, 0, 0, 0, 39, 78, 156, 312}, // pfx == 56
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 313}, // pfx == 57
[9]uint{0, 0, 0, 0, 0, 0, 0, 157, 314}, // pfx == 58
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 315}, // pfx == 59
[9]uint{0, 0, 0, 0, 0, 0, 79, 158, 316}, // pfx == 60
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 317}, // pfx == 61
[9]uint{0, 0, 0, 0, 0, 0, 0, 159, 318}, // pfx == 62
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 319}, // pfx == 63
[9]uint{0, 0, 5, 10, 20, 40, 80, 160, 320}, // pfx == 64
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 321}, // pfx == 65
[9]uint{0, 0, 0, 0, 0, 0, 0, 161, 322}, // pfx == 66
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 323}, // pfx == 67
[9]uint{0, 0, 0, 0, 0, 0, 81, 162, 324}, // pfx == 68
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 325}, // pfx == 69
[9]uint{0, 0, 0, 0, 0, 0, 0, 163, 326}, // pfx == 70
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 327}, // pfx == 71
[9]uint{0, 0, 0, 0, 0, 41, 82, 164, 328}, // pfx == 72
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 329}, // pfx == 73
[9]uint{0, 0, 0, 0, 0, 0, 0, 165, 330}, // pfx == 74
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 331}, // pfx == 75
[9]uint{0, 0, 0, 0, 0, 0, 83, 166, 332}, // pfx == 76
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 333}, // pfx == 77
[9]uint{0, 0, 0, 0, 0, 0, 0, 167, 334}, // pfx == 78
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 335}, // pfx == 79
[9]uint{0, 0, 0, 0, 21, 42, 84, 168, 336}, // pfx == 80
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 337}, // pfx == 81
[9]uint{0, 0, 0, 0, 0, 0, 0, 169, 338}, // pfx == 82
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 339}, // pfx == 83
[9]uint{0, 0, 0, 0, 0, 0, 85, 170, 340}, // pfx == 84
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 341}, // pfx == 85
[9]uint{0, 0, 0, 0, 0, 0, 0, 171, 342}, // pfx == 86
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 343}, // pfx == 87
[9]uint{0, 0, 0, 0, 0, 43, 86, 172, 344}, // pfx == 88
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 345}, // pfx == 89
[9]uint{0, 0, 0, 0, 0, 0, 0, 173, 346}, // pfx == 90
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 347}, // pfx == 91
[9]uint{0, 0, 0, 0, 0, 0, 87, 174, 348}, // pfx == 92
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 349}, // pfx == 93
[9]uint{0, 0, 0, 0, 0, 0, 0, 175, 350}, // pfx == 94
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 351}, // pfx == 95
[9]uint{0, 0, 0, 11, 22, 44, 88, 176, 352}, // pfx == 96
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 353}, // pfx == 97
[9]uint{0, 0, 0, 0, 0, 0, 0, 177, 354}, // pfx == 98
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 355}, // pfx == 99
[9]uint{0, 0, 0, 0, 0, 0, 89, 178, 356}, // pfx == 100
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 357}, // pfx == 101
[9]uint{0, 0, 0, 0, 0, 0, 0, 179, 358}, // pfx == 102
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 359}, // pfx == 103
[9]uint{0, 0, 0, 0, 0, 45, 90, 180, 360}, // pfx == 104
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 361}, // pfx == 105
[9]uint{0, 0, 0, 0, 0, 0, 0, 181, 362}, // pfx == 106
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 363}, // pfx == 107
[9]uint{0, 0, 0, 0, 0, 0, 91, 182, 364}, // pfx == 108
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 365}, // pfx == 109
[9]uint{0, 0, 0, 0, 0, 0, 0, 183, 366}, // pfx == 110
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 367}, // pfx == 111
[9]uint{0, 0, 0, 0, 23, 46, 92, 184, 368}, // pfx == 112
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 369}, // pfx == 113
[9]uint{0, 0, 0, 0, 0, 0, 0, 185, 370}, // pfx == 114
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 371}, // pfx == 115
[9]uint{0, 0, 0, 0, 0, 0, 93, 186, 372}, // pfx == 116
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 373}, // pfx == 117
[9]uint{0, 0, 0, 0, 0, 0, 0, 187, 374}, // pfx == 118
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 375}, // pfx == 119
[9]uint{0, 0, 0, 0, 0, 47, 94, 188, 376}, // pfx == 120
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 377}, // pfx == 121
[9]uint{0, 0, 0, 0, 0, 0, 0, 189, 378}, // pfx == 122
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 379}, // pfx == 123
[9]uint{0, 0, 0, 0, 0, 0, 95, 190, 380}, // pfx == 124
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 381}, // pfx == 125
[9]uint{0, 0, 0, 0, 0, 0, 0, 191, 382}, // pfx == 126
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 383}, // pfx == 127
[9]uint{0, 3, 6, 12, 24, 48, 96, 192, 384}, // pfx == 128
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 385}, // pfx == 129
[9]uint{0, 0, 0, 0, 0, 0, 0, 193, 386}, // pfx == 130
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 387}, // pfx == 131
[9]uint{0, 0, 0, 0, 0, 0, 97, 194, 388}, // pfx == 132
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 389}, // pfx == 133
[9]uint{0, 0, 0, 0, 0, 0, 0, 195, 390}, // pfx == 134
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 391}, // pfx == 135
[9]uint{0, 0, 0, 0, 0, 49, 98, 196, 392}, // pfx == 136
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 393}, // pfx == 137
[9]uint{0, 0, 0, 0, 0, 0, 0, 197, 394}, // pfx == 138
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 395}, // pfx == 139
[9]uint{0, 0, 0, 0, 0, 0, 99, 198, 396}, // pfx == 140
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 397}, // pfx == 141
[9]uint{0, 0, 0, 0, 0, 0, 0, 199, 398}, // pfx == 142
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 399}, // pfx == 143
[9]uint{0, 0, 0, 0, 25, 50, 100, 200, 400}, // pfx == 144
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 401}, // pfx == 145
[9]uint{0, 0, 0, 0, 0, 0, 0, 201, 402}, // pfx == 146
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 403}, // pfx == 147
[9]uint{0, 0, 0, 0, 0, 0, 101, 202, 404}, // pfx == 148
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 405}, // pfx == 149
[9]uint{0, 0, 0, 0, 0, 0, 0, 203, 406}, // pfx == 150
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 407}, // pfx == 151
[9]uint{0, 0, 0, 0, 0, 51, 102, 204, 408}, // pfx == 152
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 409}, // pfx == 153
[9]uint{0, 0, 0, 0, 0, 0, 0, 205, 410}, // pfx == 154
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 411}, // pfx == 155
[9]uint{0, 0, 0, 0, 0, 0, 103, 206, 412}, // pfx == 156
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 413}, // pfx == 157
[9]uint{0, 0, 0, 0, 0, 0, 0, 207, 414}, // pfx == 158
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 415}, // pfx == 159
[9]uint{0, 0, 0, 13, 26, 52, 104, 208, 416}, // pfx == 160
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 417}, // pfx == 161
[9]uint{0, 0, 0, 0, 0, 0, 0, 209, 418}, // pfx == 162
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 419}, // pfx == 163
[9]uint{0, 0, 0, 0, 0, 0, 105, 210, 420}, // pfx == 164
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 421}, // pfx == 165
[9]uint{0, 0, 0, 0, 0, 0, 0, 211, 422}, // pfx == 166
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 423}, // pfx == 167
[9]uint{0, 0, 0, 0, 0, 53, 106, 212, 424}, // pfx == 168
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 425}, // pfx == 169
[9]uint{0, 0, 0, 0, 0, 0, 0, 213, 426}, // pfx == 170
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 427}, // pfx == 171
[9]uint{0, 0, 0, 0, 0, 0, 107, 214, 428}, // pfx == 172
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 429}, // pfx == 173
[9]uint{0, 0, 0, 0, 0, 0, 0, 215, 430}, // pfx == 174
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 431}, // pfx == 175
[9]uint{0, 0, 0, 0, 27, 54, 108, 216, 432}, // pfx == 176
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 433}, // pfx == 177
[9]uint{0, 0, 0, 0, 0, 0, 0, 217, 434}, // pfx == 178
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 435}, // pfx == 179
[9]uint{0, 0, 0, 0, 0, 0, 109, 218, 436}, // pfx == 180
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 437}, // pfx == 181
[9]uint{0, 0, 0, 0, 0, 0, 0, 219, 438}, // pfx == 182
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 439}, // pfx == 183
[9]uint{0, 0, 0, 0, 0, 55, 110, 220, 440}, // pfx == 184
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 441}, // pfx == 185
[9]uint{0, 0, 0, 0, 0, 0, 0, 221, 442}, // pfx == 186
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 443}, // pfx == 187
[9]uint{0, 0, 0, 0, 0, 0, 111, 222, 444}, // pfx == 188
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 445}, // pfx == 189
[9]uint{0, 0, 0, 0, 0, 0, 0, 223, 446}, // pfx == 190
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 447}, // pfx == 191
[9]uint{0, 0, 7, 14, 28, 56, 112, 224, 448}, // pfx == 192
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 449}, // pfx == 193
[9]uint{0, 0, 0, 0, 0, 0, 0, 225, 450}, // pfx == 194
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 451}, // pfx == 195
[9]uint{0, 0, 0, 0, 0, 0, 113, 226, 452}, // pfx == 196
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 453}, // pfx == 197
[9]uint{0, 0, 0, 0, 0, 0, 0, 227, 454}, // pfx == 198
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 455}, // pfx == 199
[9]uint{0, 0, 0, 0, 0, 57, 114, 228, 456}, // pfx == 200
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 457}, // pfx == 201
[9]uint{0, 0, 0, 0, 0, 0, 0, 229, 458}, // pfx == 202
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 459}, // pfx == 203
[9]uint{0, 0, 0, 0, 0, 0, 115, 230, 460}, // pfx == 204
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 461}, // pfx == 205
[9]uint{0, 0, 0, 0, 0, 0, 0, 231, 462}, // pfx == 206
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 463}, // pfx == 207
[9]uint{0, 0, 0, 0, 29, 58, 116, 232, 464}, // pfx == 208
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 465}, // pfx == 209
[9]uint{0, 0, 0, 0, 0, 0, 0, 233, 466}, // pfx == 210
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 467}, // pfx == 211
[9]uint{0, 0, 0, 0, 0, 0, 117, 234, 468}, // pfx == 212
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 469}, // pfx == 213
[9]uint{0, 0, 0, 0, 0, 0, 0, 235, 470}, // pfx == 214
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 471}, // pfx == 215
[9]uint{0, 0, 0, 0, 0, 59, 118, 236, 472}, // pfx == 216
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 473}, // pfx == 217
[9]uint{0, 0, 0, 0, 0, 0, 0, 237, 474}, // pfx == 218
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 475}, // pfx == 219
[9]uint{0, 0, 0, 0, 0, 0, 119, 238, 476}, // pfx == 220
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 477}, // pfx == 221
[9]uint{0, 0, 0, 0, 0, 0, 0, 239, 478}, // pfx == 222
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 479}, // pfx == 223
[9]uint{0, 0, 0, 15, 30, 60, 120, 240, 480}, // pfx == 224
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 481}, // pfx == 225
[9]uint{0, 0, 0, 0, 0, 0, 0, 241, 482}, // pfx == 226
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 483}, // pfx == 227
[9]uint{0, 0, 0, 0, 0, 0, 121, 242, 484}, // pfx == 228
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 485}, // pfx == 229
[9]uint{0, 0, 0, 0, 0, 0, 0, 243, 486}, // pfx == 230
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 487}, // pfx == 231
[9]uint{0, 0, 0, 0, 0, 61, 122, 244, 488}, // pfx == 232
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 489}, // pfx == 233
[9]uint{0, 0, 0, 0, 0, 0, 0, 245, 490}, // pfx == 234
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 491}, // pfx == 235
[9]uint{0, 0, 0, 0, 0, 0, 123, 246, 492}, // pfx == 236
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 493}, // pfx == 237
[9]uint{0, 0, 0, 0, 0, 0, 0, 247, 494}, // pfx == 238
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 495}, // pfx == 239
[9]uint{0, 0, 0, 0, 31, 62, 124, 248, 496}, // pfx == 240
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 497}, // pfx == 241
[9]uint{0, 0, 0, 0, 0, 0, 0, 249, 498}, // pfx == 242
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 499}, // pfx == 243
[9]uint{0, 0, 0, 0, 0, 0, 125, 250, 500}, // pfx == 244
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 501}, // pfx == 245
[9]uint{0, 0, 0, 0, 0, 0, 0, 251, 502}, // pfx == 246
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 503}, // pfx == 247
[9]uint{0, 0, 0, 0, 0, 63, 126, 252, 504}, // pfx == 248
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 505}, // pfx == 249
[9]uint{0, 0, 0, 0, 0, 0, 0, 253, 506}, // pfx == 250
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 507}, // pfx == 251
[9]uint{0, 0, 0, 0, 0, 0, 127, 254, 508}, // pfx == 252
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 509}, // pfx == 253
[9]uint{0, 0, 0, 0, 0, 0, 0, 255, 510}, // pfx == 254
[9]uint{0, 0, 0, 0, 0, 0, 0, 0, 511}, // pfx == 255
}
38 changes: 19 additions & 19 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,9 @@ func (t *Table[V]) Lookup(ip netip.Addr) (val V, ok bool) {
}

// start backtracking, unwind the stack
for j := i; j >= 0; j-- {
n = stack[j]
octet = octets[j]
for depth := i; depth >= 0; depth-- {
n = stack[depth]
octet = octets[depth]

// longest prefix match
// micro benchmarking: skip if node has no prefixes
Expand All @@ -372,7 +372,7 @@ func (t *Table[V]) Lookup(ip netip.Addr) (val V, ok bool) {
// The prefix must be in normalized form!
func (t *Table[V]) LookupPrefix(pfx netip.Prefix) (val V, ok bool) {
_, _, val, ok = t.lpmPrefix(pfx)
return
return val, ok
}

// LookupPrefixLPM is similar to [Table.LookupPrefix],
Expand Down Expand Up @@ -444,29 +444,29 @@ func (t *Table[V]) lpmPrefix(pfx netip.Prefix) (depth int, baseIdx uint, val V,
n = c
}

// only the lastOctet may have a different prefix len
pfxLen := strideLen
if i == lastOctetIdx {
pfxLen = lastOctetBits
}

// start backtracking, unwind the stack
for j := i; j >= 0; j-- {
n = stack[j]
octet = octets[j]
for depth = i; depth >= 0; depth-- {
n = stack[depth]
octet = octets[depth]

// longest prefix match
// micro benchmarking: skip if node has no prefixes
if len(n.prefixes) != 0 {

if baseIdx, val, ok = n.lpm(prefixToBaseIndex(octet, pfxLen)); ok {
depth = j
return
// only the lastOctet may have a different prefix len
// all others are just host routes
idx := uint(0)
if depth == lastOctetIdx {
idx = prefixToBaseIndex(octet, lastOctetBits)
} else {
idx = octetToBaseIndex(octet)
}
}

// for all upper levels, just host routes
pfxLen = strideLen
baseIdx, val, ok = n.lpm(idx)
if ok {
return depth, baseIdx, val, ok
}
}
}
return
}
Expand Down

0 comments on commit f168a09

Please sign in to comment.