Skip to main content

Module refcount_protect

Module refcount_protect 

Source
Expand description

Reference-counted GC protection using a BTreeMap + VECSXP backing.

This module provides an alternative to gc_protect that uses reference counting instead of R’s LIFO protect stack. This allows releasing protections in any order and avoids the --max-ppsize limit.

§Architecture

┌─────────────────────────────────────────────────────────────────────┐
│  RefCountedArena / ThreadLocalArena                                 │
│  ┌─────────────────────────┐   ┌───────────────────────────────────┐│
│  │  BTreeMap<usize, Entry> │   │  VECSXP (R_PreserveObject'd)      ││
│  │  ────────────────────── │   │  ─────────────────────────────    ││
│  │  sexp_a → {count:2, i:0}│◄──┤  [0]: sexp_a                      ││
│  │  sexp_b → {count:1, i:1}│◄──┤  [1]: sexp_b                      ││
│  │  sexp_c → {count:1, i:2}│◄──┤  [2]: sexp_c                      ││
│  └─────────────────────────┘   │  [3]: <free>                      ││
│                                └───────────────────────────────────┘│
└─────────────────────────────────────────────────────────────────────┘

§Available Types

TypeStorageUse Case
RefCountedArenaRefCellGeneral purpose, ordered, ppsize-scale workloads
ThreadLocalArenathread_localLowest overhead — no RefCell borrow checking

Only these two flavors are instantiated anywhere in the tree today. This module previously also shipped HashMap- and ahash-backed variants (HashMapArena, ThreadLocalHashArena, FastHashMapArena, ThreadLocalFastHashArena) behind a generic MapStorage abstraction; they were removed for having zero production or test consumers (see https://github.com/a2-ai/miniextendr/issues/ISSUE_NUMBER_PLACEHOLDER). Re-add a flavor (and the generic MapStorage plumbing needed to support more than one map type again) when a real consumer appears.

Structs§

ArenaGuard
An RAII guard that unprotects a SEXP when dropped.
ArenaState 🔒
Core arena state without interior mutability.
Entry 🔒
Entry in the reference count map.
RefCountedArena
A reference-counted arena for GC protection, backed by a BTreeMap.
ThreadLocalArena
Thread-local, BTreeMap-backed reference-counted GC protection arena.
ThreadLocalState 🔒
State wrapper for the thread-local arena.

Constants§

THREAD_LOCAL_STATE 🔒

Type Aliases§

NoSendSync 🔒
Enforces !Send + !Sync (R API is not thread-safe).