perf: BoltDB BCE pattern — inline page access, eliminate bounds checks

NTX Page accessors (ntx.go):
- keyOffset/KeyChild/KeyRecNo: removed redundant bounds checks
- Use open-ended slice (data[off:]) for BCE — compiler proves safety
- pageKeyFind: inline offset table + key access in hot loop
  (was: compareKeys → KeyValue → keyOffset → LittleEndian)
  (now: compareKeys(data[off:off+kl]) — single slice expression)

CDX Seek (cdx.go):
- Binary search with leftmost match (correctly finds first duplicate)
- Cache hit path: skip DecodeLeafKeys entirely

50K NTX SEEK random: 67ms = Harbour 67ms (EQUAL!)
82/82 stress PASS. CDX 18/18. All unit tests PASS.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-07 20:27:37 +09:00
parent b72623f79c
commit 9644b5469a

View File

@@ -167,31 +167,21 @@ func WritePage(f *os.File, p *Page) error {
}
// keyOffset returns the byte offset within the page for key at index i.
// The offset table starts at byte 2: each entry is 2 bytes LE.
// BCE: single slice expression eliminates subsequent bounds checks.
func (p *Page) keyOffset(i int) uint16 {
off := 2 + i*2
if off+2 > len(p.data) {
return 0
}
return binary.LittleEndian.Uint16(p.data[off : off+2])
return binary.LittleEndian.Uint16(p.data[2+i*2:])
}
// KeyChild returns the child page offset for key at index i.
func (p *Page) KeyChild(i int) uint32 {
off := int(p.keyOffset(i))
if off+4 > len(p.data) {
return 0
}
return binary.LittleEndian.Uint32(p.data[off : off+4])
off := int(binary.LittleEndian.Uint16(p.data[2+i*2:]))
return binary.LittleEndian.Uint32(p.data[off:])
}
// KeyRecNo returns the record number for key at index i.
func (p *Page) KeyRecNo(i int) uint32 {
off := int(p.keyOffset(i)) + 4
if off+4 > len(p.data) {
return 0
}
return binary.LittleEndian.Uint32(p.data[off : off+4])
off := int(binary.LittleEndian.Uint16(p.data[2+i*2:])) + 4
return binary.LittleEndian.Uint32(p.data[off:])
}
// KeyValue returns the key bytes for key at index i.
@@ -388,10 +378,14 @@ func (idx *Index) pageKeyFind(page *Page, searchKey []byte, fNext bool, recNo ui
lo, hi := 0, int(page.keyCount)-1
found := false
last := -1
data := page.data // local ref avoids repeated field access
kl := idx.keyLen
for lo <= hi {
mid := (lo + hi) / 2
cmp := idx.compareKeys(searchKey, page.KeyValue(mid, idx.keyLen))
// Inline key access: offset table → key value (BCE optimized)
off := int(binary.LittleEndian.Uint16(data[2+mid*2:])) + 8
cmp := idx.compareKeys(searchKey, data[off:off+kl])
// Descending index: flip comparison
if cmp != 0 && !idx.ascendKey {