Skip to content

Commit 108eb9e

Browse files
committed
feat: add statements and promotion
Statements could only be issued by `abandon`, and nothing read them back or carried a result over to a new revision. `origin annotate` issues a statement about a revision, superseding the user's previous one of the same kind, `origin statements` lists them, and `origin promote` carries promotable results over from records with the same input digest, as new statements pointing at the original through `Statement::promoted_from`. A promotion claims what its original does, so it only counts once checked: `Store::claim` makes a `Claim` only of a promotion whose original the store has and is promotable, with the same kind, verdict, inputs and payload, about a record with the same input digest. Issuing a promotion checks it, and readers count only the claims that hold, of statements nobody superseded. Reserved kinds have authors (`Store::authorizes`): the change's creator, who wrote its root revisions, for `core.abandon`; a node or the revision's author for `core.conflict`; a node for `core.divergent`. Reserved kinds never promote. The input digest hashes what a snapshot stores at each declared path, with the definition and toolchain, so it costs one lookup per path. The index groups promotable statements by kind, issuer and inputs and caches their digests, so a search reads one candidate per group. Supersession counts only within one issuer. Divergence issues `core.divergent` once per set of heads, when a saved revision makes a change diverge.
1 parent c21e9be commit 108eb9e

19 files changed

Lines changed: 2376 additions & 75 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ to a hot path (hashing, storage, snapshots, transfer) should come with a
2727
benchmark, and its numbers in the pull request.
2828

2929
- `just bench`: the divan micro-benchmarks (encoding, store writes,
30-
snapshots).
30+
snapshots, input digests and promotion).
3131
- `just bench-git`: times origin against git on a 20,000-file tree with
3232
hyperfine, and rewrites `docs/benchmarks.md`.
3333

‎README.md‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@ origin edit <change> # switch the working copy to another change
2424
origin abandon --reason "Too slow" # give a change up, keeping it for others to see
2525
origin converge <change> # end a change's divergence
2626
origin index rebuild # rebuild the record index from the store
27+
28+
origin annotate @ ci.test --pass --inputs src Cargo.toml # a statement about a revision
29+
origin statements [<rev>] # the statements about a revision
30+
origin promote [<rev>] # carry results over from revisions with the same inputs
2731
```
2832

2933
Changes are named by a unique prefix of their id, or `@` for the working
30-
copy's. Your name and node key live in `$ORIGIN_CONFIG_DIR`, or else
31-
`origin` in `$XDG_CONFIG_HOME` or `~/.config`: set `user = "<name>"` in
32-
its `config.toml` if `$USER` isn't the name you want. See
34+
copy's. A revision is `@`, a change (for its head), or a unique prefix of
35+
the revision's id. Your name and node key live in `$ORIGIN_CONFIG_DIR`, or
36+
else `origin` in `$XDG_CONFIG_HOME` or `~/.config`: set `user = "<name>"`
37+
in its `config.toml` if `$USER` isn't the name you want. See
3338
[the design](docs/design.md) for the model.
3439

3540
## Development

‎apps/origin-src/Cargo.toml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ divan.workspace = true
3333
origin-src = { workspace = true, features = ["fixture"] }
3434
tempfile.workspace = true
3535

36+
[[bench]]
37+
name = "annotations"
38+
harness = false
39+
3640
[[bench]]
3741
name = "snapshot"
3842
harness = false
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
//! Statements: input digests over a large snapshot, and the promotion
2+
//! search over many statements, with their digests cached by the index and
3+
//! without.
4+
5+
use std::fs;
6+
7+
use divan::Bencher;
8+
use divan::counter::ItemsCount;
9+
use origin_platform::Native;
10+
use origin_shared::{
11+
Base, ChangeId, Digest, EncodedObject, EntryKind, EntryName, Inputs, Kind, ObjectId, Principal,
12+
Record, RecordId, RepoPath, Revision, RevisionId, Statement, Tree, TreeEntry, TreeId, Verdict,
13+
};
14+
use origin_src::annotations::Annotations;
15+
use origin_src::fixture::FixtureTree;
16+
use origin_src::index::Index;
17+
use origin_src::store::Store;
18+
use origin_src::tree::Snapshots;
19+
use origin_src::worktree::{StatCache, Worktree};
20+
21+
fn main() {
22+
divan::main();
23+
}
24+
25+
/// An initialized store in a new temporary directory.
26+
fn store() -> (tempfile::TempDir, Store) {
27+
let dir = tempfile::tempdir().unwrap();
28+
let store = Store::new(dir.path().join(".origin"), Native::default());
29+
store.init().unwrap();
30+
(dir, store)
31+
}
32+
33+
fn inputs(paths: &[&str], definition: &str) -> Inputs {
34+
Inputs {
35+
paths: paths
36+
.iter()
37+
.map(|path| RepoPath::new(path).unwrap())
38+
.collect(),
39+
definition: Digest::of(definition.as_bytes()),
40+
toolchain: Digest::of(b"rustc 1.99.0"),
41+
external: false,
42+
}
43+
}
44+
45+
/// The input digest of the 20k-file fixture, for inputs naming two
46+
/// directories, a file and a missing path. Each run reads the trees anew.
47+
#[divan::bench(sample_count = 50)]
48+
fn input_digest(bencher: Bencher<'_, '_>) {
49+
let dir = tempfile::tempdir().unwrap();
50+
FixtureTree::LARGE.write(dir.path()).unwrap();
51+
let store = Store::new(dir.path().join(".origin"), Native::default());
52+
store.init().unwrap();
53+
let (snapshot, _) = Worktree::new(dir.path(), &store)
54+
.snapshot(&StatCache::default())
55+
.unwrap();
56+
let inputs = inputs(
57+
&["dir0", "dir5/sub3", "dir12/sub7/file7820.rs", "missing"],
58+
"cargo test",
59+
);
60+
bencher.bench(|| {
61+
Snapshots::new(&store)
62+
.input_digest(snapshot, &inputs)
63+
.unwrap()
64+
});
65+
}
66+
67+
/// How many statements the promotion search runs over.
68+
const STATEMENTS: usize = 20_000;
69+
70+
/// The checks each revision has results of, with the inputs they declare.
71+
const CHECKS: [(&str, &[&str]); 10] = [
72+
("ci.lint", &["src", "Cargo.toml"]),
73+
("ci.build", &["src", "Cargo.toml"]),
74+
("ci.test", &["src", "tests", "Cargo.toml"]),
75+
("ci.docs", &["src", "docs"]),
76+
("ci.links", &["docs"]),
77+
("ci.fmt", &["src", "tests"]),
78+
("ci.bench", &["src", "benches", "Cargo.toml"]),
79+
("ci.audit", &["Cargo.toml"]),
80+
("ci.spell", &["docs", "README.md"]),
81+
("ci.unit", &["src"]),
82+
];
83+
84+
/// A snapshot of a small crate, at version `version` of its sources and
85+
/// `docs` of its documentation.
86+
fn snapshot(version: usize, docs: usize) -> Vec<EncodedObject> {
87+
let file = |content: String| {
88+
let blob = EncodedObject::blob(content.as_bytes());
89+
let entry = TreeEntry {
90+
kind: EntryKind::File,
91+
id: blob.id(),
92+
};
93+
(blob, entry)
94+
};
95+
let mut objects = Vec::new();
96+
let mut root = Vec::new();
97+
for (dir, content) in [
98+
("src", format!("fn v{version}() {{}}")),
99+
("tests", format!("#[test] fn t{}() {{}}", version / 2)),
100+
("benches", "fn bench() {}".to_owned()),
101+
("docs", format!("# Guide {docs}")),
102+
] {
103+
let (blob, entry) = file(content);
104+
let tree: Tree = [(EntryName::new("a").unwrap(), entry)]
105+
.into_iter()
106+
.collect();
107+
let tree = tree.encode();
108+
root.push((
109+
EntryName::new(dir).unwrap(),
110+
TreeEntry {
111+
kind: EntryKind::Tree,
112+
id: tree.id(),
113+
},
114+
));
115+
objects.extend([blob, tree]);
116+
}
117+
for (name, content) in [("Cargo.toml", "[package]"), ("README.md", "# Readme")] {
118+
let (blob, entry) = file(content.to_owned());
119+
root.push((EntryName::new(name).unwrap(), entry));
120+
objects.push(blob);
121+
}
122+
objects.push(root.into_iter().collect::<Tree>().encode());
123+
objects
124+
}
125+
126+
/// A store with a revision of its own change for each version, each with a
127+
/// result of every check, 20k statements in all, and a revision whose
128+
/// sources match one of them.
129+
fn promotion_store() -> (tempfile::TempDir, Store, RevisionId) {
130+
let (dir, store) = store();
131+
let revision = |store: &Store, change: usize, version: usize, docs: usize| {
132+
let objects = snapshot(version, docs);
133+
let root = TreeId::from(objects.last().unwrap().id());
134+
store.write_all(&objects).unwrap();
135+
let mut id = [0; 16];
136+
id[..8].copy_from_slice(&(change as u64).to_le_bytes());
137+
let encoded = Revision {
138+
change: ChangeId::from_bytes(id),
139+
predecessors: Vec::new(),
140+
base: Base::Acceptance(ObjectId::from_bytes([0; 32]).into()),
141+
deps: Vec::new(),
142+
snapshot: root,
143+
description: EncodedObject::blob(b"").id().into(),
144+
author: "user:bench".parse().unwrap(),
145+
}
146+
.encode();
147+
store.write_all(std::slice::from_ref(&encoded)).unwrap();
148+
RevisionId::from(encoded.id())
149+
};
150+
let revisions = STATEMENTS / CHECKS.len();
151+
let mut statements = Vec::new();
152+
for version in 0..revisions {
153+
let revision = revision(&store, version, version, version % 7);
154+
statements.extend(CHECKS.map(|(kind, paths)| {
155+
Statement {
156+
subject: RecordId::Revision(revision),
157+
kind: Kind::new(kind).unwrap(),
158+
verdict: Some(Verdict::Pass),
159+
issuer: "node:ci-1".parse().unwrap(),
160+
inputs: Some(inputs(paths, kind)),
161+
payload: None,
162+
supersedes: None,
163+
promoted_from: None,
164+
}
165+
.encode()
166+
}));
167+
}
168+
store.write_all(&statements).unwrap();
169+
// Version 1000's sources with the docs of other versions: the seven
170+
// checks that don't read docs promote from version 1000, the two that
171+
// only read docs from the latest version with those docs.
172+
let target = revision(&store, revisions, 1000, 3);
173+
(dir, store, target)
174+
}
175+
176+
/// Searching 20k statements for those to promote to a revision, with every
177+
/// digest cached: the cost of each search after the first.
178+
#[divan::bench(sample_count = 20)]
179+
fn promotions_cached(bencher: Bencher<'_, '_>) {
180+
let (_dir, store, target) = promotion_store();
181+
let issuer: Principal = "node:queue".parse().unwrap();
182+
let mut annotations = Annotations::open(&store).unwrap();
183+
assert_eq!(annotations.promotions(target, &issuer).unwrap().len(), 9);
184+
let index = Index::open(&store).unwrap();
185+
bencher
186+
.counter(ItemsCount::new(STATEMENTS))
187+
.with_inputs(|| Annotations::new(&store, index.clone()))
188+
.bench_local_refs(|annotations| annotations.promotions(target, &issuer).unwrap());
189+
}
190+
191+
/// The first search, which computes and remembers the digest of each of
192+
/// the 20k statements, reading its subject's trees.
193+
#[divan::bench(sample_count = 10, sample_size = 1)]
194+
fn promotions_uncached(bencher: Bencher<'_, '_>) {
195+
let (dir, store, target) = promotion_store();
196+
let issuer: Principal = "node:queue".parse().unwrap();
197+
let log = dir.path().join(".origin/index/log");
198+
bencher
199+
.counter(ItemsCount::new(STATEMENTS))
200+
.with_inputs(|| {
201+
// Rebuilding without the old index forgets every digest.
202+
fs::remove_file(&log).unwrap();
203+
Annotations::open(&store).unwrap()
204+
})
205+
.bench_local_refs(|annotations| annotations.promotions(target, &issuer).unwrap());
206+
}

‎apps/origin-src/benches/packs.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ fn statements(seed: usize, count: usize) -> Vec<EncodedObject> {
3737
inputs: None,
3838
payload: None,
3939
supersedes: None,
40+
promoted_from: None,
4041
}
4142
.encode()
4243
})

‎apps/origin-src/benches/store.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ fn records(count: usize) -> Vec<EncodedObject> {
168168
inputs: None,
169169
payload: None,
170170
supersedes: None,
171+
promoted_from: None,
171172
}
172173
.encode()
173174
}

0 commit comments

Comments
 (0)