Implement tablebase parsing fixes and LFU cache

#5: normalise the full lichess tablebase category vocabulary (cursed-win
and blessed-loss classify as draws), return {:ok, tablebase} on a cache
hit, and unwrap the tablebase in game_from_fen.

#6: replace the LRU cache with an LFU cache storing
{key, value, frequency, last_access}, evicting the least-frequently-used
entry (LRU tie-break) and halving every frequency on a weekly sweep.
Persistence moves to a versioned v2 dets file, dropping the legacy
backup; the cache restores synchronously at startup.

Also updates stale tags enum and Play LiveView tests, and drops the
tracked legacy cache backup.
This commit is contained in:
2026-11-01 21:24:51 +01:00
parent 6f08cdd10f
commit 8b894465cc
14 changed files with 359 additions and 83 deletions
+26 -11
View File
@@ -1,25 +1,40 @@
defmodule Chesstrainer.Cache.Evict do
@moduledoc """
Eviction functions for LRU cache
Eviction for the LFU cache.
"""
# O(n) time, not efficient
def maybe_evict_oldest(table, max_size) do
@doc """
Evicts least-frequently-used entries until `table` holds at most `max_size`
rows. Ties in frequency are broken by least recent use (`last_access`).
"""
def evict(table, max_size) do
if :ets.info(table, :size) > max_size do
case find_oldest_entry(table) do
{oldest_key, _} -> :ets.delete(table, oldest_key)
_ -> :ok
case find_least_used_entry(table) do
{key, _frequency, _last_access} ->
:ets.delete(table, key)
evict(table, max_size)
nil ->
:ok
end
else
:ok
end
end
defp find_oldest_entry(table) do
defp find_least_used_entry(table) do
:ets.foldl(
fn {k, _v, t}, acc ->
fn {k, _v, frequency, last_access}, acc ->
case acc do
nil -> {k, t}
{_, oldest_t} when t < oldest_t -> {k, t}
_ -> acc
nil ->
{k, frequency, last_access}
{_k, best_frequency, best_access}
when {frequency, last_access} < {best_frequency, best_access} ->
{k, frequency, last_access}
_ ->
acc
end
end,
nil,