Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / src / held_store.lucb

## What Edit nodes hold (docs/research/POLY-MODELING.md, "Edit nodes hold
## their result"): each held result is an immutable luce-geocore set, kept
## here under its content key so the compute worker reads it by that key
## (Luce objects never cross threads; the set's storage is shared, not
## copied). A set holding objects of the UI thread (Edit CAD's models) is
## also written once to a crate in a folder of this process's, which the
## worker reads back as its own. The UI puts each held result it makes and, after every change
## of the graph or the undo history, says which keys are still alive; the
## rest are released. Also measures what the held results of the undo
## history take, each shared array once, for its memory bound.
##
## Each result also keeps what its GeometryData says beside the set (its
## warning, sketches' state, handles, Aligns: held.meta_of's text), so an
## action the worker ran (edit_held.luc: heavy actions run there) comes
## back to the UI whole, and the worker reads a base whole.
import interop
import memory
import sync
import c
import strings
from luce_std import files
from luce_std import paths
from luce_std import process

from luce_geocore.geocore import GeometrySet, GeometryFile, geometry_set_type, invalid
from luce_geocore.kernel import IdSet, set_bytes
from luce_geocore.core.growing import Growing

## A held result as storage any thread may own (GeometrySet.plain_storage:
## its arrays shared; CAD models, objects of the UI thread, left out).
struct Entry:
    var key: i64
    ## The set's plain storage, or its crate's path when it holds objects
    ## (empty: none). Plain bytes: the store outlives the runtime's objects.
    var set: GeometrySet*?
    var file: Growing[u8]
    ## The GeometryData's own fields, as text (held.meta_of).
    var meta: Growing[u8]

    func close():
        if let storage = self.set:
            storage.close()
            free(storage) in memory.heap
        if self.file.count > 0:
            # A crate that cannot be removed stays behind in the folder.
            _ = removed((str)self.file.view())
        self.file.close()
        self.meta.close()

## Remove crate `path`; whether it went.
func removed(path: str) -> bool:
    let native = strings.copy(path) catch failure:
        return false
    defer strings.release(native)
    files.remove_file((c.str)native) catch failure:
        return false
    return true

var store_lock: sync.Mutex
var entries: Growing[Entry] = Growing[Entry]()
## Counts results added: what is held changed only if it moved on
## (Workspace.keep_held asks before walking the undo history).
var added: i64 = 0
## This process's folder of crates, made on first use (plain bytes).
var folder: Growing[u8] = Growing[u8]()

## The folder crates of held sets go in (under TMPDIR, TEMP or /tmp).
func crate_folder() -> str!:
    if folder.count > 0: return (str)folder.view()
    var parent = process.variable("TMPDIR")
    if parent == none: parent = process.variable("TEMP")
    let base = parent else "/tmp"
    let native = try strings.copy(base)
    defer strings.release(native)
    let made = try files.create_temporary_directory((c.str)native)
    defer made.release()
    try folder.assign(made.value.bytes)
    return (str)folder.view()

pub struct HeldStore:
    let reserved: u8 = 0

    ## Keep `set` under `key` with `meta`, what its GeometryData says (a key
    ## already held keeps its set: equal keys are equal contents).
    pub static func put(key: i64, set: interop.Reference[GeometrySet], meta: str = "") -> !:
        if key == 0: error(invalid, "a held result needs a key")
        if HeldStore.holds(key): return
        let whole = try set.get()
        var file = Growing[u8]()
        errdefer file.close()
        if whole.holds_objects():
            store_lock.lock()
            let directory = crate_folder() catch failure:
                store_lock.unlock()
                error(failure.code, failure.message)
            var room: u8[48]
            let name = strings.format(room[..<48], f"held-{key}.prism") catch failure:
                store_lock.unlock()
                error(failure.code, failure.message)
            let path = paths.join(directory, name) catch failure:
                store_lock.unlock()
                error(failure.code, failure.message)
            store_lock.unlock()
            defer path.release()
            _ = try GeometryFile.save(set, path.value, key)
            try file.assign(path.value.bytes)
        var storage: GeometrySet*? = none
        if file.count == 0:
            storage = try whole.plain_storage()
        var text = Growing[u8]()
        text.assign(meta.bytes) catch failure:
            text.close()
            if let made = storage:
                made.close()
                free(made) in memory.heap
            error(failure.code, failure.message)
        var entry = Entry(key, storage, file, text)
        store_lock.lock()
        defer store_lock.unlock()
        for other in entries.view():
            if other.key == key:
                entry.close()
                return
        entries.push(entry) catch failure:
            entry.close()
            error(failure.code, failure.message)
        added += 1

    ## The set held under `key`, owned by the calling thread: read back from
    ## its crate when it holds objects.
    pub static func get(key: i64) -> interop.Reference[GeometrySet]!:
        store_lock.lock()
        var found: GeometrySet*? = none
        var crate = Growing[u8]()
        defer crate.close()
        for entry in entries.view():
            if entry.key == key:
                if entry.file.count > 0:
                    crate.assign(entry.file.view()) catch failure:
                        store_lock.unlock()
                        error(failure.code, failure.message)
                    break
                let storage = entry.set else break
                found = storage.plain_storage() catch failure:
                    store_lock.unlock()
                    error(failure.code, failure.message)
                break
        store_lock.unlock()
        if crate.count > 0:
            return try GeometryFile.load((str)crate.view())
        let storage = found else error(invalid, "an Edit node's held result is gone")
        return try interop.Reference[GeometrySet].adopt(storage, geometry_set_type, memory.heap)

    ## What the GeometryData held under `key` says (held.meta_of's text;
    ## "" for a key not held).
    pub static func meta(key: i64) -> interop.Owned[str]!:
        store_lock.lock()
        defer store_lock.unlock()
        for entry in entries.view():
            if entry.key == key: return try interop.copy_text((str)entry.meta.view())
        return try interop.copy_text("")

    ## Whether `key` is held.
    pub static func holds(key: i64) -> bool:
        store_lock.lock()
        defer store_lock.unlock()
        for entry in entries.view():
            if entry.key == key: return true
        return false

    ## Release every held set whose key is not in `alive`.
    pub static func keep(alive: const i64[]) -> !:
        var dropped = Growing[Entry]()
        defer:
            for at in 0..<dropped.count: dropped.items[at].close()
            dropped.close()
        store_lock.lock()
        var at: usize = 0
        while at < entries.count:
            var found = false
            for key in alive:
                if key == entries.items[at].key:
                    found = true
                    break
            if found:
                at += 1
                continue
            dropped.push(entries.items[at]) catch failure:
                store_lock.unlock()
                error(failure.code, failure.message)
            entries.items[at] = entries.items[entries.count - 1]
            entries.count -= 1
        store_lock.unlock()

    ## How many results were added so far (it only grows).
    pub static func generation() -> i64:
        store_lock.lock()
        defer store_lock.unlock()
        return added

    ## How many results are held (tests).
    pub static func count() -> i64:
        store_lock.lock()
        defer store_lock.unlock()
        return (i64)entries.count

    ## The bytes of the sets held under `keys` (unknown keys add nothing),
    ## each shared array counted once.
    pub static func footprint(keys: const i64[]) -> i64!:
        var ids = IdSet()
        defer ids.close()
        var total: usize = 0
        store_lock.lock()
        defer store_lock.unlock()
        for key in keys:
            for entry in entries.view():
                if entry.key == key:
                    if let storage = entry.set: total += try set_bytes(storage, &ids)
                    break
        return (i64)total