# Polydat
Polydat is a compiler and runtime for deterministic procedural data in
Rust. You declare typed inputs and a graph of named functions; Polydat
compiles the graph into a reusable kernel whose named outputs are pulled
on demand, as native machine code where the graph permits it.
The same input always yields the same values, on any thread, any host,
and any engine, with no state carried between evaluations. That is the
whole contract, and everything else in the crate serves it: the DSL,
the node library, the type system, the parameter-space comprehensions,
the tile templates, and the host embedding surface.
Polydat powers the variates subsystem for
[nmbrs](https://github.com/nosqlbench/nmbrs), the successor to
NoSQLBench, and is built to embed in any host.
## Start here
The grammar, the scope model and the node library are how a host shapes
data and carries it between scopes, and each is documented. Before
concluding polydat cannot do something, look it up:
- [Questions a host asks](https://github.com/nosqlbench/polydat/blob/main/crates/polydat/docs/guides/host_questions.md):
how a constant, and how a per-cycle value, reaches a child scope, and
what a kernel knows before its first cycle, each answered by running
two programs.
- [The grammar](https://github.com/nosqlbench/polydat/blob/main/crates/polydat/docs/design/polydat_grammar.md) and
[the node reference](https://github.com/nosqlbench/polydat/blob/main/crates/polydat/docs/reference/nodes.md): what a
program can say and what it can call.
- [Embedding Polydat](https://github.com/nosqlbench/polydat/blob/main/crates/polydat/docs/guides/embedding.md): what a host
owns and what polydat owns.
- [The documentation index](https://github.com/nosqlbench/polydat/blob/main/crates/polydat/docs/README.md): everything
else, by section.
An agent starts at [AGENTS.md](https://github.com/nosqlbench/polydat/blob/main/crates/polydat/AGENTS.md), which routes a
question to the document that answers it. The documentation ships
inside this crate's package, under `docs/`.
## Quick start
```toml
[dependencies]
polydat = "0.12"
```
Compile a graph from source and pull named results:
```rust
use polydat::dsl::compile_polydat_kernel;
fn main() -> Result<(), polydat::KernelError> {
let mut kernel = compile_polydat_kernel(r#"
input cycle: u64
hashed := hash(cycle)
user_id := mod(hashed, 1000000)
bucket := mod(hashed, 64)
"#)?;
for cycle in 0..5 {
kernel.set_inputs(&[cycle]);
let user_id = kernel.pull("user_id").as_u64();
let bucket = kernel.pull("bucket").as_u64();
println!("{cycle:>5} {user_id:>7} {bucket:>6}");
}
Ok(())
}
```
```text
0 607535 47
1 822465 1
2 348110 14
3 139053 45
4 603978 10
```
The same program with a header and ten rows is `examples/basic.rs`; run
it with `cargo run -p polydat --example basic`. The
[examples directory](https://github.com/nosqlbench/polydat/tree/main/crates/polydat/examples) also covers the programmatic
assembler, modules, context layering, parameter-space projection, cursor
partitions, traversal strategies, type safety, sharing a kernel across
threads, and defining your own nodes.
## Engines
One program compiles to any of four engines and gives the same values
on each. The host picks with `Engine`; the default is native when the
build has `jit`, and closures without it.
| Interpreter | Boxed nodes over typed value buffers | always |
| Closures | One generated closure per node over a flat slot buffer | always |
| Native | Cranelift machine code where a node has a lowering, its closure elsewhere | `jit` |
| Pure native | The whole program as native code, calling a node's kit where it has no lowering | `jit` |
## Cargo features
| `jit` | yes | The native engine, on Cranelift. |
| `cli` | yes | The `polydat` command-line harness. |
| `vectordata` | no | Vector-dataset access nodes for ML/AI-oriented workloads. |
For an interpreter-and-closures library build without Cranelift or the
binary:
```toml
[dependencies]
polydat = { version = "0.12", default-features = false }
```
## Why one subsystem
Polydat is the reduction of NoSQLBench's core features that a Rust port
forced into a minimal shape. The lesson that shaped it: managing many
testing and simulation parameters across different places and formats
is overwhelming. Workload parameters, template parameters, procedural
recipes, op-template fields, and dimensional labels are all values in
one graph under one grammar, so a single set of precedence and
referencing rules applies everywhere, and any value can be asked where
it comes from and how it is computed.
## Documentation
The rustdoc on [docs.rs](https://docs.rs/polydat) covers the API. The
[documentation index](https://github.com/nosqlbench/polydat/blob/main/crates/polydat/docs/README.md)
lists the narrative documentation by section.
Tutorials, each backed by a runnable example:
- [Illustrations](https://github.com/nosqlbench/polydat/blob/main/crates/polydat/docs/tutorials/illustrations.md):
the DSL and the programmatic assembler, function graphs, libraries,
parameter spaces, partitions, traversal, and tiles.
- [Polytile tutorial](https://github.com/nosqlbench/polydat/blob/main/crates/polydat/docs/tutorials/polytile_tutorial.md):
templates whose holes are wires, rendering JSON, CSV, and text per
cycle.
- [A toy test definition](https://github.com/nosqlbench/polydat/blob/main/crates/polydat/docs/tutorials/toy_test_definition.md):
one grammar for coordinates, a workload, and the documents it emits.
Guides:
- [Embedding Polydat](https://github.com/nosqlbench/polydat/blob/main/crates/polydat/docs/guides/embedding.md):
what a host owns and what Polydat owns, the APIs for compiling,
driving, sharing, and extending a kernel, and the extension points.
- [Compilation](https://github.com/nosqlbench/polydat/blob/main/crates/polydat/docs/guides/compilation.md):
the four engines, how to choose one, and the Cargo features.
- [Engine-ladder performance](https://github.com/nosqlbench/polydat/blob/main/crates/polydat/docs/guides/performance.md):
one typed graph measured on every engine.
- [Porting to 0.6.0](https://github.com/nosqlbench/polydat/blob/main/crates/polydat/docs/guides/porting_to_0_6_0.md):
every change a host sees from 0.5.
Reference and design:
- [Nodes](https://github.com/nosqlbench/polydat/blob/main/crates/polydat/docs/reference/nodes.md):
the built-in function nodes by family.
- [Design documents](https://github.com/nosqlbench/polydat/tree/main/crates/polydat/docs/design):
the specifications the code implements, with their axioms.
## Crates
`polydat` is the facade. It re-exports three crates at the paths they
always had and holds the binary, the tests, the benches, the examples,
and the docs.
- [`polydat-grammar`](https://crates.io/crates/polydat-grammar): the
language without the runtime.
- [`polydat-core`](https://crates.io/crates/polydat-core): the runtime,
compiler, and engines.
- [`polydat-nodes`](https://crates.io/crates/polydat-nodes): the node
library.
- [`polydat-derive`](https://crates.io/crates/polydat-derive): the
`#[polydat_node]` attribute.
## License
Apache-2.0. See [LICENSE](https://github.com/nosqlbench/polydat/blob/main/LICENSE).