Principles and decisions
This page records why Cimba Python is shaped the way it is: the principles the design follows, the invariants every change must keep, and the main decisions with the alternatives that were rejected.
Principles
Model the domain, not the ABI. Every user-visible concept is a discrete-event or input-modeling concept: model, process, entity, input, source, sweep, trial. Nothing in the API exists only to satisfy the C layer.
Inputs are first-class and source-agnostic. Observed real-world variability is a declared input. Its source is configuration, interchangeable without touching model code, with its own random stream and recorded provenance.
The running system mirrors the object graph. One record per model instance; references are pointers; lists are pointer tables.
Compile classes, bind instances. Code depends only on class definitions. Structure, values and sources are data.
Let the compiler compile. The modeling language is implemented in the JIT’s type system (views, handles, overloads), not by rewriting source text.
Fixed machinery is native and precompiled. Only user methods are JIT-compiled. The trial lifecycle and the input runtime are C.
Configure objects; read results through objects. No string addressing. Experiments snapshot configuration, and results are immutable.
One way to do each thing; boundaries enforced by tests. Package rules live in the test suite, not in a style guide.
Invariants
Invariant |
Statement |
|---|---|
Trial independence |
A trial reads shared read-only data and writes only its own block and its own native objects. |
Reproducibility |
A trial’s outcome is a function of (model classes, configuration, design point, trial seed). It is independent of worker count, scheduling, memory chunking and input extension. |
Structural immutability |
The instance tree, fields and input declarations are fixed for an experiment, except that swept children, references and lists give one tree per combination of options. Experiments vary values, sources and structural options. |
Window semantics |
Time-weighted statistics cover the measurement window. Datasets are cleared when it opens. |
Lifecycle pairing |
Every native object is created and destroyed exactly once per trial, including abandoned trials. |
Hook order |
Static |
Objects are identity |
Users address everything through objects; canonical paths are labels. |
Source independence |
Changing a source changes values only, never compiled code or other inputs’ streams. |
Stream synchronization |
An input’s values in a trial depend only on the trial seed, the input’s identity and its source. |
Prefix stability |
Regenerating a row source with more values reproduces the leading values, so extension can’t change results. |
Key decisions
How inputs reach model code
Source-agnostic input fields (chosen) |
Trace fields + inline random calls |
Resampling verbs inside the trial |
Python callback per value |
|
|---|---|---|---|---|
Swap distribution ↔ trace ↔ bootstrap |
configuration only |
rewrite the model |
partly |
configuration |
Per-input streams (CRN) |
yes |
one shared stream |
shared stream |
up to the user |
Block, joint, model-based structure |
yes (host rows) |
yes |
i.i.d. only, realistically |
yes |
Speed |
one C call |
one C call |
one C call |
a GIL round trip per value |
Running out |
explicit policies, exact extension |
silent |
n/a |
n/a |
Runtime data model
Per-instance records, compiled per class (chosen) versus one flattened record per model. Flattening gives constant offsets, but it leaks flattened names into the API, needs slot and owner tables to rebuild the object graph, and forces recompilation whenever structure changes. Per-instance records cost one pointer load to cross between models and make structure pure data.
Compilation strategy
Numba extension types (chosen) versus rewriting model source into flattened functions. With typed views and handles, real type inference sees your code as written. Misusing a handle is a compile error with a line number, not a runtime crash, and callbacks don’t need inspectable source. Generating C code was rejected as a second compiler to maintain.
Native binding
ctypes plus direct symbol calls (chosen) versus Cython or cffi. The host needs about ten calls, so ctypes costs nothing measurable. It keeps the C library free of the Python C API, which gives one wheel per platform instead of one per CPython version. Model code calls the same library directly through registered symbols.
Isolation
In-process trials with engine-level abandonment (chosen) versus a process per trial. Abandonment handles every engine-detected error without leaking. Typed handles and bounds checks prevent memory errors in model code. Process isolation would add a second execution path and serialization costs for the rare remaining crash.
Results format
NumPy arrays in immutable containers (chosen) versus pandas or xarray.
Arrays are the natural shape for (points, replications) data and add no
dependency; to_table() feeds pandas when wanted.
Functions dispatch on the model’s class
Per-class function tables (chosen) versus static calls by declared type.
Models are built by composition and inheritance: a store holds a
Ref[Policy], a line holds list[Station]. For polymorphic behavior to
work there, @cb.function calls go through the dispatch table the record
already points to. Each class fills its slots with its own implementations,
and subclasses keep their bases’ slot positions. The price is one indirect
call and annotated signatures, since a call through a pointer needs a fixed
machine signature. In return, the caller still depends only on the base
class, and swapping a policy (even by sweeping it) never recompiles
anything.
Sweeping models: one tree per option
Separate trees (chosen) versus one tree with every option present. If all options lived in every trial and a pointer chose among them, the options that weren’t selected would still run their processes and hooks and silently distort the results. Instead, the experiment expands the full design, groups design points by the options they select, and runs each group on a tree that contains only those options, with the original seeds. Classes are compiled once, so the cost is only a separate memory layout per option.
Common random numbers by default
Sweeps exist to compare configurations and input models. Common seeds per
replication, combined with per-input streams, make those comparisons paired
and sharp. seeding="independent" remains available.
Simulation optimization
Decisions replace parameter values. A decision is assigned to an existing
Param field, and its annotation determines the domain. This keeps the
same object addressing and model validation used by experiments. Objectives
return individual trial values so failure handling, paired comparisons and
confidence intervals remain available after evaluation.
Reuse the experiment runner. The optimize package supplies candidate
values and seeds to a reusable experiment snapshot. Compilation and native
execution stay in their existing layers; experiments have no dependency on
the search package. SciPy is already a core dependency, and its DE solver
supports both real and integer domains. The public API therefore needs one
settings object rather than a general optimizer plugin interface.
Evaluate generations together. SciPy’s vectorized objective and deferred updates deliver a full generation at once. Cimba runs the new distinct candidates as a batch and parallelizes their trials with native threads. No process pool or per-trial Python callback is needed.
Keep search samples fixed; estimate the choice independently. Common search seeds make comparisons paired and cached evaluations exact. Fresh selection seeds choose among finalists, then another seed stream estimates the chosen configuration. The reported estimate has influenced neither stage of the choice.
Use a fixed default population. The multiplier is 15 on every machine,
so worker count does not change the default search. popsize="fill" is an
explicit choice to size the population from the available workers. Recording
its resolved integer lets the caller reproduce that population elsewhere.
Current limitations
Known gaps that the documentation works around:
The compile cache lives in memory, so each new Python process compiles its classes once.