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:
+43
-2
@@ -2,9 +2,10 @@
|
||||
## No timing code lives in server/: this harness times from outside with O(1) accumulators (sum, worst), no sample arrays.
|
||||
## Targets (DESIGN 5.3 / 13 M2, PROPOSED): polygon + cells <= 0.451 ms avg on the prototype scene; polygon < 1 ms avg on 5k walls.
|
||||
## The egress rows record the absolute averages the in-process gate (tests/tviews.nim, TESTING 5.3) deliberately does not assert.
|
||||
import std/[json, monotimes, times, strformat, os, math, sets, tables]
|
||||
import std/[json, monotimes, times, strformat, os, math, options, sets, tables]
|
||||
import db_connector/db_sqlite
|
||||
import geometry, moverules
|
||||
import characters, props, tokens, views, vision, world
|
||||
import auth, characters, crypto, db, explored, props, sessions, tiles, tokens, views, vision, world
|
||||
import scenegen
|
||||
import ../tests/[refold, testutil]
|
||||
|
||||
@@ -105,5 +106,45 @@ discard bench("GM egress pass (fresh diff base)", 200, proc(i: int) =
|
||||
discard bench("player egress pass (fresh diff base)", 200, proc(i: int) =
|
||||
var known: Known
|
||||
sink += egressFrames(Recipient(isGm: false, playerId: 2), egressLarge.borrow, known).len)
|
||||
echo "== tile gate (#65): old SQL-under-lock path vs published snapshot =="
|
||||
let gateScene = SceneRec(id: 1, name: "Scene", w: 4096, h: 4096, tileSize: 32, cell: 32,
|
||||
visionRadiusDefault: 400, tokenRadiusDefault: 20, artVersion: 1,
|
||||
nextWallId: 1, extra: newJObject())
|
||||
# The "before" side: a real (in-memory) SQLite session table and the explored HashSet the old gate
|
||||
# read under dbLock. The SELECT is the same `session JOIN player` auth.sessionPlayer runs.
|
||||
let mem = openDatabase(":memory:")
|
||||
mem.exec(sql"CREATE TABLE player (id INTEGER PRIMARY KEY, name TEXT, role TEXT, colour TEXT)")
|
||||
mem.exec(sql"""CREATE TABLE session (id INTEGER PRIMARY KEY, player_id INTEGER,
|
||||
token_hash TEXT, created_at TEXT, last_seen TEXT)""")
|
||||
mem.exec(sql"INSERT INTO player (id, name, role, colour) VALUES (2, 'Alice', 'player', '#222')")
|
||||
let benchToken = "bench-session-token"
|
||||
mem.exec(sql"INSERT INTO session (player_id, token_hash) VALUES (2, ?)", sha256Hex(benchToken))
|
||||
var exploredSet = initHashSet[int]()
|
||||
var gateCells: seq[(int, int)]
|
||||
for cx in 8 .. 12:
|
||||
for cy in 8 .. 12:
|
||||
exploredSet.incl gateScene.cellIndex(cx, cy)
|
||||
gateCells.add (cx, cy)
|
||||
let gc = newGateCache(gateScene)
|
||||
gc.putSession(sha256Hex(benchToken), 2, false)
|
||||
gc.markExplored(2, gateScene, gateCells)
|
||||
discard bench("tile gate old: parse + SHA-256 + session JOIN + HashSet", 2000, proc(i: int) =
|
||||
let h = sha256Hex(benchToken)
|
||||
let parsed = parseTileRequest("1", "v1", "9_9.png", gateScene)
|
||||
if parsed.outcome == toTileOk and sessionPlayer(mem, h).isSome:
|
||||
if tileCoveredByExplored(gateScene, exploredSet, parsed.req.x, parsed.req.y): inc sink)
|
||||
discard bench("tile gate new: parse + SHA-256 + cache lookup + bitmap", 2000, proc(i: int) =
|
||||
let h = sha256Hex(benchToken)
|
||||
if gc.gateTile(h, "1", "v1", "9_9.png").allowed: inc sink)
|
||||
# Explored ops: the snapshot build at the 16384 px size (512x512 cells) and the incremental mark.
|
||||
let bigScene = SceneRec(id: 1, name: "Scene", w: 16384, h: 16384, tileSize: 32, cell: 32,
|
||||
visionRadiusDefault: 400, tokenRadiusDefault: 20, artVersion: 1,
|
||||
nextWallId: 1, extra: newJObject())
|
||||
var bigSet = initHashSet[int]()
|
||||
for k in 0 ..< 262144: bigSet.incl k
|
||||
discard bench("explored snapshot build (262144 cells)", 20, proc(i: int) =
|
||||
sink += bitMapOf(bigScene, bigSet).bitCount)
|
||||
discard bench("explored incremental mark (16 fresh cells)", 5000, proc(i: int) =
|
||||
gc.markExplored(2, gateScene, @[(9, 9), (10, 9), (11, 9), (12, 9)]))
|
||||
echo "(sink ", sink, ")"
|
||||
quit(if misses == 0: 0 else: 1)
|
||||
|
||||
Reference in New Issue
Block a user