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
//! Proof 5, second half: a long command file does not follow its own volume.
//!
//! The first half of the proof, a million rows read as a flow, is measured by
//! counting what the flow holds. A cut cannot be measured that way without
//! trusting a counter somebody has to remember to update, so this measures the
//! heap itself: every allocation the process makes while the file is being cut
//! is counted, and the count is read at the moments that matter.
//!
//! This file carries one test on purpose. The counter is global to the process,
//! so a second test running beside it would be measuring both.
use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering};
/// Bytes handed out and not yet given back.
static LIVE: AtomicUsize = AtomicUsize::new(0);
/// The system allocator, counting what it lends.
struct Counting;
// SAFETY: every call is forwarded to the system allocator unchanged, and the
// only addition is an atomic counter, which allocates nothing itself.
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let pointer = unsafe { System.alloc(layout) };
if !pointer.is_null() {
LIVE.fetch_add(layout.size(), Ordering::Relaxed);
}
pointer
}
unsafe fn dealloc(&self, pointer: *mut u8, layout: Layout) {
LIVE.fetch_sub(layout.size(), Ordering::Relaxed);
unsafe { System.dealloc(pointer, layout) };
}
unsafe fn realloc(&self, pointer: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
let moved = unsafe { System.realloc(pointer, layout, new_size) };
if !moved.is_null() {
LIVE.fetch_sub(layout.size(), Ordering::Relaxed);
LIVE.fetch_add(new_size, Ordering::Relaxed);
}
moved
}
}
#[global_allocator]
static ALLOCATOR: Counting = Counting;
/// Bytes the process is holding right now.
fn live() -> usize {
LIVE.load(Ordering::Relaxed)
}
/// A file of many statements, each one small, so that what grows is the count.
fn file_of(statements: usize) -> String {
let mut file = String::new();
for index in 0..statements {
use std::fmt::Write as _;
// Written straight into the file rather than through a second string
// built only to be copied in and dropped on every turn of the loop.
let _ = writeln!(file, "SELECT {index};");
}
file
}
/// FR-028. Every statement of the file is handed over in order and carries its
/// own position, from the first to the twenty thousandth, which is what a
/// result reported per command with its position rests on.
///
/// A long command file costs its longest statement, and never its whole self.
///
/// Two things are measured, because either one alone would pass a cut that is
/// still wrong.
///
/// The first is that what the walk holds *besides the statement in the
/// caller's hand* is the same at the twenty thousandth statement as at the
/// first. Asking whether the heap is simply equal at both ends would be the
/// wrong question and would fail on a correct walk: the statements are not all
/// the same length, so holding one of them is not always the same number of
/// bytes. Taking the statement's own length out is what leaves the property
/// the proof is about, and it is exact rather than approximate.
///
/// The second is that the walk holds far less than the same statements
/// gathered. Without it a cut that chopped the file up before handing the
/// first statement over would pass: such a cut is also perfectly flat, at the
/// cost of the whole file.
#[test]
fn a_long_command_file_is_cut_as_a_flow_and_never_held_whole() {
let statements = 20_000;
let file = file_of(statements);
let before = live();
let mut walk = mariadb_cli::exec::script::split(&file, ";").expect("the file is readable");
let first = walk.next().expect("a first statement");
// What the walk holds that is not the statement being looked at.
let besides_the_first = live() - before - first.sql.len();
assert_eq!(first.index, 1);
let mut seen = 1;
let mut widest = first.sql.len();
let mut last = first;
for piece in &mut walk {
seen += 1;
// The statement handed over before this one is dropped by the
// assignment, so exactly one statement is alive when the heap is read.
last = piece;
widest = widest.max(last.sql.len());
assert_eq!(
live() - before - last.sql.len(),
besides_the_first,
"the cut grew by statement {}",
last.index
);
}
drop(walk);
assert_eq!(seen, statements, "every statement is handed over");
assert_eq!(last.index, statements);
assert_eq!(last.sql, format!("SELECT {}", statements - 1));
// The peak is the file's blanked twin plus the longest single statement,
// which is what "costs its longest statement" means in bytes.
let peak = besides_the_first + widest;
assert!(
peak < file.len() + 1024,
"the walk peaked at {peak} bytes over a {} byte file, so it is holding more than \
the file's blanked twin and one statement",
file.len()
);
// The same statements gathered, which is what the cut used to do and what
// this walk must cost far less than.
let before_gathering = live();
let gathered: Vec<_> = mariadb_cli::exec::script::split(&file, ";")
.expect("the file is readable")
.collect();
let holding_them_all = live() - before_gathering;
assert_eq!(gathered.len(), statements);
drop(gathered);
assert!(
peak * 2 < holding_them_all,
"the walk holds {peak} bytes and the same statements gathered hold {holding_them_all}; \
a walk that costs what gathering costs is not a walk"
);
}