relate-executor 1.0.1

In-memory graph and Cypher physical executor
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
//! Owned resident workspace of one statement.
//!
//! Query peak bytes are the high-water mark of this workspace. The formula
//! charges containers the statement allocates: vector headers and
//! [`reserved_len`] capacity, one shared row schema per distinct schema,
//! inline values at their slot size, a shallow node or relationship
//! wrapper, and deep payload only for an allocation this statement created.
//! A cloned graph `Arc` contributes its slot, not the payload again.
//! [`Value::logical_bytes`](relate_core::Value::logical_bytes) stays the
//! deep payload function and is not applied to borrowed values.

use std::collections::{BTreeMap, HashMap, HashSet};
use std::mem::size_of;
use std::sync::Arc;

#[cfg(test)]
use indexmap::IndexMap;
use relate_core::{str_logical_bytes, Node, NodeId, RelId, Relationship, Value};

use crate::UsedSet;

use crate::group_key::GroupKey;
use crate::memory::{reserved_len, OVERHEAD_MAP_ENTRY, VEC_HEADER};
use crate::row::Row;

/// Header plus reserved capacity of a vector of `len` elements. An empty
/// vector reserves nothing.
pub(crate) fn buffer_bytes(len: usize, stride: u64) -> u64 {
    if len == 0 {
        0
    } else {
        VEC_HEADER + reserved_len(len) as u64 * stride
    }
}

/// [`buffer_bytes`] with saturating arithmetic: a length whose reserved
/// capacity or slot product cannot be represented saturates to [`u64::MAX`]
/// instead of wrapping. The charge path uses this, so an extreme
/// `range`/comprehension length is refused as a typed capacity error rather
/// than wrapping the byte figure to a small value and letting
/// `Vec::with_capacity` abort the process.
pub(crate) fn buffer_bytes_saturating(len: usize, stride: u64) -> u64 {
    if len == 0 {
        return 0;
    }
    let reserved = crate::memory::reserved_len_saturating(len);
    VEC_HEADER.saturating_add(reserved.saturating_mul(stride))
}

/// The slot-buffer figure of one `Vec<Value>`: the one spelling beside
/// [`buffer_bytes_saturating`], used by the charge seam and by callers that
/// compose several buffer figures into one combined charge.
pub(crate) fn value_buffer_bytes(len: usize) -> u64 {
    buffer_bytes_saturating(len, size_of::<Value>() as u64)
}

/// The retained bytes of `count` materialized rows of `slots_per_row` value
/// slots. The row's value buffer is charged through the same reservation
/// model as [`value_buffer_bytes`] (header + power-of-two reserved slots,
/// [`buffer_bytes_saturating`]), plus its slot in the row vector
/// (`size_of::<Row>()`). Saturating; the one formula both UNWIND arms read.
pub(crate) fn retained_rows_bytes(count: usize, slots_per_row: usize) -> u64 {
    (count as u64).saturating_mul(
        buffer_bytes_saturating(slots_per_row, size_of::<Value>() as u64)
            .saturating_add(size_of::<Row>() as u64),
    )
}

/// The entry storage of a map of `len` entries: one
/// [`OVERHEAD_MAP_ENTRY`] per entry. The map container's own figure โ€” a
/// `BTreeMap` is not a `Vec`, so it must not be seeded with the value-slot
/// model. Saturating.
pub(crate) fn map_entries_bytes(len: usize) -> u64 {
    (len as u64).saturating_mul(OVERHEAD_MAP_ENTRY)
}

/// Heap this value owns. Inline values sit in their enum slot. A shared
/// `Arc` is borrowed from the graph or from another row and adds nothing.
///
/// `#[inline]` here and on the helpers below is ADR-0499's fix, not
/// decoration: the aggregate fold charges this walk per row, and when the
/// chain was a real call graph (this fn recursing through `unique_list`
/// and the entity walks, plus the cross-crate string formulas) its
/// overhead measured as 46.5% of a plain `count` โ€” a shared payload's
/// single `Arc` count read cost a call chain. Flattened, the common arms
/// inline into the charging loop and only a genuinely deep walk pays
/// depth.
#[inline]
pub(crate) fn owned_heap(value: &Value) -> u64 {
    match value {
        Value::Null
        | Value::Bool(_)
        | Value::Int(_)
        | Value::Float(_)
        | Value::Temporal(_)
        | Value::Point(_)
        | Value::Uuid(_) => 0,
        Value::String(text) => {
            if Arc::strong_count(text) == 1 {
                str_logical_bytes(text)
            } else {
                0
            }
        }
        Value::Bytes(bytes) => {
            if Arc::strong_count(bytes) == 1 {
                relate_core::bytes_logical_bytes(bytes)
            } else {
                0
            }
        }
        Value::List(items) => unique_list(items),
        Value::Vector { values, .. } => unique_list(values),
        Value::Map(entries) => {
            if Arc::strong_count(entries) == 1 {
                map_value_heap(entries)
            } else {
                0
            }
        }
        Value::Node(node) => {
            if Arc::strong_count(node) == 1 {
                size_of::<Node>() as u64 + node_heap(node)
            } else {
                0
            }
        }
        Value::Relationship(rel) => {
            if Arc::strong_count(rel) == 1 {
                size_of::<Relationship>() as u64 + rel_heap(rel)
            } else {
                0
            }
        }
        Value::Path(path) => {
            if Arc::strong_count(path) != 1 {
                return 0;
            }
            let mut bytes = size_of::<relate_core::Path>() as u64;
            bytes += buffer_bytes(path.nodes.len(), size_of::<Node>() as u64);
            bytes += buffer_bytes(path.rels.len(), size_of::<Relationship>() as u64);
            for node in &path.nodes {
                bytes += node_heap(node);
            }
            for rel in &path.rels {
                bytes += rel_heap(rel);
            }
            bytes
        }
    }
}

#[inline]
fn unique_list(items: &Arc<Vec<Value>>) -> u64 {
    if Arc::strong_count(items) != 1 {
        return 0;
    }
    buffer_bytes(items.len(), size_of::<Value>() as u64) + items.iter().map(owned_heap).sum::<u64>()
}

#[inline]
fn map_value_heap(entries: &BTreeMap<String, Value>) -> u64 {
    let mut bytes = 0;
    for (key, value) in entries {
        bytes += OVERHEAD_MAP_ENTRY;
        bytes += str_logical_bytes(key);
        bytes += owned_heap(value);
    }
    bytes
}

#[inline]
fn property_heap(entries: &relate_core::PropertyMap) -> u64 {
    created_property_bytes(entries)
}

#[inline]
fn node_heap(node: &Node) -> u64 {
    let mut bytes = 0;
    if Arc::strong_count(&node.labels) == 1 {
        bytes += buffer_bytes(node.labels.len(), size_of::<String>() as u64);
        for label in node.labels.iter() {
            bytes += str_logical_bytes(label);
        }
    }
    if Arc::strong_count(&node.properties) == 1 {
        bytes += property_heap(&node.properties);
    }
    bytes
}

#[inline]
fn rel_heap(rel: &Relationship) -> u64 {
    let mut bytes = str_logical_bytes(&rel.rel_type);
    if Arc::strong_count(&rel.properties) == 1 {
        bytes += property_heap(&rel.properties);
    }
    bytes
}

/// The row's accounted contents, and whether this call was the set's
/// first sight of the row's schema โ€” the one derivation of schema
/// freshness (F-1-19): the meter's rollback flag and its byte charge
/// come from the same `insert`, so they cannot disagree.
fn row_contents(row: &Row, schemas: &mut HashSet<usize>) -> (u64, bool) {
    let mut bytes = 0;
    let fresh_schema = schemas.insert(row.schema_ptr() as usize);
    if fresh_schema {
        bytes += row.schema_bytes();
    }
    bytes += buffer_bytes(row.values().len(), size_of::<Value>() as u64);
    for value in row.values() {
        bytes += owned_heap(value);
    }
    (bytes, fresh_schema)
}

fn rows_accounted<'a>(len: usize, rows: impl Iterator<Item = &'a Row>) -> u64 {
    let mut bytes = buffer_bytes(len, size_of::<Row>() as u64);
    let mut schemas = HashSet::new();
    for row in rows {
        bytes += row_contents(row, &mut schemas).0;
    }
    bytes
}

/// The row vector, each distinct schema once, each value vector, and the
/// heap those values own.
pub(crate) fn row_buffer_bytes(rows: &[Row]) -> u64 {
    rows_accounted(rows.len(), rows.iter())
}

/// Retained heap of a fully materialized public result. Shared payloads are
/// counted once by allocation identity, including payloads referenced by
/// more than one output row.
pub fn result_rows_bytes(rows: &[Vec<Value>]) -> u64 {
    let mut seen = HashSet::new();
    let mut bytes = buffer_bytes(rows.len(), size_of::<Vec<Value>>() as u64);
    for row in rows {
        bytes += buffer_bytes(row.len(), size_of::<Value>() as u64);
        for value in row {
            bytes += result_value_heap(value, &mut seen);
        }
    }
    bytes
}

mod retained;

pub(crate) use retained::row_retained_heap;
#[cfg(test)]
pub(crate) use retained::{note_ops, reset_note_ops};

fn mark_arc<T: ?Sized>(value: &Arc<T>, seen: &mut HashSet<usize>) -> bool {
    seen.insert(Arc::as_ptr(value) as *const () as usize)
}

fn result_value_heap(value: &Value, seen: &mut HashSet<usize>) -> u64 {
    match value {
        Value::Null
        | Value::Bool(_)
        | Value::Int(_)
        | Value::Float(_)
        | Value::Temporal(_)
        | Value::Point(_)
        | Value::Uuid(_) => 0,
        Value::String(text) => {
            if mark_arc(text, seen) {
                str_logical_bytes(text)
            } else {
                0
            }
        }
        Value::Bytes(bytes) => {
            if mark_arc(bytes, seen) {
                relate_core::bytes_logical_bytes(bytes)
            } else {
                0
            }
        }
        Value::List(items) | Value::Vector { values: items, .. } => {
            if !mark_arc(items, seen) {
                return 0;
            }
            buffer_bytes(items.len(), size_of::<Value>() as u64)
                + items
                    .iter()
                    .map(|item| result_value_heap(item, seen))
                    .sum::<u64>()
        }
        Value::Map(entries) => {
            if !mark_arc(entries, seen) {
                return 0;
            }
            entries
                .iter()
                .map(|(key, value)| {
                    OVERHEAD_MAP_ENTRY + str_logical_bytes(key) + result_value_heap(value, seen)
                })
                .sum()
        }
        Value::Node(node) => {
            if !mark_arc(node, seen) {
                return 0;
            }
            size_of::<Node>() as u64 + result_node_heap(node, seen)
        }
        Value::Relationship(rel) => {
            if !mark_arc(rel, seen) {
                return 0;
            }
            size_of::<Relationship>() as u64 + result_rel_heap(rel, seen)
        }
        Value::Path(path) => {
            if !mark_arc(path, seen) {
                return 0;
            }
            size_of::<relate_core::Path>() as u64
                + buffer_bytes(path.nodes.len(), size_of::<Node>() as u64)
                + buffer_bytes(path.rels.len(), size_of::<Relationship>() as u64)
                + path
                    .nodes
                    .iter()
                    .map(|node| result_node_heap(node, seen))
                    .sum::<u64>()
                + path
                    .rels
                    .iter()
                    .map(|rel| result_rel_heap(rel, seen))
                    .sum::<u64>()
        }
    }
}

fn result_node_heap(node: &Node, seen: &mut HashSet<usize>) -> u64 {
    let labels = if mark_arc(&node.labels, seen) {
        buffer_bytes(node.labels.len(), size_of::<String>() as u64)
            + node
                .labels
                .iter()
                .map(|label| str_logical_bytes(label))
                .sum::<u64>()
    } else {
        0
    };
    labels + result_property_heap(&node.properties, seen)
}

fn result_rel_heap(rel: &Relationship, seen: &mut HashSet<usize>) -> u64 {
    str_logical_bytes(&rel.rel_type) + result_property_heap(&rel.properties, seen)
}

fn result_property_heap(entries: &Arc<relate_core::PropertyMap>, seen: &mut HashSet<usize>) -> u64 {
    if !mark_arc(entries, seen) {
        return 0;
    }
    let stride = size_of::<(String, Value)>() as u64;
    let mut bytes = if entries.slot_capacity() == 0 {
        0
    } else {
        VEC_HEADER + entries.slot_capacity() as u64 * stride
    };
    for (key, value) in entries.iter() {
        bytes += str_logical_bytes(key);
        bytes += result_value_heap(value, seen);
    }
    bytes
}

/// Input rows an aggregation still owns. Releasing a row drops that row's
/// payload; the vector's reserved slots stay until the charge is dropped,
/// because draining the vector does not free its buffer.
pub(crate) struct RetainedRows {
    bytes: u64,
    schemas: HashMap<usize, SchemaHold>,
}

struct SchemaHold {
    bytes: u64,
    rows: usize,
}

impl RetainedRows {
    pub(crate) fn measure(rows: &[Row]) -> Self {
        let mut schemas = HashMap::new();
        for row in rows {
            let hold = schemas
                .entry(row.schema_ptr() as usize)
                .or_insert(SchemaHold {
                    bytes: row.schema_bytes(),
                    rows: 0,
                });
            hold.rows += 1;
        }
        Self {
            bytes: row_buffer_bytes(rows),
            schemas,
        }
    }

    pub(crate) fn bytes(&self) -> u64 {
        self.bytes
    }

    /// `row` has moved out of the input vector.
    pub(crate) fn release_row(&mut self, row: &Row) {
        let ptr = row.schema_ptr() as usize;
        if let Some(hold) = self.schemas.get_mut(&ptr) {
            hold.rows = hold.rows.saturating_sub(1);
            if hold.rows == 0 {
                self.bytes = self.bytes.saturating_sub(hold.bytes);
            }
        }
        self.bytes = self.bytes.saturating_sub(row_payload(row));
    }
}

fn row_payload(row: &Row) -> u64 {
    buffer_bytes(row.values().len(), size_of::<Value>() as u64)
        + row.values().iter().map(owned_heap).sum::<u64>()
}

/// Same walk as [`row_buffer_bytes`] for rows that are not a contiguous slice.
pub(crate) fn row_refs_bytes<'a>(rows: impl IntoIterator<Item = &'a Row>) -> u64 {
    let rows: Vec<&Row> = rows.into_iter().collect();
    rows_accounted(rows.len(), rows.into_iter())
}

/// Sort-key auxiliary: the keyed vector plus each key's value buffer.
pub(crate) fn sort_key_columns<'a>(keys: impl IntoIterator<Item = &'a [Value]>) -> u64 {
    let keys: Vec<&[Value]> = keys.into_iter().collect();
    let stride = (size_of::<Vec<Value>>() + size_of::<usize>()) as u64;
    let mut bytes = buffer_bytes(keys.len(), stride);
    for key in keys {
        bytes += buffer_bytes(key.len(), size_of::<Value>() as u64);
        for value in key {
            bytes += owned_heap(value);
        }
    }
    bytes
}

/// One bound row's slot in an expand batch: the row plus its used-edge
/// set. The one spelling of the layout both the batch accounting
/// ([`binding_bytes`]) and the incremental meter ([`BindingMeter::push`])
/// charge โ€” they must move together with the pair's types (LABS-1116).
fn binding_stride() -> u64 {
    size_of::<(Row, UsedSet)>() as u64
}

/// One expand batch: the pair vector, the rows, and each used-edge set.
pub(crate) fn binding_bytes(rows: &[(Row, UsedSet)]) -> u64 {
    let stride = binding_stride();
    let mut bytes = buffer_bytes(rows.len(), stride);
    let mut schemas = HashSet::new();
    for (row, used) in rows {
        bytes += row_contents(row, &mut schemas).0;
        bytes += buffer_bytes(used.len(), size_of::<RelId>() as u64);
    }
    bytes
}

fn group_key_heap(key: &GroupKey) -> u64 {
    key.logical_bytes()
}

/// A `HashSet<GroupKey>`: its table plus each key's accounted heap.
pub(crate) fn group_key_set_bytes(keys: &HashSet<GroupKey>) -> u64 {
    let mut bytes = buffer_bytes(keys.len(), size_of::<GroupKey>() as u64);
    for key in keys {
        bytes += group_key_heap(key);
    }
    bytes
}

/// The word tier of a distinct set holding `len` words of one tag: the
/// stride-8 figure T1 and a T2 tag table share, so promotion is free.
pub(crate) fn word_set_bytes(len: usize) -> u64 {
    buffer_bytes(len, size_of::<u64>() as u64)
}

/// A distinct set's row-key table. The hot path adds
/// [`distinct_row_insert_delta`]; this walk is the oracle and the figure a
/// cloned set retains.
pub(crate) fn distinct_row_set_bytes(keys: &HashSet<Vec<GroupKey>>) -> u64 {
    let mut bytes = buffer_bytes(keys.len(), size_of::<Vec<GroupKey>>() as u64);
    for key in keys {
        bytes += buffer_bytes(key.len(), size_of::<GroupKey>() as u64);
        for part in key {
            bytes += group_key_heap(part);
        }
    }
    bytes
}

/// Live grouped aggregation: the map, the group keys, and the retained rows.
/// The hot path updates [`GroupMapMeter`]; this walk is the oracle.
#[cfg(test)]
pub(crate) fn group_map_bytes(groups: &IndexMap<Vec<GroupKey>, Vec<Row>>) -> u64 {
    let stride = (size_of::<Vec<GroupKey>>() + size_of::<Vec<Row>>()) as u64;
    let mut bytes = buffer_bytes(groups.len(), stride);
    for (key, rows) in groups {
        bytes += buffer_bytes(key.len(), size_of::<GroupKey>() as u64);
        for part in key {
            bytes += group_key_heap(part);
        }
        bytes += row_buffer_bytes(rows);
    }
    bytes
}

/// Scratch the statement created. Property values use [`owned_heap`], so a
/// string still owned by the graph is not billed again. The property map
/// and the redo log are charged for the slots they reserve, not for a
/// `BTreeMap` leaf or a catalog-sized record. Redo shares the map `Arc`;
/// the container is still this statement's, so it is charged without a
/// strong-count check.
pub(crate) fn scratch_created_bytes(
    nodes: &HashMap<NodeId, Node>,
    rels: &HashMap<RelId, Relationship>,
    created_nodes: &HashSet<NodeId>,
    created_rels: &HashSet<RelId>,
    out_adj: &HashMap<NodeId, Vec<RelId>>,
    in_adj: &HashMap<NodeId, Vec<RelId>>,
    redo_bytes: u64,
) -> u64 {
    let mut bytes = buffer_bytes(created_nodes.len(), size_of::<NodeId>() as u64);
    bytes += buffer_bytes(created_rels.len(), size_of::<RelId>() as u64);
    for id in created_nodes {
        if let Some(node) = nodes.get(id) {
            bytes += created_node_bytes(node);
        }
    }
    for id in created_rels {
        if let Some(rel) = rels.get(id) {
            bytes += created_rel_bytes(rel);
        }
    }
    bytes += adj_map_bytes(out_adj);
    bytes += adj_map_bytes(in_adj);
    bytes += redo_bytes;
    bytes
}

fn created_node_bytes(node: &Node) -> u64 {
    let mut bytes = OVERHEAD_MAP_ENTRY + size_of::<Node>() as u64;
    bytes += buffer_bytes(node.labels.len(), size_of::<String>() as u64);
    for label in node.labels.iter() {
        bytes += str_logical_bytes(label);
    }
    bytes += created_property_bytes(&node.properties);
    bytes
}

fn created_rel_bytes(rel: &Relationship) -> u64 {
    OVERHEAD_MAP_ENTRY
        + size_of::<Relationship>() as u64
        + str_logical_bytes(&rel.rel_type)
        + created_property_bytes(&rel.properties)
}

fn created_property_bytes(entries: &relate_core::PropertyMap) -> u64 {
    let stride = size_of::<(String, Value)>() as u64;
    let mut bytes = if entries.slot_capacity() == 0 {
        0
    } else {
        VEC_HEADER + entries.slot_capacity() as u64 * stride
    };
    for (key, value) in entries {
        bytes += str_logical_bytes(key);
        bytes += owned_heap(value);
    }
    bytes
}

/// Byte change of one buffer when its length moves from `old_len` to `new_len`.
pub(crate) fn buffer_growth(old_len: usize, new_len: usize, stride: u64) -> u64 {
    buffer_bytes(new_len, stride).saturating_sub(buffer_bytes(old_len, stride))
}

/// Running total for a binding batch. `push` adds one row; the total equals
/// [`binding_bytes`] of the same rows.
#[derive(Debug, Default)]
pub(crate) struct BindingMeter {
    pub bytes: u64,
    len: usize,
    schemas: HashSet<usize>,
}

/// Snapshot taken before a [`BindingMeter::push`], so a refused resize can
/// put the meter back.
pub(crate) struct BindingMark {
    bytes: u64,
    len: usize,
    schema: Option<usize>,
}

impl BindingMeter {
    pub(crate) fn push(&mut self, row: &Row, used_len: usize) -> BindingMark {
        let mark = BindingMark {
            bytes: self.bytes,
            len: self.len,
            schema: None,
        };
        let ptr = row.schema_ptr() as usize;
        let stride = binding_stride();
        let old = self.len;
        self.len += 1;
        self.bytes += buffer_growth(old, self.len, stride);
        let (contents, fresh_schema) = row_contents(row, &mut self.schemas);
        self.bytes += contents;
        self.bytes += buffer_bytes(used_len, size_of::<RelId>() as u64);
        BindingMark {
            schema: fresh_schema.then_some(ptr),
            ..mark
        }
    }

    pub(crate) fn unpush(&mut self, mark: BindingMark) {
        self.bytes = mark.bytes;
        self.len = mark.len;
        if let Some(ptr) = mark.schema {
            self.schemas.remove(&ptr);
        }
    }
}

#[derive(Debug, Default)]
struct GroupSlot {
    rows: usize,
    schemas: HashSet<usize>,
}

impl GroupSlot {
    fn add_row(&mut self, row: &Row) -> u64 {
        let old = self.rows;
        self.rows += 1;
        let mut delta = buffer_growth(old, self.rows, size_of::<Row>() as u64);
        if self.schemas.insert(row.schema_ptr() as usize) {
            delta += row.schema_bytes();
        }
        delta += buffer_bytes(row.values().len(), size_of::<Value>() as u64);
        for value in row.values() {
            delta += owned_heap(value);
        }
        delta
    }
}

/// Running total for an aggregation map. Adding a group or a row matches
/// [`group_map_bytes`] of the same map.
#[derive(Debug, Default)]
pub(crate) struct GroupMapMeter {
    pub bytes: u64,
    groups: Vec<GroupSlot>,
    payload: u64,
}

impl GroupMapMeter {
    fn stride() -> u64 {
        (size_of::<Vec<GroupKey>>() + size_of::<Vec<Row>>()) as u64
    }

    fn recompute(&mut self) {
        self.bytes = buffer_bytes(self.groups.len(), Self::stride()) + self.payload;
    }

    pub(crate) fn add_group(&mut self, key: &[GroupKey], row: &Row) {
        let mut slot = GroupSlot::default();
        let row_bytes = slot.add_row(row);
        self.groups.push(slot);
        self.payload += key_account(key) + row_bytes;
        self.recompute();
    }

    pub(crate) fn add_row(&mut self, index: usize, row: &Row) {
        self.payload += self.groups[index].add_row(row);
        self.recompute();
    }

    /// The keyless group aggregation inserts before any input row arrives.
    pub(crate) fn add_empty_group(&mut self) {
        self.groups.push(GroupSlot::default());
        self.recompute();
    }
}

fn key_account(key: &[GroupKey]) -> u64 {
    buffer_bytes(key.len(), size_of::<GroupKey>() as u64)
        + key.iter().map(GroupKey::logical_bytes).sum::<u64>()
}

/// The one group-map entry-landing charge: the map slot's buffer growth
/// (the slot holds the key vector's and the row vector's headers) plus
/// the key's slot buffer and owned payload โ€” the same figures
/// [`GroupMapMeter`]'s `key_account` counts for the workspace ledger,
/// spelled once for the expression-budget running total a new group
/// grows it by.
pub(crate) fn group_entry_insert_delta(old_len: usize, key: &[GroupKey]) -> u64 {
    buffer_growth(old_len, old_len + 1, GroupMapMeter::stride()) + key_account(key)
}

pub(crate) fn distinct_row_insert_delta(old_len: usize, key: &[GroupKey]) -> u64 {
    buffer_growth(old_len, old_len + 1, size_of::<Vec<GroupKey>>() as u64) + key_account(key)
}

/// The one distinct-entry charge policy, applied by
/// [`DistinctSet`](crate::distinct_set::DistinctSet) to each new key: the
/// delta is computed only while a ledger is installed, so an admission-off
/// run never walks a key for bytes (ADR-0507 ยง5); a zero delta charges
/// nothing; otherwise grow the meter and hold the peak. `delta` comes from
/// the representation that took the key ([`word_insert_delta`],
/// [`group_key_insert_delta`] or [`distinct_row_insert_delta`]).
pub(crate) fn charge_distinct_delta(
    charge: &mut relate_core::tokens::WorkspaceCharge,
    delta: impl FnOnce() -> u64,
) -> relate_core::Result<()> {
    if !relate_core::tokens::workspace_accounting() {
        return Ok(());
    }
    let delta = delta();
    if delta == 0 {
        return Ok(());
    }
    charge.resize(charge.bytes().saturating_add(delta))?;
    relate_core::tokens::peak_hold("distinct");
    Ok(())
}

pub(crate) fn group_key_insert_delta(old_len: usize, key: &GroupKey) -> u64 {
    buffer_growth(old_len, old_len + 1, size_of::<GroupKey>() as u64) + key.logical_bytes()
}

/// One new word in a distinct set's word tier ([`word_set_bytes`]).
pub(crate) fn word_insert_delta(old_len: usize) -> u64 {
    buffer_growth(old_len, old_len + 1, size_of::<u64>() as u64)
}

pub(crate) fn created_node_insert_delta(existing: usize, node: &Node) -> u64 {
    buffer_growth(existing, existing + 1, size_of::<NodeId>() as u64) + created_node_bytes(node)
}

pub(crate) fn created_node_remove_delta(remaining: usize, node: &Node) -> u64 {
    buffer_growth(remaining, remaining + 1, size_of::<NodeId>() as u64) + created_node_bytes(node)
}

pub(crate) fn created_rel_insert_delta(
    existing: usize,
    rel: &Relationship,
    out_len: Option<usize>,
    in_len: Option<usize>,
) -> u64 {
    buffer_growth(existing, existing + 1, size_of::<RelId>() as u64)
        + created_rel_bytes(rel)
        + adj_len_delta(out_len)
        + adj_len_delta(in_len)
}

/// Deleting a created relationship drops its record and id-set slot. The
/// adjacency lists keep the id, matching [`delete_rel`].
pub(crate) fn created_rel_remove_delta(remaining: usize, rel: &Relationship) -> u64 {
    buffer_growth(remaining, remaining + 1, size_of::<RelId>() as u64) + created_rel_bytes(rel)
}

fn adj_len_delta(existing: Option<usize>) -> u64 {
    match existing {
        None => OVERHEAD_MAP_ENTRY + buffer_bytes(1, size_of::<RelId>() as u64),
        Some(len) => buffer_growth(len, len + 1, size_of::<RelId>() as u64),
    }
}

pub(crate) fn created_property_delta(entries: &relate_core::PropertyMap) -> u64 {
    created_property_bytes(entries)
}

pub(crate) fn created_label_bytes(labels: &[String]) -> u64 {
    buffer_bytes(labels.len(), size_of::<String>() as u64)
        + labels
            .iter()
            .map(|label| str_logical_bytes(label))
            .sum::<u64>()
}

fn adj_map_bytes(adj: &HashMap<NodeId, Vec<RelId>>) -> u64 {
    let mut bytes = adj.len() as u64 * OVERHEAD_MAP_ENTRY;
    for rels in adj.values() {
        bytes += buffer_bytes(rels.len(), size_of::<RelId>() as u64);
    }
    bytes
}

#[cfg(test)]
mod tests;