Skip to content

Lookups and selectors

Unlike a projection, a Lookup<'K, 'V> maintains no Keysarray: it creates per-key cells on demand over an open key domain, keeps them only while observed, and pushes targeted invalidations when a shared upstream state changes.

Selectors

createSelector source returns a Lookup<'K, bool> where Get key is true for the currently selected key and false for every other key (keys compare structurally).

A dedicated SelectorLookup tracks the previous selection and invalidates at most {prev, next} in O(1)O(1) time with zero intermediate list or buffer allocations:

let selected = signal { return 1 }
let isSelected = createSelector (fun () -> selected.Value)
selected.Value <- 3
[ for k in 1..4 -> k, isSelected.Get k ]
[(1, false); (2, false); (3, true); (4, false)]

Moving the selection wakes only the deselected and newly selected keys while every untouched key does 0 recomputations:

Example code
let mapSelected = signal { return 1 }
let mapIsSelected = createSelector (fun () -> mapSelected.Value)
let mutable runs1 = 0
let mutable runs2 = 0
let mutable runs3 = 0

let sel1 =
    createMemo (fun _ ->
        runs1 <- runs1 + 1
        mapIsSelected.Get 1)

let sel2 =
    createMemo (fun _ ->
        runs2 <- runs2 + 1
        mapIsSelected.Get 2)

let sel3 =
    createMemo (fun _ ->
        runs3 <- runs3 + 1
        mapIsSelected.Get 3)

effect { let! v = sel1 in printfn "key 1 = %b" v }
effect { let! v = sel2 in printfn "key 2 = %b" v }
effect { let! v = sel3 in printfn "key 3 = %b" v }

controls
    [
        button "Select 2" (fun () -> mapSelected.Value <- 2)
        |> describe "Only keys 1 and 2 recompute; key 3 does 0 recomputations."
        |> expect "Keys 1 and 2 flip while key 3 stays at 1 run." (fun () ->
            not sel1.Peek
            && sel2.Peek
            && not sel3.Peek
            && runs1 = 2
            && runs2 = 2
            && runs3 = 1)
        button "Select 3" (fun () -> mapSelected.Value <- 3)
        |> describe "Only keys 2 and 3 recompute; key 1 does 0 recomputations."
        |> expect "Keys 2 and 3 flip while key 1 stays at 2 runs." (fun () ->
            not sel1.Peek
            && not sel2.Peek
            && sel3.Peek
            && runs1 = 2
            && runs2 = 3
            && runs3 = 2)
    ]
Hover a node for its state; click it for why it last ran.

Lookups

createLookup f affected source holds source () in an internal state memo and computes f state key for each observed key.

On each unequal state transition prev -> next, it recomputes only the live cells named by affected prev next in O(affected)O(\text{affected}) time, ahead of woken effects.1

Pointwise reads follow the unified naming across Ranvier's keyed primitives:

OperationCE descriptorBehaviour
lookup.Get key / Lookup.get key lookup ('V)Lookup.at key lookupDemand-creating tracked read; raises NotReadyException while pending or the cell/source failure.
lookup.TryGet key / Lookup.tryGet key lookup ('V voption)Lookup.tryAt key lookupTracked read of an already-live cell (ValueSome v), or ValueNone untracked (0 cells allocated when cold).
lookup.TryRead key / Lookup.tryRead key lookup (Reading<'V>)tryRead (Lookup.at key lookup)Non-throwing demand-creating tracked read returning Ready v, Pending, or Failed ex.
lookup.CellCount—Untracked count of resident cells; orphaned cells (0 observers) evict automatically on the next transition or read.
let stock = signal { return Map [ "apples", 3; "pears", 0 ] }

let changedKeys (prev: Map<string, int>) (next: Map<string, int>) =
    seq {
        for KeyValue (k, v) in prev do
            if next.TryFind k <> Some v then
                yield k

        for KeyValue (k, _) in next do
            if not (prev.ContainsKey k) then
                yield k
    }

let onHand =
    createLookup (fun (m: Map<string, int>) k -> m.TryFind k |> Option.defaultValue 0) changedKeys (fun () -> stock.Value)

let coldPear = memo { let! count = Lookup.tryAt "pears" onHand in return count }
let appleCount = memo { let! count = Lookup.at "apples" onHand in return count }
let warmApple = memo { let! count = Lookup.tryAt "apples" onHand in return count }
let plumReading = Lookup.tryRead "plums" onHand
stock |> Signal.update (Map.add "apples" 4) // refreshes "apples" and evicts unobserved "plums"

coldPear.Value, appleCount.Value, warmApple.Value, plumReading, onHand.CellCount
(ValueNone, 4, ValueSome 4, Ready 0, 1)

Lookup vs MemoTable

Choose between createSelector, createLookup, and MemoTable<'K, 'V> based on how keys depend on upstream state and how memory is bounded:

PrimitiveUpstream dependency modelInvalidation & evaluationMemory retention
createSelector (Lookup<'K, bool>)One selected key 'KPush: O(1)O(1) direct 2-key invalidation (prev & next)0 unobserved cells; O(observed keys)O(\text{observed keys})
createLookup (Lookup<'K, 'V>)One shared state 'S + affected prev next diffPush: O(affected)O(\text{affected}) targeted cell recomputation; f reads 'S untracked0 unobserved cells; auto-evicts orphaned keys on next read/transition
memoTable (MemoTable<'K, 'V>)Arbitrary independent IReactive reads per key (let! k = memoTable.key) + recursive table.GetPull-lazy per-slot Memo nodes; O(1)O(1) amortized hit; optional onCycle recoveryBounded weighted-LRU payloads (maxEntries / maxWeight), eviction-invariant stamp cutoff, and maxDormant slot cap
  • Reach for createSelector whenever rows test equality against a single active ID (selection, focus, active tab, hovered item).

  • Reach for createLookup when one upstream map or state snapshot changes and affected prev next can name the touched keys without maintaining independent dependency lists per key.

  • Reach for MemoTable (Memo tables) when different keys read different signals or memos, when keys call each other recursively, or when unobserved heavy values should remain cached up to an explicit count or byte-weight budget.

Edit this page