Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ approx = "0.5"
rust_decimal = "1"
rust_decimal_macros = "1"
indexmap = { version = "2", features = ["serde"] }
im = "15"
clap = { version = "4", features = ["derive"] }
rgb = { version = "0.8", features = ["serde"] }
thiserror = "2"
Expand Down
26 changes: 15 additions & 11 deletions bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ how an axis scales — without editing any source. Pass a comma-separated list:
| Env var | Axis | Default |
| ------- | ---- | ------- |
| `ARGON_BENCH_SHAPES` | shapes (recursion) | `500,1000,2000,4000,8000,16000,32000` |
| `ARGON_BENCH_SHAPES_LOOP` | shapes (`for` loop) | `250,500,1000,2000` |
| `ARGON_BENCH_SHAPES_LOOP` | shapes (`for` loop) | `500,1000,2000,4000,8000,16000,32000` |
| `ARGON_BENCH_INSTANCES` | instances | `500,…,64000` |
| `ARGON_BENCH_CONSTRAINTS` | coupled constraints | `32,64,128,256,512,1024` |
| `ARGON_BENCH_HIER_SINGLE` | hierarchy (1 ref) | `4,8,16,32,48,64,96,128` |
Expand Down Expand Up @@ -104,7 +104,7 @@ parameter; "peak" is peak heap allocated during compilation.
| Instances | 64 000 insts | 3.14 s | 1.29 GiB | **~linear** (time `∝ n^1.2`, mem `∝ n^1.0`) |
| Hierarchy, 1 child ref | depth 128 | 0.09 s | 0.12 GiB | **polynomial** (`∝ depth^1.3–1.4`) |
| Coupled constraints | 1 024 rects | 21.7 s | 0.13 GiB | **super-cubic in time** (see below) |
| Shapes (`for`-loop) | 2 000 rects | 0.59 s | 4.1 GiB | **quadratic** (mem `∝ n^2`) |
| Shapes (`for`-loop) | 32 000 rects | 1.07 s | 0.85 GiB | **~linear** (time `∝ n^1.2`, mem `∝ n^1.0`) |
| Hierarchy, 2 child refs | depth 18 | 11.5 s | 3.6 GiB | **exponential** (`×1.9` per level) |

### Interpretation
Expand Down Expand Up @@ -141,15 +141,19 @@ parameter; "peak" is peak heap allocated during compilation.
capped). Very deep hierarchies additionally hit a native-recursion stack
limit in the compiler at a few hundred levels.

- **Recursion vs. iteration measures the list/iteration machinery.** `shapes`
and `shapes_loop` emit identical geometry; the only difference is that
`shapes_loop` builds and iterates a `std::range` list. On the build measured
here that list path is markedly heavier (≈4 GiB to emit 2 000 rectangles via
a `for` loop, vs. 32 000 by recursion in under 1 GiB), so the gap between the
two series is a direct measure of the cost of the list representation rather
than of the geometry or solver. Re-running both series (e.g. with
`ARGON_BENCH_SHAPES_LOOP` set to the same sizes as `bench_shapes`) is the way
to see that cost change as the iteration/list machinery is optimized.
- **Recursion and iteration now scale identically.** `shapes` and `shapes_loop`
emit identical geometry; the only difference is that `shapes_loop` builds and
iterates a `std::range` list. That list path used to be quadratic — emitting
just 2 000 rectangles through a `for` loop cost ≈4 GiB (against 32 000 by
recursion in under 1 GiB), because `range` was built by repeated `cons` onto a
`Vec` (an O(n) clone-and-prepend per element). Backing sequences with a
persistent vector and lowering `range` to a native builtin made `cons`
O(log n) and `range` O(n); the two series now coincide, both linear in time
and memory out to 32 000 rectangles (`shapes_loop`: 1.07 s / 0.85 GiB;
`shapes`: 1.53 s / 0.89 GiB). The idiomatic `for i in std::range(n)` loop is
no longer a scaling hazard. The gap between the two series is now a small
constant — `shapes_loop` is even marginally faster, as the native `range`
avoids the per-element recursion overhead of `emit_shapes`.

The takeaways for the paper: editable-object count and instance count scale
linearly; the practically-relevant limits are the dense general constraint
Expand Down
Binary file modified bench/argon_scaling.pdf
Binary file not shown.
Binary file modified bench/argon_scaling.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions bench/results/constraints.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
size,time_s,peak_bytes,n_objects
32,0.00383075,2585876,33
64,0.00726206,3829492,65
128,0.029626408,6749992,129
256,0.21347691,15396760,257
512,1.4369801039999999,42127416,513
1024,21.745505947,133337720,1025
32,0.003910329,2558765,33
64,0.007259055,3802445,65
128,0.029592347,6723977,129
256,0.213075799,15371913,257
512,1.437183028,42104953,513
1024,21.973577632,133319817,1025
18 changes: 9 additions & 9 deletions bench/results/hierarchy_double_ref.csv
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
size,time_s,peak_bytes,n_objects
2,0.000971778,1325410,5
4,0.001176136,1653373,9
6,0.001882165,2474335,13
8,0.004620015,5364013,17
10,0.015455478,16602635,21
12,0.088864192,61484713,25
14,0.481928844,240102311,29
16,2.506498165,954179013,33
18,11.538940627,3810189475,37
2,0.000989074,1298738,5
4,0.001195103,1626764,9
6,0.001874394,2447723,13
8,0.004614937,5337431,17
10,0.015541538,16576083,21
12,0.065779532,61458213,25
14,0.450641777,240075819,29
16,2.584315362,954152551,33
18,11.463743993,3810163043,37
16 changes: 8 additions & 8 deletions bench/results/hierarchy_single_ref.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
size,time_s,peak_bytes,n_objects
4,0.000845819,1556216,9
8,0.001178198,2140495,17
16,0.002346772,4034443,33
32,0.005942098,10387267,65
48,0.012042876,19826107,97
64,0.022953693,33362240,129
96,0.052964538,69348808,193
128,0.090173076,120382868,257
4,0.000770567,1529602,9
8,0.001090958,2113873,17
16,0.002090448,4007901,33
32,0.005648728,10360885,65
48,0.01153892,19799885,97
64,0.021413491,33336178,129
96,0.05172001,69323054,193
128,0.08977602,120357446,257
16 changes: 8 additions & 8 deletions bench/results/instances.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
size,time_s,peak_bytes,n_objects
500,0.012283447,11806766,501
1000,0.02516376,22390998,1001
2000,0.056507946,43559462,2001
4000,0.147355775,85896390,4001
8000,0.310525996,170570230,8001
16000,0.689046789,339917926,16001
32000,1.457187325,678613350,32001
64000,3.140683519,1356004118,64001
500,0.012092831,11781069,501
1000,0.026317872,22366301,1001
2000,0.056346714,43536765,2001
4000,0.142755802,85877693,4001
8000,0.306179635,170559549,8001
16000,0.677504909,339923245,16001
32000,1.443219008,678650637,32001
64000,3.11408106,1356105421,64001
14 changes: 7 additions & 7 deletions bench/results/shapes.csv
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
size,time_s,peak_bytes,n_objects
500,0.012574383,16159584,500
1000,0.028867058,31116160,1000
2000,0.071651056,61029296,2000
4000,0.158150608,120855616,4000
8000,0.329667899,240508160,8000
16000,0.70516885,479813376,16000
32000,1.530754693,958423696,32000
500,0.011923036,16139393,500
1000,0.024312932,31102469,1000
2000,0.069437864,61028621,2000
4000,0.147926021,120880909,4000
8000,0.317370371,240585533,8000
16000,0.706298237,479994669,16000
32000,1.528382155,958813069,32000
11 changes: 7 additions & 4 deletions bench/results/shapes_loop.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
size,time_s,peak_bytes,n_objects
250,0.026441618,73227389,250
500,0.091560351,274248679,500
1000,0.269871454,1063292012,1000
2000,0.589680644,4189379419,2000
500,0.006984864,15422856,500
1000,0.01352496,29623733,1000
2000,0.04564351,58027149,2000
4000,0.097997791,114789917,4000
8000,0.211025502,228319133,8000
16000,0.463940318,455443074,16000
32000,1.0717253,909703618,32000
1 change: 1 addition & 0 deletions core/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ arcstr = { workspace = true }
serde = { workspace = true }
approx = { workspace = true }
indexmap = { workspace = true }
im = { workspace = true }
geometry = { workspace = true }
uniquify = { workspace = true }
rgb = { workspace = true }
Expand Down
80 changes: 73 additions & 7 deletions core/compiler/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ use crate::{
solver::{LinearExpr, Solver},
};

pub const BUILTINS: [&str; 12] = [
pub const BUILTINS: [&str; 13] = [
"list",
"cons",
"head",
"tail",
"range_full",
"crect",
"rect",
"text",
Expand Down Expand Up @@ -1648,6 +1649,13 @@ impl<'a> AstTransformer for VarIdTyPass<'a> {
(None, Ty::Seq(Box::new(elem_ty)))
}
}
"range_full" => {
// Native builtin backing `std::range`/`std::range_full`: builds the
// whole `[Int]` in one pass instead of recursive `cons`.
self.typecheck_posargs(input.span, &args.posargs, &[Ty::Int, Ty::Int, Ty::Int]);
self.typecheck_kwargs(&args.kwargs, IndexMap::default());
(None, Ty::Seq(Box::new(Ty::Int)))
}
"head" => {
self.assert_eq_arity(input.span, args.posargs.len(), 1);
if args.posargs.len() == 1 {
Expand Down Expand Up @@ -3529,11 +3537,15 @@ impl<'a> ExecPass<'a> {
) {
let val = match tail {
Value::SeqNil => {
vec![head.clone()]
let mut s = Seq::new();
s.push_back(head.clone());
s
}
Value::Seq(s) => {
// O(1) structural clone + O(log n) prepend (was O(n) deep
// clone + O(n) front-insert, making `range` O(n^2)).
let mut s = s.clone();
s.insert(0, head.clone());
s.push_front(head.clone());
s
}
_ => {
Expand Down Expand Up @@ -3576,6 +3588,44 @@ impl<'a> ExecPass<'a> {
false
}
}
"range_full" => {
if let (Defer::Ready(start), Defer::Ready(stop), Defer::Ready(step)) = (
&self.values[&c.state.posargs[0]],
&self.values[&c.state.posargs[1]],
&self.values[&c.state.posargs[2]],
) {
if let (Value::Int(start), Value::Int(stop), Value::Int(step)) =
(start, stop, step)
{
// Build the whole `[Int]` in one O(n) pass (O(log n) pushes),
// avoiding the per-element interpreter overhead (frame, scope,
// deferred value) of the old recursive `cons` definition.
let mut seq = Seq::new();
if *step > 0 {
let mut i = *start;
while i < *stop {
seq.push_back(Value::Int(i));
i += *step;
}
}
self.values.insert(vid, Defer::Ready(Value::Seq(seq)));
true
} else {
let span = self.span(&vref.loc, c.expr.span);
self.errors.push(ExecError {
span: Some(span),
cell: cell_id,
kind: ExecErrorKind::InvalidType,
});
return Err(());
}
} else {
self.add_value_dependent(c.state.posargs[0], vid);
self.add_value_dependent(c.state.posargs[1], vid);
self.add_value_dependent(c.state.posargs[2], vid);
false
}
}
"head" => {
if let Defer::Ready(head) = &self.values[&c.state.posargs[0]] {
let val = match head {
Expand All @@ -3589,7 +3639,7 @@ impl<'a> ExecPass<'a> {
return Err(());
}
Value::Seq(s) => {
if let Some(s) = s.first() {
if let Some(s) = s.front() {
s.clone()
} else {
let span = self.span(&vref.loc, c.expr.span);
Expand Down Expand Up @@ -3632,7 +3682,12 @@ impl<'a> ExecPass<'a> {
}
Value::Seq(s) => {
if !s.is_empty() {
Value::Seq(s[1..].to_vec())
// Drop the head: O(1) structural clone + O(log n)
// pop_front (was O(n) `s[1..].to_vec()`, which made
// `tail`-recursion such as `std::last` O(n^2)).
let mut s = s.clone();
s.pop_front();
Value::Seq(s)
} else {
let span = self.span(&vref.loc, c.expr.span);
self.errors.push(ExecError {
Expand Down Expand Up @@ -4534,8 +4589,9 @@ impl<'a> ExecPass<'a> {
PartialEvalState::ForLoop(f) => {
if let Defer::Ready(val) = &self.values[&f.seq] {
let seq = match val.as_ref() {
// `s.clone()` is now an O(1) refcount bump (was an O(n) deep copy).
ValueRef::Seq(s) => s.clone(),
ValueRef::SeqNil => Vec::new(),
ValueRef::SeqNil => Seq::new(),
_ => {
let span = self.span(&vref.loc, f.for_loop.seq.span());
self.errors.push(ExecError {
Expand Down Expand Up @@ -4605,6 +4661,16 @@ impl<'a> ExecPass<'a> {
}
}

/// Persistent immutable sequence backing `Value::Seq`.
///
/// Backed by an RRB-tree (`im::Vector`): O(1) clone (structural sharing) and
/// O(log n) `push_front`/`get`/`pop_front`. This keeps `cons` (used to build
/// `range`) at O(log n) instead of the O(n) clone+prepend a `Vec` requires, so
/// building `range(n)` is O(n log n) rather than O(n^2), while random indexing
/// (`arr[i]`) stays O(log n). `im::Vector` is `Arc`-backed, so `Seq` is `Send`
/// exactly when `Value` is — no regression for the (tokio) language server.
type Seq = im::Vector<Value>;

#[enumify]
#[derive(Debug, Clone)]
pub enum Value {
Expand Down Expand Up @@ -4652,7 +4718,7 @@ pub enum Value {
///
/// `mycell_inst` is a value of type `Inst`.
Inst(Instance),
Seq(Vec<Value>),
Seq(Seq),
Tuple(Vec<Value>),
SeqNil,
Nil,
Expand Down
Loading
Loading