Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-geocore

Source of release 0.7.9

0.7.9 / src / groups / cache.lucb

## Evaluated groups, cached by what they read: the expression, the target
## domain, the change ids of the topology, and only what the expression
## reads besides: positions and normals for `@P`, `@N` and select rules,
## every attribute's name, domain and ids for group names and attributes.
## Equal ids are equal bytes, so a cached result is valid exactly while all
## of those are. A drag's frames change only positions, so `0-99`, `*` or a
## named group stays one entry over the whole drag instead of a new one per
## frame; 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 program = try GroupProgram.parse(text)
    defer program.close()
    var wanted = FullKey()
    defer wanted.close()
    try signature(mesh, &program, 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 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 `program` (`text`) on `target` reads: the text, the
## target, the ids of the topology, and as `reads` finds, the positions and
## normals and each attribute's name, domain and column ids (a renamed
## attribute keeps its column).
func signature(mesh: const Mesh*, program: const GroupProgram*, text: str, target: Domain, into: FullKey*) -> !:
    let (attributes, geometry) = reads(program)
    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((u64)mesh.point_count())
    if geometry:
        try into.word(mesh.points_id())
        try into.word(mesh.normals_id())
    if not attributes: return
    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())

## Whether `program` reads attributes (group names, `@name`) and whether it
## reads positions or normals (`@P`, `@N`, and calls, whose select rules
## may measure); numbers, ranges and edges read only the topology.
func reads(program: const GroupProgram*) -> (bool, bool):
    var attributes = false
    var geometry = false
    for term in program.terms.view():
        match term.kind:
            .name, .names: attributes = true
            .attribute:
                let name = program.text(&term)
                if name == "P": geometry = true
                elif name == "N":
                    geometry = true
                    attributes = true
                elif name != "ptnum" and name != "primnum" and name != "vtxnum" and name != "elemnum" and name != "intrinsic:indexorder": attributes = true
            .call:
                attributes = true
                geometry = true
            _: continue
    return (attributes, geometry)