fix(anatomy): R5.4 records weigh every cloned string with its header

This commit is contained in:
bchanot committed 2026-10-23 15:34:38 +02:00
1 parent 8bdb079f70
commit 29eb499539
5 files changed
+133 -15

No files matched your search

+21 -11
View File
@@ -147,9 +147,10 @@ impl<'a> Recorder<'a> {
}
}
/// What a record holds, in weight units: 64 per JSON value (a `Value` node costs tens of
/// bytes) plus one per byte of every string: the values, and the change and the op, which each
/// record clones. A rename or a remove records the whole item and one op can record once per
/// What a record holds, in weight units, about its bytes in memory: 64 per JSON value and per
/// string (a `Value` node or a `String` header and its allocation cost tens of bytes, so a run of
/// one-character ids is not free) plus one per byte of text: the values, and the change and the
/// op, which each record clones. A rename or a remove records the whole item and one op can record once per
/// item it reaches, so the records can grow far past the document: `charge` bounds their total.
pub fn record_weight(record: &ChangeRecord) -> usize {
parts_weight(&RecordParts {
@@ -173,22 +174,31 @@ fn parts_weight(parts: &RecordParts<'_>) -> usize {
parts.target,
parts.field.unwrap_or_default(),
];
let text: usize = texts.iter().map(|text| text.len()).sum::<usize>()
+ change.reference_ids.iter().map(String::len).sum::<usize>();
let text: usize = texts.iter().map(|text| text_weight(text)).sum::<usize>()
+ change
.reference_ids
.iter()
.map(|id| text_weight(id))
.sum::<usize>();
text + op_weight(parts.op) + value_weight(parts.before) + value_weight(parts.after)
}
/// The op a record clones: its target, its value or its ids.
fn op_weight(op: &Op) -> usize {
let carried = match op {
Op::Set(op) => op.field.len() + value_weight(&op.value),
Op::Set(op) => text_weight(&op.field) + value_weight(&op.value),
Op::Add(op) => value_weight(&op.value),
Op::Rename(op) => op.id.len(),
Op::Move(op) => op.to.len(),
Op::Cite(op) => op.reference_ids.iter().map(|id| 64 + id.len()).sum(),
Op::Rename(op) => text_weight(&op.id),
Op::Move(op) => text_weight(&op.to),
Op::Cite(op) => op.reference_ids.iter().map(|id| text_weight(id)).sum(),
Op::Remove(_) => 0,
};
64 + op.target().len() + carried
64 + text_weight(op.target()) + carried
}
/// A cloned string: its header and allocation, then its bytes.
fn text_weight(text: &str) -> usize {
64 + text.len()
}
/// The weight of a value, walked with an explicit stack (no recursion).
@@ -200,7 +210,7 @@ fn value_weight(value: &Value) -> usize {
match value {
Value::String(text) => weight += text.len(),
Value::Object(map) => {
weight += map.keys().map(String::len).sum::<usize>();
weight += map.keys().map(|key| text_weight(key)).sum::<usize>();
pending.extend(map.values());
}
Value::Array(items) => pending.extend(items),
+14 -1
View File
@@ -833,11 +833,24 @@ fn the_records_bound_holds_inside_one_op_and_the_marking() {
("B", "r".repeat(8_192), json!([add])),
("A", "Essai".to_owned(), json!([add, subtree])),
];
// A run of one-character reference ids weighs its headers too (security gate R5.4 round 9:
// 200,000 ids cloned into each record reached 948 MB at one unit per byte).
let mut cases = cases.to_vec();
let sets: Vec<Value> = (0..100)
.map(|i| json!({ "op": "set", "target": MOBILITY, "field": "name.en", "value": format!("v{i}") }))
.collect();
cases.push(("ids", "Essai".to_owned(), Value::from(sets)));
for (level, rationale, ops) in cases {
let ids = if level == "ids" {
vec!["atlas"; 200_000]
} else {
Vec::new()
};
let level = if level == "ids" { "A" } else { level };
let file = json!({ "changes": [{
"id": "CHG-001", "level": level, "kind": "technical",
"title": { "fr": "Essai", "en": "Test" }, "rationale": rationale,
"reference_ids": [], "ops": ops,
"reference_ids": ids, "ops": ops,
}]});
let changeset = parse_changeset(&to_js_compact(&file).unwrap()).unwrap();
let error = apply_changeset(&frozen["baseline"], &references, &changeset.changes)