feat: session cache, explored bitmap and lock-free tile gate (#65)

The tile gate no longer takes dbLock or runs a SQL session JOIN per tile: a published
in-memory view (server/sessions.nim) holds token hashes -> player/role and one explored
bitmap per player. Sessions load from the DB at startup and are updated by join, key
replacement and player removal in the same lock as the write; refreshed cells publish
before the ack. The gate is one snapshot read (parse + session + bit test + redirect)
over scalar data only, so a tile burst is answered while a mutation holds dbLock.

Evidence: tests/tsessions.nim pins the cache/bitmap lifecycle and gate rule; tiles.test
adds the lock-held burst and the revocation/restart seam; auth.test adds the
LEAK_FAULT=sessioncache negative control (a stale entry makes the auth group fail);
mutation c5 keeps the cache drop load-bearing. Explored wire and persistence untouched.

Bench (devbox run bench, in-process): tile gate old 0.006 ms -> new 0.001 ms per
request; incremental explored mark 0.001 ms; snapshot build 2.0 ms at 262144 cells
(startup only).
This commit is contained in:
2026-11-21 00:10:39 +01:00
parent 3c0258ea31
commit 2727617db9
12 changed files with 664 additions and 56 deletions
+41
View File
@@ -38,6 +38,47 @@ type
cx*, cy*: int
bits*: seq[byte]
BitMap* = object
## The explored grid as one bit per cell, row-major from the world origin (`cy * cols + cx`).
## This is the tile gate's read-side snapshot (issue #65): the gate asks "is this cell explored?"
## for a handful of indices instead of walking a `HashSet` under the global lock. The on-disk
## chunk bits use the same least-significant-bit-first convention, but per 64x64 chunk; this is
## the whole map flattened, which is what the gate's index arithmetic wants.
cols*: int
rows*: int
bits*: seq[byte]
proc newBitMap*(cols, rows: int): BitMap =
result.cols = max(1, cols)
result.rows = max(1, rows)
result.bits = newSeq[byte]((result.cols * result.rows + 7) div 8)
proc setBit*(bm: var BitMap, idx: int) {.inline.} =
## Idempotent: explored only grows, so setting a set bit costs one load and one store.
if idx < 0 or idx >= bm.cols * bm.rows: return
let byteIdx = idx shr 3
bm.bits[byteIdx] = bm.bits[byteIdx] or byte(1 shl (idx and 7))
proc bitMapOf*(sc: SceneRec, explored: HashSet[int]): BitMap =
## The whole explored set as a bitmap, for startup restore and tests.
result = newBitMap(sc.cols, sc.rows)
for idx in explored:
result.setBit(idx)
proc bitSet*(bits: openArray[byte], idx: int): bool {.inline.} =
## The gate's hot read: no copy, no refcount (the caller passes a borrowed `seq` as openArray).
if idx < 0: return false
let byteIdx = idx shr 3
byteIdx < bits.len and (bits[byteIdx] and byte(1 shl (idx and 7))) != 0
proc bitCount*(bm: BitMap): int =
## How many explored cells the snapshot holds (tests / diagnostics).
for b in bm.bits:
var v = b
while v != 0:
inc result
v = v and (v - 1)
# ---------------------------------------------------------------------------------------------------
# Geometry of a chunk