#Inside eLucid8: how it runs, and how fast
Who this page is for: anyone who wants to know what happens inside
elucid8when a program runs, how its speed and memory are measured, and where it can get faster. To use eLucid8, the Tutorial and How eLucid8 fits together are the places to start.
An eLucid8 program is a set of equations that define values over named dimensions. Its meaning is fixed by the equations alone: the answer at any point is what the equations say it is. Everything on this page is how the implementation finds that answer quickly and in little memory. None of it is allowed to change an answer: caches, compiled loops, the number of threads, and how much memory is kept decide only how fast an answer comes and how much memory it takes.
#The pieces
- The compiler reads a program (
program.el8) and writes its plan (plan.json): every value's equation as a tree, its dimensions and bounds, and, where the equations allow, a compiled form of each value for loops (its kernel). The compiler is built into theelucid8command, so a program compiles offline. - The engine runs a plan. It is written in Rust, and is the same engine in the
elucid8command, inelucid8 serve, in the Python package (which runs the command) and, compiled to WebAssembly, on the website's hosted MCP server. - The reference evaluator is a second engine, written in TypeScript, which the playground uses. It is the definition: the Rust engine is tested to give exactly its answers, value for value and bit for bit, on thousands of programs, and a conformance suite records the expected answers so that other implementations can be checked too.
#Eduction: computing only what is asked for
At the centre of the engine is eduction, Lucid's demand-driven evaluation. Nothing is computed until a point is asked for. Asking for total[t = 5] starts from that point and follows the equations: total at 5 needs total at 4 and reading at 5, and so on down, and only those points are computed.
- A demand is a value at a point (a coordinate for each of its dimensions). The engine first looks for it among what it already holds (below), and otherwise evaluates the value's equation at that point, demanding the points the equation reads.
- A conditional demands its test, then only the branch the test chooses. The other branch is never computed, so it can neither cost time nor fail.
- Deep histories do not use deep recursion in the engine. After 100 nested demands, the engine sets the demand aside and continues it from a fresh start, so an iteration a million steps deep runs in ordinary memory. A chain of demands that never ends stops at a depth limit (1,000,000 nested demands by default) with a program error, never a wrong value.
- Recursive functions run the same way, with each call's place recorded as an extra coordinate. A function that calls itself once per step (such as a running maximum over a history) is compiled to an ordinary iteration over a depth dimension, and a parameter passed on unchanged is not evaluated at every level.
Eduction is what lets eLucid8 answer one cell of a large simulation without computing the rest: on the 512 × 512 heat plate, one probe cell at step 100 reads 5% of the field, and is answered 16 times faster than NumPy computes the whole plate (Examples).
#Compiled loops (kernels)
Evaluating one point at a time is flexible, but most of its time goes on bookkeeping: a key for each point, a lookup, a place on the stack of demands. Where the equations say in advance what a region of points will read, the engine computes the whole region in compiled loops instead.
For example, with
avg3 = mean reading over (t : #t - 2 .. #t) end
a query for avg3[t = 0..9999] tells the engine, before anything is computed, that it needs reading from t = 0 to 9999. It reads that once into an array and runs one loop, which in outline is
for t in 0..9999:
avg3[t] = (reading[t-2] + reading[t-1] + reading[t]) / 3
with no key or lookup per point (positions below 0 are left out of the mean, as the language says).
- Where the regions come from. Always from demand: a query over a box (
total[t = 0..9999]), the points a demand needs, or the levels of an iteration below a demanded point. Never from declarations: a dimension declared with a large bound is not computed unless something asks for it. - Box regions. A query over a box of a value whose kernel exists is computed over the whole box at once, together with the boxes it reads of other values, planned so that overlapping reads are computed as one box rather than again.
- Levels of an iteration. A value defined along a dimension by
fby(heat overtime, a training run overstep) is computed a level at a time: the whole plate attime = 5, from the plate attime = 4. Values that iterate together (the weights, activations and gradients of a network) are computed together, level by level, and members that read each other at the same level are computed in one loop. Levels below the one being computed are released as the iteration climbs. - Searches by timestamp (
asof,hold,latest,after, windows) run as loops over a column store of their target: each column's candidates kept in timestamp order, so a search is a binary search or a walk from where the last one stopped. - The same answers. A loop runs each element through the same operations, in the same order, as evaluating that element alone would. Anything a loop cannot settle exactly (an error that must name where it began, a value of an unexpected kind, a test that is an error) is left to per-element evaluation for that element. A check mode compares every element a loop computed with per-element evaluation.
- Conditionals in loops. Both branches are computed over the box, and each element keeps the one its test chose. That is safe because a branch is pure: computing it where it was not needed costs time, never an answer. Values that may call a data service are never computed ahead like this; a service is called only where an answer needs it.
- Across threads. A loop is cut into chunks of 2,048 elements, shared among a pool of threads that is started once. Every element is computed whole on one thread, and every sum is exact, so the answers are identical, bit for bit, with any number of threads.
On the heat simulation, computing by levels is about 30 times faster than one point at a time on one thread, and splitting the levels across 18 threads makes it about 8 times faster again.
#Where values are kept
When a demand looks for a value, it looks in these places, in order:
- Region arrays, computed by loops for a box query.
- Levels of an iteration being computed.
- The memo: points computed one at a time in this query.
- The point cache, if switched on: answers kept from earlier queries.
Then, if none holds it, the point is computed, and the answer goes into the memo.
Arrays are flat. A region is a box (a range of coordinates along each dimension, and every combination of them) laid out as one contiguous block of 64-bit numbers, the last dimension varying fastest, as in NumPy's C order. Beside it are one byte per element saying whether it is a value, exactly eod, or "not known here", and, only when the data carry timestamps, a column of timestamps. Where every timestamp is a simple function of one coordinate (a reading's tick, say), it is kept once per coordinate instead of once per element. Texts are numbered and the array holds their numbers; errors are kept beside the array by element, with the chain that says where they began. There are no nested arrays: every expression denotes one value at each point, so a shift or a neighbourhood is a strided read into another flat array.
The memo is mostly arrays too. Points computed one at a time over whole-number dimensions are kept in blocks of 64 (8 × 8 on a grid), found by arithmetic on the coordinates rather than by hashing a key. Values over texts or real numbers, and points too scattered to fill blocks, are kept in a map by key.
#Memory
Within a query, the engine keeps what it computed only while something may still read it:
- Usage counts. From the equations, the engine knows how many times each point will be read. When the last reader has read it, the point is let go. A linear training run evaluated point by point, which would hold every step of its history, holds 47 MB at step 200 instead of 573 MB, and the same at any later step.
- Retirement. Points the counts cannot account for (those read only inside conditionals, for instance) are let go by age once memory is under pressure. A point let go too early is simply computed again, with the same answer.
- When it starts. A query keeps everything until what it holds reaches about 1 GiB, then starts letting go. Small queries pay nothing for counting.
- Very large boxes are computed in slabs of at most 4,194,304 cells, each delivered and freed before the next.
- Large blocks freed by the
elucid8command are kept for reuse up to 256 MiB, and anything beyond is returned to the system at once.
Peak memory is therefore set mostly by the data read and by one slab or level of work, not by the length of a history.
#Across queries, and after corrections
By default each query starts afresh. Keeping answers across queries is a choice, because it costs memory and pays only when the same answers are asked again.
- The point cache keeps each answer with a record of exactly what it read: which input points, at which version of the data. A later query reuses an answer while everything it read is unchanged. After a correction, only the answers that read the corrected data are computed again: in the features example, 106 values after one purchase was corrected, out of hundreds of thousands.
- Versions of data. Each version of an input is identified by a hash of its bytes, so the same bytes are always the same version. A query pins the versions it reads, and sees one consistent set of data however long it runs. Rows appended to an input make a new version without copying the old one, and answers that read nothing the new rows could change stay valid without being checked again.
elucid8 servekeeps a box query's region between requests. When a file is corrected, the next query computes only the cells the correction can change and writes them into the kept array. In the markets example, after one feed is corrected for 5 of 4,001 symbols, that is about 34,000 cells instead of 33 million.- A memory budget (
cacheBytes) bounds what the caches hold. Over the budget, records are first compacted (an answer's record of what it read is reduced to the boxes of data it read), then the least recently used answers are dropped.
#Reading and writing data
- Reading files. CSV and JSON lines are read in blocks of about 1 MiB, parsed on every thread, and merged in file order; Parquet and Arrow are read group by group. Several files are read at once. The rows become a table sorted by coordinates, with an index built when it is first read. Reading the sensor field's 1.1 GB CSV (65 million rows) takes about 0.5 s on 18 threads.
- A version's hash is computed as the file is read: XXH64 over blocks of 64 KiB, combined into one 64-bit id, at about 18 GB/s on one thread.
- Writing. Answers are written as they are computed: text formats in pieces, binary formats (NumPy, Arrow, Parquet, Zarr) from a compact table of the answers, by a thread of their own. Arrow, Parquet, NumPy and Zarr files are written by the command itself, without libraries, and read by pyarrow, pandas, Polars, DuckDB, NumPy and xarray.
#Exact numbers, the same everywhere
Answers are the same bits on every engine, every machine and every number of threads. Three things make this so, and each has a cost.
- One maths library.
exp,log,tanh,sin,cos,powand the other functions are computed by a port of fdlibm 5.3 in both engines, never by the machine's own library, whose results differ between machines in the last bits. - Exact sums.
sumandmeangive the exact sum of their elements, rounded once, whatever the order. The engine first adds in a way that keeps the rounding error, and checks that the result is certainly the correctly rounded one; only when it cannot tell (a near tie) does it compute exactly with wider integers. On data such as whole cents or whole ticks, exact sums cost almost nothing; on the heat plate's 3 × 3 means of fractions, they cost about a third more time.approx_sumandapprox_meanadd in any order, as NumPy and the databases do, for when the last bits do not matter. - No reordering of arithmetic. Each operation is done as written, never fused or reassociated, in every loop.
#How speed and memory are measured
Each of the examples has a baseline/ folder holding the same computation written as a user of another tool would write it (NumPy; DuckDB SQL; Polars; pandas), checked against eLucid8's answers. Its measure.py runs eLucid8 and each baseline at full size, one after the other.
- What is timed: each from reading the same input files to writing the same result, as wall-clock time. The clock starts just before the command or script is launched as a new process, and stops when the process ends, so starting up, reading, computing and writing are all included. Where a Python script runs
elucid8(the features and markets), the time is the script's, from its start to the finished Parquet file. For the stream, the time per tick is what a Python client sees: sending the tick's rows toelucid8 serve, the answer, and reading it into NumPy. - Not timed: generating the data, compiling the program (the plan is compiled beforehand), and starting a long-running service.
- Each number is the best of 3 runs, each a fresh process, with the median recorded too. Before each run the script waits until nothing else is measuring, and records the machine's load.
- Two comparisons, because tools use threads differently: every tool on one thread, and every tool at its defaults (18 threads where it uses them).
- Memory is each run's peak resident memory, the most physical memory the process held at any moment, read from the operating system when it ends. The largest of the runs is recorded. Peak resident memory includes memory the allocator has freed but kept for reuse, so it can overstate what was in use.
- The machine: an Apple M5 Max (18 cores, 64 GB), macOS 26.6.2, with NumPy 2.5.3, DuckDB 1.5.6, Polars 2.0.0, pandas 3.0.6 and pyarrow 26.0.0, measured with release 0.22.0 of
elucid8(the heat simulation's whole plate and the stream with release 0.24.0).
#Results, in brief
The Examples page has every number and what each comparison measures. In brief, with each tool at its defaults:
| task | eLucid8 | against |
|---|---|---|
| heat simulation, the whole 512 × 512 plate at step 1,000 | 0.35 s | 3.8× faster than NumPy |
| heat simulation, one probe cell at step 100 | 0.017 s | 16× faster than NumPy (which computes the whole plate) |
| sensor field, 1,000 ticks from a 1.1 GB CSV | 1.42 s | 3.1× faster than NumPy |
| point-in-time feature table, 36.5 million rows | 4.14 s | level with DuckDB; 3.5× faster than pandas |
| every trade of a day priced against its quote | 6.00 s | 1.5× faster than DuckDB, 1.7× faster than Polars (pandas not measured: it needs about 40 GB here) |
Peak memory: on the sensor field, NumPy 8.3 GB and elucid8 2.5 GB; on the feature table, elucid8 0.7 GB, DuckDB 6.2 GB and pandas 12 GB; on the markets, eLucid8 7.1 GB, DuckDB 18.6 GB and Polars 27.0 GB.
Where eLucid8 is slower today:
- On one thread, whole fields that NumPy computes with array operations: NumPy is 1.35× faster on the heat plate with exact sums; the sensor field's alerts take the same time in both.
- The markets on one thread, where Polars is 1.5× faster.
- A stream's round trip through
elucid8 serve: the query itself takes 2.2 ms, but sending the rows and reading the answer bring a tick to 10.5 ms with answers as Arrow (the example client's default), and 14.0 ms with answers as JSON, against NumPy's 5.8 ms in one process. - Small neural networks against PyTorch: 1.7× slower per training step with exact sums, 1.4× with sums in any order, and more on one thread.
#How performance could improve
These are the known ways eLucid8 can get faster or leaner, from measurements of the examples and of the engine. None changes an answer.
Computing less
- Sharing regions between queries and between an output's parts. Each query and each part of an output computes its own regions, so values near their edges are computed more than once. Measured on the examples, the feature table computes 1.42 times the values it needs, its records 1.25 times and the sensor batch 1.125 times; sharing would bring these close to 1.
- More constructs in loops. Filtered aggregates (
… over R if c end),skip eod, regions given as lists, windows beside other region items, and values over real-number coordinates are still evaluated one point at a time. - Recursion. A linear recursion is compiled to an iteration but not yet run in loops; a function that calls itself twice (Fibonacci) repeats its work, since calls are shared by their place, not by the value of their arguments.
Using the threads better
- The remaining one-thread parts: handing out a box's answers in some formats, merging what the readers parsed, and evaluating points one at a time, which runs on one thread because the memo and its bookkeeping are shared. Making per-element evaluation parallel is a larger change, and would help programs that do not run in loops.
- One large sum on several threads. A single sum over many elements (a training loss over 9,212 samples) runs on one thread; exact sums make splitting it safe, and a first attempt was slower, from allocating its buffers per piece.
Faster arithmetic
- A vectorised maths library. The fdlibm port computes one value at a time; computing 2 to 8 at once in the processor's vector units, with the same bits, is expected to make each call 1.5 to 4 times faster. It matters most for networks (
tanhis a third of a step's time). - Cheaper exact sums of fractions, where the check that a sum is correctly rounded costs about a third of the heat plate's time.
Moving data
- Arrow from connectors. Connectors send rows as JSON lines today: 100,000 rows from PostgreSQL take 630 ms, against 5 ms from a CSV file. Sending Arrow over the connector protocol is expected to make this 5 to 6 times faster.
- Several queries answered at once by
elucid8 serve, which answers one request at a time today.
Memory
- Peak memory of outputs written in parts, about 6–7% above the same output written whole, and of iterations that keep one more level than strictly needed.
Larger machines
- Accelerators. eLucid8 has no GPU backend. Lowering the parts of a program that run in loops to a tensor compiler (such as XLA) is planned after automatic differentiation over equations, so that training no longer needs gradients written by hand. Placing work across several machines has been worked out and checked in simulation only.