dymokomi/luce-geocore
Source of release 0.7.7
0.7.7 / src / groups / cache.lucb
## Evaluated groups, cached by what they read: the expression, the target
## domain, the change ids of the topology and positions, and every
## attribute's name, domain and ids. Equal ids are equal bytes, so a cached
## result is valid exactly while all of those are; a Move leaves `top` and
## `0-99` valid, and anything sharing the columns (snapshots, worker results)
## shares the entries. A few recent results are kept, process-wide.
let cache_size: usize = 16
struct CachedGroup:
var key: u64 = 0
## Everything the group read (`signature`), compared in full on lookup.
var signature: FullKey
var bits: Column[u64]
var report: GroupReport
var used: u64 = 0
func close():
self.signature.close()
self.bits.release()
var cache_lock: sync.Mutex
## `cache_size` entries, allocated on first use; under the lock.
var cached: CachedGroup*? = none
var cache_clock: u64 = 0
func entries() -> CachedGroup[]!:
if let known = cached: return CachedGroup[](known, cache_size)
let made = try new CachedGroup[cache_size] --- in memory.heap
for at in 0..<cache_size: made[at] = CachedGroup(0, FullKey(), Column[u64](), GroupReport(), 0)
cached = made.data
return made
## An evaluated group: a share of its bits (released by the caller) and what
## evaluation reported.
pub struct GroupResult:
pub var bits: Column[u64]
pub var report: GroupReport
## The bits of `text` on `target`, evaluated or cached.
pub func group_bits(mesh: const Mesh*, text: str, target: Domain) -> GroupResult!:
var wanted = FullKey()
defer wanted.close()
try signature(mesh, text, target, &wanted)
let key = wanted.hash()
cache_lock.lock()
let slots = entries() catch failure:
cache_lock.unlock()
error(failure.code, failure.message)
for at in 0..<cache_size:
if slots[at].key == key and slots[at].signature.equals(&wanted):
cache_clock += 1
slots[at].used = cache_clock
let found = GroupResult(slots[at].bits.share(), slots[at].report)
cache_lock.unlock()
return found
cache_lock.unlock()
var program = try GroupProgram.parse(text)
defer program.close()
var report = GroupReport()
var bits = try evaluate(&program, mesh, target, &report)
let column = bits.finish()
cache_lock.lock()
defer cache_lock.unlock()
# Uncached when the table cannot be made; the result stands.
let table = entries() catch failure: return GroupResult(column, report)
var slot: usize = 0
for at in 1..<cache_size:
if table[at].used < table[slot].used: slot = at
table[slot].close()
cache_clock += 1
table[slot] = CachedGroup(key, wanted, column.share(), report, cache_clock)
wanted = FullKey()
return GroupResult(column, report)
## Drop every cached group (tests with allocation trackers).
pub func clear_group_cache():
cache_lock.lock()
defer cache_lock.unlock()
let known = cached else return
let slots = CachedGroup[](known, cache_size)
for at in 0..<cache_size: slots[at].close()
free(slots) in memory.heap
cached = none
## What evaluating `text` on `target` reads: the text, the target, the ids
## of the topology, positions and normals, and each attribute's name, domain
## and column ids (a renamed attribute keeps its column).
func signature(mesh: const Mesh*, text: str, target: Domain, into: FullKey*) -> !:
try into.text(text)
try into.word((u64)(u8)target)
try into.word(mesh.corners_id())
try into.word(mesh.offsets_id())
try into.word(mesh.points_id())
try into.word(mesh.normals_id())
for index in 0..<(usize)mesh.attribute_count():
let entry = try mesh.attribute_entry((i64)index)
try into.text(entry.name())
try into.word((u64)(u8)entry.domain)
try into.word(entry.data.id())
try into.word(entry.table.id())