fix(anatomy): R5.4 records weigh every cloned string with its header
This commit is contained in:
1 parent
8bdb079f70
commit
29eb499539
5 files changed
+133
-15
No files matched your search
@@ -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),
|
||||
|
||||
Reference in new issue
Block a user