diff --git a/src/promql/src/native/execute.rs b/src/promql/src/native/execute.rs index 10036d47767..eee3e18b503 100644 --- a/src/promql/src/native/execute.rs +++ b/src/promql/src/native/execute.rs @@ -1240,11 +1240,20 @@ struct BinaryOperand { impl BinaryOperand { /// The bytes `chunk` would add to what this operand holds, read without retaining it. /// - /// It is what [`Self::enqueue`] adds for the same chunk: what the chunk holds on its own and the - /// room a chunk that closes its series needs in the boundary list. The figure is what a caller - /// reserves before it retains the chunk, so the pool admits it before the operand holds it. + /// It is what [`Self::enqueue`] adds for the same chunk: what the chunk holds on its own, and the + /// one entry a chunk that closes its series records in the boundary list. The boundary list holds + /// one `usize` per closed series and is charged one entry at a time as it is written, rather than + /// from the capacity it grows to -- following that `Vec`'s first-four-then-double policy would be + /// a second copy of it to keep in step, and one entry within a factor of two is the granularity + /// the accounting aims at. The figure is what a caller reserves before it retains the chunk, so + /// the pool admits it before the operand holds it. fn pending(&self, chunk: &EvaluatedChunk) -> usize { - operand_chunk_bytes(&chunk.series).saturating_add(self.ends_growth(chunk.is_last)) + let end = if chunk.is_last { + std::mem::size_of::() + } else { + 0 + }; + operand_chunk_bytes(&chunk.series).saturating_add(end) } /// Retains one chunk, folding the estimate it is charged with into what the operand holds. @@ -1254,7 +1263,11 @@ impl BinaryOperand { // in it: an end is charged for before it exists, the same figure `pending` reported. self.held = self .held - .saturating_add(self.ends_growth(closing)) + .saturating_add(if closing { + std::mem::size_of::() + } else { + 0 + }) .saturating_add(operand_chunk_bytes(&chunk.series)); self.series.push(chunk.series); if closing { @@ -1262,21 +1275,6 @@ impl BinaryOperand { } } - /// The bytes the boundary list grows by when a chunk that ends its series `closing` is recorded - /// in it. - /// - /// The list holds one `usize` per closed logical series and grows by doubling from a first - /// allocation of four entries, so a record costs nothing while the capacity it has holds it and - /// otherwise costs the new capacity. - fn ends_growth(&self, closing: bool) -> usize { - let needed = self.ends.len() + 1; - if !closing || needed <= self.ends.capacity() { - return 0; - } - let capacity = (2 * self.ends.capacity()).max(4).max(needed); - (capacity - self.ends.capacity()) * std::mem::size_of::() - } - /// The chunk ranges of the logical series this operand closed, in arrival order. /// /// Read after the input ended: a series that is still open has no end, and @@ -1289,38 +1287,35 @@ impl BinaryOperand { }) } - /// The bytes assembling this operand's chunks into logical series allocates, before it does. + /// The bytes assembling this operand's chunks into logical series allocate, before they do. /// /// One series per closed logical series, on `schema`'s grid, and nothing that copies a sample: /// the descriptors the chunks' fields are made of are collected and shared. What is counted is - /// therefore metadata only, and every term is what the work holds at its peak: the entry in the - /// assembled vector, the field list of the series ([`SeriesFields`] takes a one-field list out of - /// its vector and boxes a wider one, so neither copies), and the exactly sized vector - /// [`Self::assemble`] collects a field's chunk descriptors into. + /// therefore metadata only, and it is a rough figure per field, per segment and per run rather + /// than a model of the containers the work builds: /// - /// A field's descriptors then go through [`Samples::try_from_chunks`], which copies them into a - /// vector that grows by doubling -- so it ends at no more than twice the segments it holds, and - /// starts from the four slots such a `Vec` allocates first -- and then into the `Arc<[Samples]>` - /// it returns, which holds one slot per segment while that vector is still alive. Three slots - /// per segment and the four-slot floor cover the two together, and the floor is also what pays - /// for the empty result an all-empty field is replaced with. + /// - per series, the assembled entry and the field list ([`SeriesFields`] takes a one-field list + /// out of its vector and boxes a wider one, so neither copies); + /// - per chunk and field, the descriptor collected out of it; + /// - per segment, three descriptors: the one the flatten copies out, the slot the `Arc<[Samples]>` + /// it is copied into holds, and the doubling of the vector it is copied out of; + /// - per run the boundary covers, the span [`Self::assemble`] collects and the span and + /// row-offset arrays [`TimeAxis::from_grid_spans`] copies them into. /// - /// The axis adds the runs the boundary covers, collected into an exactly sized vector, and, from - /// two runs on, the span and row-offset arrays [`TimeAxis::from_grid_spans`] copies them into - /// while the vectors they came from are alive; a single run becomes a [`GridRange`] and - /// allocates neither. + /// No term follows how a `Vec` happens to grow -- there is no branch on a capacity here and no + /// first-allocation floor -- so a change to the flatten's growth policy cannot make this figure + /// wrong. What it is instead is a rough per-field, per-segment and per-run estimate, within a + /// small factor of what the assembly holds at its peak, which is the granularity a reservation + /// is made at. fn assembly_bytes(&self, schema: &SeriesSchema) -> Option { let samples = std::mem::size_of::(); let grids = std::mem::size_of::(); let width = schema.fields.len(); - // The slots a first push allocates, which is the floor every growth of the flatten starts - // from: `Vec` grows a non-zero capacity to at least this many elements. - let vec_floor = 4usize; let mut bytes = 0usize; for boundary in self.boundaries() { let chunks = &self.series[boundary]; - // The runs the boundary covers, and the dense segments its fields hold: a chunk's field - // can itself be chunked, and flattening it copies one descriptor per segment. + // The runs the boundary covers, and the segments its fields hold: a chunk's field can + // itself be chunked, and flattening it copies one descriptor per segment. let mut runs = 0usize; let mut segments = 0usize; for chunk in chunks { @@ -1329,25 +1324,25 @@ impl BinaryOperand { segments = segments.checked_add(field.segments().len())?; } } - // Per field: the chunks collected into one vector, and the three slots per segment plus - // the four-slot floor that the flatten and the `Arc` it is copied into need together. + // The descriptors of a field's chunks, the flatten's descriptors and the `Arc` they are + // copied into; a field with no segment still builds an empty `Samples`, which is one + // descriptor. let descriptors = chunks .len() .checked_mul(width)? - .checked_add(segments.checked_mul(3)?)? - .checked_add(width.checked_mul(vec_floor)?)?; - let collected = runs.checked_mul(grids)?; - let ranges = if runs > 1 { - runs.checked_mul(grids.checked_add(std::mem::size_of::())?)? - } else { - 0 - }; + .checked_add(segments.checked_mul(3)?.checked_add(width)?)?; + // The axis: the spans collected, and the span and offset arrays a run layout copies + // them into. + let axis = runs.checked_mul( + grids + .checked_mul(2)? + .checked_add(std::mem::size_of::())?, + )?; bytes = bytes .checked_add(std::mem::size_of::())? .checked_add(width.checked_mul(samples)?)? .checked_add(descriptors.checked_mul(samples)?)? - .checked_add(collected)? - .checked_add(ranges)?; + .checked_add(axis)?; } Some(bytes) } @@ -1581,10 +1576,12 @@ impl BinaryRuntime { /// otherwise be a candidate per chunk on its own side and be paired with every candidate of the /// other, which is quadratic in the chunk count of one series rather than in the series count. /// - /// The kernel requests a conservative live-workspace budget before allocating its matching - /// tables, per-point presence arrays and results. The reservation covers the retained chunks, - /// the assembled series the kernel reads and that workspace, and resizes as temporary storage is - /// released. It keeps covering the returned results until the caller queues them. + /// The reservation covers the retained chunks, the assembled series the kernel reads and the + /// kernel's own live workspace, and it is what the pool admits before each of them allocates. + /// The kernel reports that workspace coarsely -- where a long-lived result buffer grows, once per + /// phase of its walk, and in the blocks a growing output buffer is reserved in -- rather than per + /// grid point, and the total is resized as a phase's storage is released. It keeps covering the + /// returned results until the caller queues them. fn compute(&mut self, path: &NodePath, buffer: &mut NodeBuffer) -> Result>> { // The kernel reads the operands once and drops them, so a second call has nothing to read // and nothing left to produce: what it produced the first time is in the node's queue, and diff --git a/src/promql/src/native/execute/tests.rs b/src/promql/src/native/execute/tests.rs index 818fadea4ff..b87794b79e1 100644 --- a/src/promql/src/native/execute/tests.rs +++ b/src/promql/src/native/execute/tests.rs @@ -4794,10 +4794,10 @@ fn a_binary_operand_reserves_what_it_retains() { let second = evaluated_array_chunk_of(&schema, metric(), 20_000, batch.slice(20_000, 20_000), true); - // What each chunk holds on its own, and the room the closing chunk's end takes in the operand's - // boundary list -- the list is storage the operand keeps, and its first end allocates room for - // four. - let boundaries = 4 * std::mem::size_of::(); + // What each chunk holds on its own, and the entry the closing chunk records in the operand's + // boundary list -- the list is storage the operand keeps, and it is charged one entry per closed + // series as it is written. + let boundaries = std::mem::size_of::(); let (first_alone, second_alone) = ( operand_chunk_bytes(&first.series), operand_chunk_bytes(&second.series), @@ -4836,8 +4836,7 @@ fn a_binary_operand_reserves_what_it_retains() { assert_eq!(pool.reserved(), held); // The figure a caller reserves for before retaining a chunk is the one the operand grows by: the - // estimate of the series it carries. The boundary list already has room for its end, so nothing - // of that is added. + // estimate of the series it carries and the one entry the chunk's end records. let own = evaluated_chunk(&schema, 0, vec![1.0, 2.0], true); assert_eq!( runtime @@ -4846,7 +4845,7 @@ fn a_binary_operand_reserves_what_it_retains() { .unwrap() .pending(&NodePath::root(), 0, &own) .unwrap(), - operand_chunk_bytes(&own.series), + operand_chunk_bytes(&own.series) + std::mem::size_of::(), "the figure reserved for a chunk is not what the operand grows by" ); @@ -4897,8 +4896,8 @@ fn a_binary_beneath_the_executor_reserves_what_it_retains() { let second = evaluated_array_chunk_of(&schema, metric(), 2_000, batch.slice(2_000, 2_000), true); // What the left operand holds once both chunks are retained: what each chunk holds on its own, - // and the boundary list the closing chunk adds its end to. - let retained = 4 * std::mem::size_of::() + // and the boundary list the closing chunk adds one entry to. + let retained = std::mem::size_of::() + [&first, &second] .iter() .map(|chunk| operand_chunk_bytes(&chunk.series)) diff --git a/src/promql/src/vector_match.rs b/src/promql/src/vector_match.rs index 3cd4a5d81a9..83e50a71df2 100644 --- a/src/promql/src/vector_match.rs +++ b/src/promql/src/vector_match.rs @@ -26,7 +26,9 @@ use std::sync::Arc; use datafusion::error::{DataFusionError, Result}; use datatypes::series::histogram::{CounterResetHint, HistogramBatchBuilder, HistogramView}; -use datatypes::series::samples::{FieldSpec, RowCursor, SampleRef, Series, SeriesSchema}; +use datatypes::series::samples::{ + DenseSamplesRef, FieldSpec, RowCursor, SampleRef, Series, SeriesSchema, +}; use datatypes::series::time::{EvalGrid, GridRunPair, GridSpan, TimeAxis}; use datatypes::series::{Label, MetricName}; @@ -202,10 +204,10 @@ pub fn binary_vectors( /// What the operation will allocate cannot be known before it allocates: which series match each /// other, where their axes overlap and which rows the operator keeps all come out of the operands /// as they are read. So `reserve` receives the **total** live workspace the operation holds, and it -/// is asked whenever that total changes: it grows as rows are kept and falls as a step's scratch -- -/// one field's tables, one pairing's label set, an owned copy of a histogram -- is replaced or -/// dropped. A total `reserve` refuses ends the operation with that error, and what it admitted stays -/// the caller's to release. +/// is asked at the three granularities [`Budget`] describes: where a long-lived result buffer grows, +/// once per phase, and in the blocks a growing result buffer is reserved in. It is never asked per +/// grid point or per row. A total `reserve` refuses ends the operation with that error, and what it +/// admitted stays the caller's to release. pub(crate) fn binary_vectors_with_reservation( matching: &BinaryMatching, lhs_schema: &SeriesSchema, @@ -242,7 +244,7 @@ fn binary_vectors_inner( // The schema is what these bytes cover, so they are reserved before it is built. let width = lhs_schema.fields.len().min(rhs_schema.fields.len()); if let Some(budget) = budget.as_deref_mut() { - budget.retain(output_bytes(width))?; + budget.hold(output_bytes(width))?; } let output = binary_output_schema(lhs_schema, rhs_schema)?; let mut errors = Vec::new(); @@ -258,30 +260,16 @@ fn binary_vectors_inner( &mut errors, budget.as_deref_mut(), )?; - // The field's tables and sort are destroyed with its frame, so their release is admitted - // here: one figure for what the field kept -- its messages and rows -- once they are gone. + // The field's flags, tables and sort are destroyed with its frame, so its phase ends here: + // what the field filed is what stays live, and the next field's phase is charged on its own. if let Some(budget) = budget.as_deref_mut() { - budget.drop_scratch()?; + budget.phase(0)?; } fields.push(results); } earliest_error(errors)?; - // The merge holds every field's results at once: the triples it files, the table it files them - // in and the builder writing the series out are the last workspace of the operation. - if let Some(budget) = budget.as_deref_mut() { - let sets = fields - .iter() - .map(|results| results.series.len()) - .fold(0usize, usize::saturating_add); - let merge = merge_bytes( - budget.kept, - sets, - width, - budget.histograms, - budget.histogram_heap, - ); - budget.step(merge)?; - } + // The merge is the operation's last phase: it enters its own figure from the results the fields + // filed, which is where every term of it is read. merge_fields(fields, output, budget) } @@ -304,13 +292,15 @@ const MATCH_SLOT_BYTES: usize = 2 * std::mem::size_of::<(usize, usize)>(); /// One entry of a field's result table, charged the same way. const RESULT_SLOT_BYTES: usize = 2 * std::mem::size_of::<(GroupKey, ResultRows)>(); -/// One row of a field's results: its point, its match group and its value, charged twice for the -/// capacity each of the three vectors ends at. +/// One row of a field's results: its point, its match group and its value. The three vectors grow +/// together, so one slot of each is what a row takes, and a block of the vectors is charged a row's +/// bytes per slot it grew by. const RESULT_ROW_BYTES: usize = - 2 * (2 * std::mem::size_of::() + std::mem::size_of::>()); + 2 * std::mem::size_of::() + std::mem::size_of::>(); /// One row while a label set is sorted: the old and the new copy of its point, group and value -/// vectors, each charged twice for the capacity it ends at, and the row order the sort reads. +/// vectors, and the row order the sort reads. The sort copies a label set's rows when they are not +/// already in order, so this is what one row costs while it runs. const SORT_ROW_BYTES: usize = 4 * std::mem::size_of::() + 2 * std::mem::size_of::>(); @@ -335,91 +325,62 @@ fn output_bytes(fields: usize) -> usize { .saturating_add(std::mem::size_of::()) } -/// The live workspace a binary operation admits as it allocates. +/// The live workspace a binary operation admits as it allocates, at three granularities. /// -/// What the results keep -- rows, the label sets they are filed under, their histograms -- stays -/// live until the operation returns, while the scratch of a step is what one step holds beside it. -/// [`Budget::step`] replaces that scratch instead of adding to it, so the total handed to the caller -/// is what is retained plus the step being run: it rises when a wider step begins and falls when a -/// narrower one replaces it, and it is never the sum of every step the operation has run. +/// A resource limit belongs at an allocation boundary, not at a value: +/// +/// - [`Budget::hold`]: what the results keep until the operation returns -- rows, the label sets they +/// are filed under, the values boxed along them, the messages an error reports. Each is charged +/// where its container grows. +/// - [`Budget::phase`]: one figure for the workspace of one walk, read from the operands before it +/// allocates and replacing the phase before it, so the total is the phase being run beside what is +/// held and never the sum of every phase. +/// - [`Budget::grow`]: a block a container of that phase took when it outgrew what it held. +/// +/// It exists because the caller is handed a *total*, and this turns those figures into that total. struct Budget<'a> { /// Admits the live workspace, or refuses it. reserve: &'a mut dyn FnMut(usize) -> Result<()>, /// The bytes the results keep until the operation returns. - retained: usize, - /// The bytes of the step being run, beside what is retained. - scratch: usize, + held: usize, + /// The bytes of the phase being run, beside what is held. + phase: usize, /// The total handed to `reserve` last, so an unchanged one is not asked for twice. requested: Option, - /// The result rows filed, rows the operator filtered included: what the sort walks. - rows: usize, - /// The result rows holding a value: what the merge files as triples and writes out. - kept: usize, - /// The retained rows holding a histogram, and the heap those histograms hold. - histograms: usize, - histogram_heap: usize, } impl<'a> Budget<'a> { fn new(reserve: &'a mut dyn FnMut(usize) -> Result<()>) -> Self { Self { reserve, - retained: 0, - scratch: 0, + held: 0, + phase: 0, requested: None, - rows: 0, - kept: 0, - histograms: 0, - histogram_heap: 0, } } - /// Retains `bytes` of results and admits the total covering them. - fn retain(&mut self, bytes: usize) -> Result<()> { - self.retained = self.retained.saturating_add(bytes); + /// Retains `bytes` of results, which stay live until the operation returns. + fn hold(&mut self, bytes: usize) -> Result<()> { + self.held = self.held.saturating_add(bytes); self.admit() } - /// Admits `bytes` as the whole workspace of the step being run, replacing the step before it. - fn step(&mut self, bytes: usize) -> Result<()> { - self.scratch = bytes; + /// Admits `bytes` as the whole workspace of the phase being run, replacing the phase before it. + fn phase(&mut self, bytes: usize) -> Result<()> { + self.phase = bytes; self.admit() } - /// Forgets the last step's scratch: its tables and buffers are gone. - fn drop_scratch(&mut self) -> Result<()> { - self.scratch = 0; + /// Adds `bytes` to the phase for a container of it that has outgrown what was reserved. + fn grow(&mut self, bytes: usize) -> Result<()> { + self.phase = self.phase.saturating_add(bytes); self.admit() } - /// Adds `bytes` to what the results retain without admitting it, for a figure the next - /// [`Budget::step`] admits as one total. - fn keep(&mut self, bytes: usize) { - self.retained = self.retained.saturating_add(bytes); - } - - /// Moves one kept histogram into what the results retain: the value it boxes and the heap its - /// buckets and bounds hold. The [`Budget::step`] that follows admits the transfer together with - /// the released operator temporary, so the figure never holds the histogram twice. - fn keep_histogram(&mut self, histogram: &FloatHistogram) { - self.histograms = self.histograms.saturating_add(1); - self.histogram_heap = self.histogram_heap.saturating_add(histogram.heap_bytes()); - self.keep(result_histogram_bytes(histogram)); - } - - /// Counts one filed row: every row is what the sort walks, a row holding a value is what the - /// merge files and writes out. - fn file_row(&mut self, kept: bool) { - self.rows = self.rows.saturating_add(1); - if kept { - self.kept = self.kept.saturating_add(1); - } - } - /// Hands the live total to `reserve` when it differs from the last one. It falls as well as /// rises, and only a repeated figure is left out. fn admit(&mut self) -> Result<()> { - let total = self.retained.saturating_add(self.scratch); + let total = self.held.saturating_add(self.phase); if self.requested != Some(total) { self.requested = Some(total); (self.reserve)(total)?; @@ -428,29 +389,29 @@ impl<'a> Budget<'a> { } } -/// What one field's tables, flags and messages cost before it pairs anything. +/// The figures one field's phases are charged from, read once from its operands. #[derive(Default)] struct FieldMetadata { - /// The presence flags and the one side's match groups: what the field holds beside the pairing - /// it walks, and the figure a step of that walk is charged on top of. + /// The presence flags and the one side's match groups. tables: usize, - /// The allowance for the messages the field can report, which it reserves for its sort before - /// it knows which of them it appends; only the ones it writes are kept. + /// The allowance for the messages the field can report, which its sort reserves before it knows + /// which of them it appends; only the ones it writes are kept. errors: usize, /// The largest key a row of the field is filed under. largest_key: usize, + /// The widest pairing's label set and the histogram workspace one row of the walk reads. + pairing: usize, } -/// The bytes one field's tables, flags and messages cost. +/// The figures one field's phases are charged from. /// /// The presence of each side and of the points both are present at are three flags per grid point; /// the one side's groups are a key, the values `on(...)` matching copies and a member list per -/// series with the table holding them and the references indexing them; one series of the many side -/// is looked up by a key of its own; and a field reports at most three messages, each a fixed frame -/// around one rendering. Nothing here counts the pairing the field walks: which series match, where -/// their axes overlap and which rows the operator keeps is only known as the walk reaches them. -/// The duplicate scan's list of present points is not part of these tables either: a group of one -/// member collects nothing, and a wider group's list is charged by [`overlap`] as it is pushed to. +/// series; one series of the many side is looked up by a key of its own; and a field reports at most +/// three messages, each a fixed frame around one rendering. The duplicate scan's list is not part of +/// these tables: a group of one member collects nothing, and a wider group's list is charged by +/// [`overlap`] as it grows. The pairing's own terms are read from the widest series of each side +/// rather than from every pair, since one pairing's label set replaces the one before it. fn field_metadata_bytes( matching: &BinaryMatching, include: &[Arc], @@ -486,26 +447,27 @@ fn field_metadata_bytes( .fold(include_text, |bytes, series| { bytes.max(label_text_bytes(&series.metric).saturating_add(include_text)) }); + // The widest series of each side: one pairing's label set replaces the one before it, so the + // widest is what the phase holds at its peak. + let pairing = widest_metric(many) + .zip(widest_metric(one)) + .map_or(0, |(many, one)| pair_bytes(many, one, matching, include)); FieldMetadata { tables: bytes.saturating_add(lookup), errors: 3usize.saturating_mul(ERROR_TEXT_BYTES.saturating_add(text)), largest_key: largest_key.saturating_add(include.len().saturating_mul(KEY_LABEL_BYTES)), + pairing: pairing.saturating_add(histogram_phase_bytes(many, one)), } } -/// The workspace one row of a pairing holds: the field's tables, the match slots it holds, the -/// pairing's label set and -- while the operator reads one -- its histogram temporaries. -fn row_workspace( - metadata: &FieldMetadata, - matched: usize, - pairing: usize, - histograms: usize, -) -> usize { - metadata - .tables - .saturating_add(matched) - .saturating_add(pairing) - .saturating_add(histograms) +/// The series of `operand` with the most labels: the widest label set one pairing of its field can +/// build. +fn widest_metric<'a>(operand: &'a Operand<'_>) -> Option<&'a MetricName> { + operand + .series + .iter() + .map(|series| &series.metric) + .max_by_key(|metric| metric.tags().len()) } /// The bytes one pairing allocates before it files a row: the result label set `result_metric` @@ -597,8 +559,7 @@ fn result_set_bytes(metric: &MetricName, one: &MetricName, include: &[Arc]) /// row of the histogram table per histogram and the heap its buckets are copied to. Every term counts /// a value a field actually kept or a label set it filed, so a pair the operator filtered costs its /// label set -- the table holds one without a value -- and no row at all. What grows with the output -/// rows is charged by [`merge_fields`] as they are pushed: the value and validity column of every -/// field, and the index column of a field whose first histogram backfills the rows before it. +/// rows is the builder's columns, which [`merge_fields`] reserves in blocks as it pushes them. fn merge_bytes( kept: usize, sets: usize, @@ -616,10 +577,8 @@ fn merge_bytes( .saturating_add( sets.saturating_mul(MERGE_SLOT_BYTES.saturating_add(2 * std::mem::size_of::())), ) - // The row the builder reads its pushes from and the per-field ledger of the capacity each - // histogram index column has reached, one slot per field. + // The row the builder reads its pushes from. .saturating_add(2 * fields.saturating_mul(std::mem::size_of::>())) - .saturating_add(fields.saturating_mul(std::mem::size_of::())) // The histogram table of every field that carries one: the builders of its columns, the // scalars and list offsets of its rows, and the buckets the batch copies beside the value // the results still hold. @@ -628,16 +587,6 @@ fn merge_bytes( .saturating_add(2 * histogram_heap) } -/// The bytes one output row of the merge adds to the builder's value columns: the value and the -/// validity bit of every field of the schema, charged twice for the capacity each column grows to. -/// -/// A row holds a value only in the fields the results filed at its point, and the builder writes a -/// null into every other column, so this figure counts the schema's fields rather than the row's -/// values: fields whose samples sit at different positions still write a column each. -fn merge_row_bytes(fields: usize) -> usize { - 2 * fields.saturating_mul(std::mem::size_of::() + 1) -} - /// The bytes one match key holds: its label pairs and, for `on(...)`, a copy of each value. /// /// `group_key` copies an `on(...)` value with `Arc::from` and shares the `Arc` of an @@ -678,7 +627,7 @@ fn label_text_bytes(metric: &MetricName) -> usize { }) } -/// The bytes one owned copy of a histogram operand holds: its scalars inline, and on the heap the +/// The bytes one owned copy of a histogram view holds: its scalars inline, and on the heap the /// buckets of both sides, the span layout of the view and its custom bounds. fn owned_histogram_bytes(view: &HistogramView<'_>) -> usize { let sides = [view.positive, view.negative]; @@ -714,31 +663,26 @@ fn result_histogram_bytes(histogram: &FloatHistogram) -> usize { std::mem::size_of::().saturating_add(histogram.heap_bytes()) } -/// The bytes `elem_binop` allocates to read one histogram pairing before it can hand back a value. -/// -/// The operator copies both operands into owned [`FloatHistogram`]s that stay alive while the -/// result is built from them: their buckets, the span layout of the view each was read from and -/// their custom bounds. `+` and `-` then insert every bucket index of the right side into the left -/// copy, so the result holds the union of both sides' buckets -- subtracting disjoint buckets adds -/// the right side's -- in a list charged twice because it reallocates while the old buffer is still -/// alive, and a schema mismatch reduces a copy of each side before merging. The value the operator -/// boxes is charged as well. This is one row's scratch: a row the operator drops leaves only this -/// behind, and the next row replaces it. -fn histogram_pair_bytes(lhs: &SampleRef<'_>, rhs: &SampleRef<'_>) -> usize { - // The owned copy of one operand, its buckets and its custom bounds; a float has none of them. - let operand = |sample: &SampleRef<'_>| match sample { - SampleRef::Histogram(view) => ( - owned_histogram_bytes(view), +/// The bytes one row of a histogram operator holds at its peak, read once from the widest histogram +/// of each side of a field: an owned copy of each operand, the union `+` and `-` grow into -- both +/// sides' buckets, charged twice because the union reallocates while the old buffer is alive -- the +/// custom bounds the wider side copies, and the value the operator boxes. A phase figure, not one per +/// row: a row that pairs two histograms is bounded by the widest of each side. +fn histogram_phase_bytes(many: &Operand<'_>, one: &Operand<'_>) -> usize { + // What one owned copy of a view takes, the buckets the union grows by and its custom bounds. + let operand = |operand: &Operand<'_>| match operand.widest_histogram() { + Some(view) => ( + owned_histogram_bytes(&view), view.positive .buckets .len() .saturating_add(view.negative.buckets.len()), view.custom_values.len(), ), - _ => (0, 0, 0), + None => (0, 0, 0), }; - let (lhs_owned, lhs_buckets, lhs_bounds) = operand(lhs); - let (rhs_owned, rhs_buckets, rhs_bounds) = operand(rhs); + let (lhs_owned, lhs_buckets, lhs_bounds) = operand(many); + let (rhs_owned, rhs_buckets, rhs_bounds) = operand(one); if lhs_owned == 0 && rhs_owned == 0 { return 0; } @@ -778,11 +722,13 @@ fn binary_field( }; let points = grid.timestamps().len(); let errors_before = errors.len(); - // This field's tables, before it pairs anything: nothing of the field before is still live. + // The pairing phase is entered once, before the field allocates anything: its presence flags, + // its match groups, the widest label set a pairing builds and the histogram payload one row of + // the walk reads. Nothing of the field before it is still live. let metadata = match budget.as_deref_mut() { Some(budget) => { let metadata = field_metadata_bytes(matching, include, points, many, one); - budget.step(metadata.tables)?; + budget.phase(metadata.tables.saturating_add(metadata.pairing))?; metadata } None => FieldMetadata::default(), @@ -801,13 +747,14 @@ fn binary_field( let one_groups = group(one, &matching.labels); let mut one_duplicate: Option<(usize, &GroupKey)> = None; for (key, members) in &one_groups { - // The group's list of present points is charged as this scan grows it, beside the tables - // the field keeps from here on. + // Scanning one group is its own phase: the list of its present points is the only thing it + // allocates, it is charged by the block it takes as it grows, and it is dropped with the + // scan, so the next group's list replaces it. let duplicate = overlap( one, members, |point| evaluated[point], - metadata.tables, + metadata.tables.saturating_add(metadata.pairing), budget.as_deref_mut(), )?; if let Some(point) = duplicate @@ -818,7 +765,6 @@ fn binary_field( } let drop_name = !matching.op.is_comparison() || matching.return_bool; - let rows_before = budget.as_deref().map_or(0, |budget| budget.rows); let mut pairs = Pairs { matching, one_is_left, @@ -830,8 +776,6 @@ fn binary_field( // consults the same set. matched: (matching.cardinality == Cardinality::OneToOne || fill.lhs.is_some()) .then(HashSet::new), - // The slots the match set holds are part of this field's workspace, not of what a row keeps. - matched_bytes: 0, implicit_many_to_one: None, }; for (many_index, many_series) in many.series.iter().enumerate() { @@ -847,18 +791,9 @@ fn binary_field( // pairing's label set and axis intersection are built. #[cfg(test)] VISITED_PAIRS.with(|visited| visited.set(visited.get() + 1)); - // One pairing's label set is built before the axes are intersected, so it is reserved - // first and the next pairing replaces it. - let mut pairing = 0usize; - if let Some(budget) = budget.as_deref_mut() { - pairing = pair_bytes( - &many_series.metric, - &one.series[one_index].metric, - matching, - include, - ); - budget.step(row_workspace(&metadata, pairs.matched_bytes, pairing, 0))?; - } + // One pairing's label set is built before the axes are intersected, and the next + // pairing replaces it: its bytes are part of the phase entered above, not a figure + // reserved per pair. let metric = result_metric( &many_series.metric, &one.series[one_index].metric, @@ -896,8 +831,6 @@ fn binary_field( point, many_value, one_value, - &metadata, - pairing, label_set, budget.as_deref_mut(), )?; @@ -936,8 +869,6 @@ fn binary_field( point, sample, SampleRef::Float(value), - &metadata, - 0, label_set, budget.as_deref_mut(), )?; @@ -972,15 +903,13 @@ fn binary_field( { let sample = cursor.at(row).expect("a row never goes back"); // A fill row pairs no two series, so it builds no pairing label set: what it - // reserves is the workspace of one row and the label set it is filed under. + // keeps is the label set it is filed under. pairs.apply( group, &metric, point, SampleRef::Float(value), sample, - &metadata, - 0, label_set, budget.as_deref_mut(), )?; @@ -988,24 +917,23 @@ fn binary_field( } } } - // The match set is a step of the walk, not something the field keeps: taking the pairing state - // apart here drops it, so the sort and the messages are what is live from here on. + // The match set, the groups and the pairing state are gone with the walk: taking them apart + // here drops them, so the sort and the messages are what is live from here on. let Pairs { mut results, group_keys, implicit_many_to_one, .. } = pairs; - // The messages the field reports and the sort of its rows are its last workspace, with the - // messages it may append charged before they exist. + // The sort phase: one figure for the tables still live beside it, the messages it may append + // and one slot per row it sorts -- entered once, before it allocates any of them. if let Some(budget) = budget.as_deref_mut() { - let rows = budget.rows.saturating_sub(rows_before); - budget.step( + budget.phase( metadata .tables .saturating_add(metadata.errors) .saturating_add(metadata.largest_key) - .saturating_add(rows.saturating_mul(SORT_ROW_BYTES)), + .saturating_add(results.rows.saturating_mul(SORT_ROW_BYTES)), )?; } // Prometheus fails at the first failing step. Within a step it checks the one side, then @@ -1059,8 +987,7 @@ fn binary_field( }); } // Only the messages this field actually appended stay live until the earliest one is reported. - // They join what the results retain, and the caller admits them once the field's tables and - // sort -- still live here -- are gone. + // They join what the results retain, once the field's tables and sort are gone. if let Some(budget) = budget { let appended = errors[errors_before..] .iter() @@ -1069,7 +996,7 @@ fn binary_field( .saturating_add(std::mem::size_of::()) .saturating_add(text.capacity()) }); - budget.keep(appended); + budget.hold(appended)?; } Ok(results) } @@ -1085,9 +1012,6 @@ struct Pairs<'a> { /// one-to-one matching, and filling the many side. A filtered comparison counts as a match, /// an operation that failed does not. matched: Option>, - /// The bytes the match set holds: its slots are part of the field's workspace, not of what a - /// row keeps, so they are charged beside the tables and given back with the set. - matched_bytes: usize, implicit_many_to_one: Option<(usize, usize)>, } @@ -1105,9 +1029,10 @@ impl Pairs<'_> { /// Applies the operator to a sample of each side at `point`; a missing sample is no pair. /// - /// `metadata` is what the field keeps between rows, `pairing` the label set and axis - /// intersection this pairing built, and `label_set` the bytes the result label set keeps: the - /// workspace is reserved through `budget` as each step of the row replaces the one before. + /// Nothing here is charged per point: the operator's histograms are part of the phase, and the + /// only figures a row can charge are the two allocation boundaries it crosses -- a block the match + /// set grows into, and `label_set`, the bytes a result label set keeps when this row is the first + /// filed under it, or the value the results retain. #[allow(clippy::too_many_arguments)] fn apply( &mut self, @@ -1116,8 +1041,6 @@ impl Pairs<'_> { point: usize, many_value: SampleRef<'_>, one_value: SampleRef<'_>, - metadata: &FieldMetadata, - pairing: usize, label_set: usize, mut budget: Option<&mut Budget<'_>>, ) -> Result<()> { @@ -1129,56 +1052,37 @@ impl Pairs<'_> { } else { (many_value, one_value) }; - // The operator's temporaries: an owned copy of each histogram operand and the union it - // grows into, live while the row it reads is computed. - if let Some(budget) = budget.as_deref_mut() { - let histograms = histogram_pair_bytes(&lhs_value, &rhs_value); - budget.step(row_workspace( - metadata, - self.matched_bytes, - pairing, - histograms, - ))?; - } let Some((result, keep)) = elem_binop(self.matching.op, lhs_value, rhs_value)? else { - // The operator skipped the pair: its temporaries are gone and nothing of it was filed. - if let Some(budget) = budget.as_deref_mut() { - budget.step(row_workspace(metadata, self.matched_bytes, pairing, 0))?; - } + // The operator skipped the pair and nothing of it was filed. return Ok(()); }; - // The operator has returned, so the copies it read its operands into are gone and what it - // still owns -- possibly on the heap -- is the value it handed back. That value is live - // until it is dropped or filed, so the figure it costs replaces the operator's temporary and - // is held across the steps below. - let owned = match &result { - GridSample::Histogram(histogram) => result_histogram_bytes(histogram), - GridSample::Float(_) => 0, - }; - if let Some(budget) = budget.as_deref_mut() { - budget.step(row_workspace(metadata, self.matched_bytes, pairing, owned))?; - } // One-to-one: a match group may be matched once per point; Prometheus counts a filtered // comparison as a match too, but not an operation that failed. if let Some(matched) = &mut self.matched { - if !matched.insert((group, point)) { - if self.matching.cardinality == Cardinality::OneToOne - && self - .implicit_many_to_one - .is_none_or(|(first, _)| point < first) - { - self.implicit_many_to_one = Some((point, group)); - } - } else if let Some(budget) = budget.as_deref_mut() { - // Only a point the group has not been matched at yet costs an entry, charged with - // the value that is still owned beside it. - self.matched_bytes = self.matched_bytes.saturating_add(MATCH_SLOT_BYTES); - budget.step(row_workspace(metadata, self.matched_bytes, pairing, owned))?; + // The match set is the one container this walk grows point by point, so it is charged by + // the block it takes when it outgrows what it holds -- before the insert allocates it, and + // never per point of a set that has room. + if matched.len() + 1 > matched.capacity() + && let Some(budget) = budget.as_deref_mut() + { + let grown = (2 * matched.capacity()).max(VEC_MIN_CAPACITY); + budget.grow( + grown + .saturating_sub(matched.capacity()) + .saturating_mul(MATCH_SLOT_BYTES), + )?; + } + if !matched.insert((group, point)) + && self.matching.cardinality == Cardinality::OneToOne + && self + .implicit_many_to_one + .is_none_or(|(first, _)| point < first) + { + self.implicit_many_to_one = Some((point, group)); } } // The value the row keeps. A `bool` comparison keeps a float and a filtered comparison - // keeps nothing, so what the operator built is dropped there before its charge is given up; - // a kept value is moved into the row. + // keeps nothing; a kept value is moved into the row. let value = if self.matching.return_bool { drop(result); Some(GridSample::Float(if keep { 1.0 } else { 0.0 })) @@ -1188,14 +1092,6 @@ impl Pairs<'_> { drop(result); None }; - // One total replaces the operator's temporary: a kept histogram moves into the results as it - // goes, and in every other case the value is already dropped. - if let Some(budget) = budget.as_deref_mut() { - if let Some(GridSample::Histogram(histogram)) = &value { - budget.keep_histogram(histogram); - } - budget.step(row_workspace(metadata, self.matched_bytes, pairing, 0))?; - } self.results .insert(metric, group, point, value, label_set, budget) } @@ -1322,31 +1218,32 @@ fn set_field( Ok(results) } -/// The capacity a field's histogram index column reaches when `pushed` output rows have been pushed -/// and the row being pushed holds `histogram`; `None` when the column does not grow. -/// -/// [`GridSeriesBuilder::push_samples`] creates the column at the first histogram with one entry per -/// row before it and then pushes, so that push allocates `max(2 * pushed, 4)`; every later row adds -/// an entry, and a full column doubles. -fn index_column_growth(pushed: usize, capacity: usize, histogram: bool) -> Option { - if capacity == 0 { - return histogram.then(|| (2 * pushed).max(VEC_MIN_CAPACITY)); - } - (pushed == capacity).then(|| 2 * capacity) -} - /// Joins the per-field results into series with all fields, by label set. /// -/// The builder's columns grow with the output rows, which only exist once the triples are being -/// grouped, so they are reserved per row -- one row's worth before the builder takes it -- when a -/// caller accounts for the workspace: the value and validity column of every field, and the -/// histogram index column of a field that holds one. +/// The merge is one phase, entered from the results the fields filed: the triples it groups, the +/// table it groups them in and the builder's own buffers. Its columns are reserved once per result +/// series, before the series pushes its first row, rather than once per row. fn merge_fields( fields: Vec, schema: Arc, mut budget: Option<&mut Budget<'_>>, ) -> Result> { let width = schema.fields.len(); + // What the merge holds before it allocates any of it, read from the results themselves: the + // kept triples, the label sets they are filed under and the histogram payload they carry. + if let Some(budget) = budget.as_deref_mut() { + let mut sets = 0usize; + let mut merge = (0usize, 0usize, 0usize); + for results in &fields { + sets = sets.saturating_add(results.series.len()); + merge = ( + merge.0.saturating_add(results.kept), + merge.1.saturating_add(results.histograms), + merge.2.saturating_add(results.histogram_heap), + ); + } + budget.phase(merge_bytes(merge.0, sets, width, merge.1, merge.2))?; + } // Per label set: the metric and its `(point, field, value)` samples. type Merged = (MetricName, Vec<(usize, usize, GridSample)>); let mut merged: HashMap = HashMap::new(); @@ -1368,18 +1265,13 @@ fn merge_fields( for (metric, mut samples) in merged.into_values() { // One builder serves every label set, so each result series starts explicitly. builder.begin_series(); - // The capacity each field's histogram index column has reached, `0` until its first - // histogram. The columns are handed out inside the series, so what they are charged stays - // charged; only the growth of one series is charged at a time. Only a caller accounting for - // the workspace reads the ledger, so an unaccounted merge allocates nothing for it. - let mut index_capacity = if budget.is_some() { - vec![0usize; width] - } else { - Vec::new() - }; samples.sort_unstable_by_key(|(point, field, _)| (*point, *field)); + // This series' columns, in one reservation before it writes the first row of them. They stay + // live inside the series it is finished as, so the phase keeps them. + if let Some(budget) = budget.as_deref_mut() { + budget.grow(merge_series_columns(&samples, width))?; + } let mut samples = samples.into_iter().peekable(); - let mut pushed = 0usize; while let Some((point, field, value)) = samples.next() { row.clear(); row.resize_with(width, || None); @@ -1387,31 +1279,34 @@ fn merge_fields( while let Some((_, field, value)) = samples.next_if(|next| next.0 == point) { row[field] = Some(value); } - // The columns of this row, admitted before the builder grows them. - if let Some(budget) = budget.as_deref_mut() { - budget.retain(merge_row_bytes(width))?; - let mut index = 0usize; - for (capacity, value) in index_capacity.iter_mut().zip(&row) { - let histogram = matches!(value, Some(GridSample::Histogram(_))); - if let Some(grown) = index_column_growth(pushed, *capacity, histogram) { - index = index.saturating_add( - grown - .saturating_sub(*capacity) - .saturating_mul(std::mem::size_of::>()), - ); - *capacity = grown; - } - } - budget.retain(index)?; - } builder.push_samples(point, &row)?; - pushed += 1; } output.extend(builder.finish(metric)?); } Ok(output) } +/// The bytes one result series' columns take: the rows it writes -- one per distinct point of its +/// samples, which is what the builder writes a row for -- times a row's columns. +/// +/// A row is the value and validity column of every field, charged twice for the capacity each column +/// grows to, and the histogram index column of every field. The index columns are counted for every +/// field rather than only the ones that happen to carry a histogram, so the figure over-reserves +/// rather than missing a column, and nothing here follows how a column grows. +fn merge_series_columns(samples: &[(usize, usize, GridSample)], fields: usize) -> usize { + let row = 2 * fields.saturating_mul(std::mem::size_of::() + 1) + + fields.saturating_mul(std::mem::size_of::>()); + let mut rows = 0usize; + let mut consumed: Option = None; + for (point, ..) in samples { + if consumed != Some(*point) { + rows = rows.saturating_add(1); + consumed = Some(*point); + } + } + rows.saturating_mul(row) +} + fn same_grid(lhs: &SeriesSchema, rhs: &SeriesSchema) -> Result> { match (&lhs.eval_grid, &rhs.eval_grid) { (Some(lhs), Some(rhs)) if lhs.spec() == rhs.spec() => Ok(lhs.clone()), @@ -1476,15 +1371,15 @@ fn group(operand: &Operand<'_>, labels: &MatchingLabels) -> HashMap, members: &[usize], relevant: impl Fn(usize) -> bool, - tables: usize, + base: usize, mut budget: Option<&mut Budget<'_>>, ) -> Result> { if members.len() < 2 { @@ -1499,14 +1394,13 @@ fn overlap( cursor.at(row).expect("a row never goes back"), SampleRef::Missing ) { - // The push that starts a new allocation: the capacity the list reaches is what is - // live beside the field's tables, charged twice for the buffer it replaces. + // A new block of the list, reserved before the push that allocates it. if let Some(budget) = budget.as_deref_mut() && points.len() == points.capacity() { let capacity = points.capacity().saturating_mul(2).max(VEC_MIN_CAPACITY); - let list = capacity.saturating_mul(2 * std::mem::size_of::()); - budget.step(tables.saturating_add(list))?; + let list = capacity.saturating_mul(std::mem::size_of::()); + budget.phase(base.saturating_add(list))?; } points.push(point); } @@ -1517,11 +1411,9 @@ fn overlap( .windows(2) .find(|pair| pair[0] == pair[1] && relevant(pair[0])) .map(|pair| pair[0]); - // The list belongs to this scan alone: it is dropped -- a duplicate found or not -- before its - // bytes are given back. - drop(points); + // The list is dropped with this scan, so the field's own phase figure is live from here on. if let Some(budget) = budget { - budget.step(tables)?; + budget.phase(base)?; } Ok(duplicate) } @@ -1611,6 +1503,36 @@ impl<'a> Operand<'a> { &self.series[index].time } + /// The widest histogram this operand's field holds. + /// + /// Read once before the rows are matched, so the workspace of a histogram row is reserved for + /// the field rather than per row. A field of floats is answered without reading a sample: its + /// segments say what it holds, so the numeric path pays nothing for this figure. + fn widest_histogram(&self) -> Option> { + let mut widest: Option<(HistogramView<'a>, usize)> = None; + for series in self.series { + let field = &series.fields.as_slice()[self.field]; + let holds_histograms = field.segments().iter().any(|segment| { + matches!( + segment.dense(), + Some(DenseSamplesRef::Histogram(_) | DenseSamplesRef::Mixed(_)) + ) + }); + if !holds_histograms { + continue; + } + for sample in field.iter() { + let SampleRef::Histogram(view) = sample else { + continue; + }; + if widest.is_none_or(|(_, widest)| owned_histogram_bytes(&view) > widest) { + widest = Some((view, owned_histogram_bytes(&view))); + } + } + } + widest.map(|(view, _)| view) + } + /// Every absolute position this series carries, with its compact row, in order. fn positions(&self, index: usize) -> datatypes::series::time::Positions<'a> { self.series[index].time.positions() @@ -1737,6 +1659,12 @@ fn elem_binop( #[derive(Default)] struct Results { series: HashMap, + /// The rows filed, the rows holding a value, the rows holding a histogram and the heap those + /// histograms hold: counted as each row is filed, so neither later phase walks them again. + rows: usize, + kept: usize, + histograms: usize, + histogram_heap: usize, } /// Rows in arrival order: their points, the match group each came from, and their values; @@ -1764,13 +1692,14 @@ fn same_labelset(key: &GroupKey, timestamp: i64) -> String { } impl Results { - /// Files one row of one field, retaining the slots it takes. + /// Files one row of one field, retaining the block its row vectors take when they outgrow what + /// they hold. /// /// The label set the row is filed under is only built when the table does not hold it yet, so /// `label_set` -- the bytes that set keeps -- is charged on the vacant entry alone. A row whose /// value the operator filtered is still a row: it keeps its slots and its label set, which the - /// merge carries as a set without a value. The value itself is charged by the caller, which - /// knows whether the row keeps a histogram. + /// merge carries as a set without a value. A kept histogram is boxed beside its row, so its + /// payload is charged here, where the value is retained, and never per grid point of a float. fn insert( &mut self, metric: &MetricName, @@ -1780,12 +1709,11 @@ impl Results { label_set: usize, mut budget: Option<&mut Budget<'_>>, ) -> Result<()> { - let kept = value.is_some(); let rows = match self.series.entry(key_of(metric)) { Entry::Occupied(entry) => entry.into_mut(), Entry::Vacant(entry) => { if let Some(budget) = budget.as_deref_mut() { - budget.retain(label_set)?; + budget.hold(label_set)?; } entry.insert(ResultRows { metric: metric.clone(), @@ -1795,9 +1723,28 @@ impl Results { }) } }; - if let Some(budget) = budget { - budget.retain(RESULT_ROW_BYTES)?; - budget.file_row(kept); + // The row vectors grow together, so one block of them is charged once when the point, + // group and value vectors outgrow the capacity they hold. + if rows.points.len() == rows.points.capacity() + && let Some(budget) = budget.as_deref_mut() + { + let grown = (2 * rows.points.capacity()).max(VEC_MIN_CAPACITY); + budget.hold( + grown + .saturating_sub(rows.points.capacity()) + .saturating_mul(RESULT_ROW_BYTES), + )?; + } + self.rows = self.rows.saturating_add(1); + if let Some(value) = &value { + self.kept = self.kept.saturating_add(1); + if let GridSample::Histogram(histogram) = value { + self.histograms = self.histograms.saturating_add(1); + self.histogram_heap = self.histogram_heap.saturating_add(histogram.heap_bytes()); + if let Some(budget) = budget { + budget.hold(result_histogram_bytes(histogram))?; + } + } } rows.points.push(point); rows.groups.push(group); diff --git a/src/promql/src/vector_match/tests.rs b/src/promql/src/vector_match/tests.rs index 3ae1fe35662..1459250a192 100644 --- a/src/promql/src/vector_match/tests.rs +++ b/src/promql/src/vector_match/tests.rs @@ -28,8 +28,7 @@ use rand::{Rng, SeedableRng}; use super::{ BinaryMatching, BinaryOp, Cardinality, FillValues, MatchingLabels, RESULT_ROW_BYTES, SetOp, - binary_vectors, binary_vectors_with_reservation, elem_binop, histogram_pair_bytes, - result_histogram_bytes, set_vectors, + binary_vectors, binary_vectors_with_reservation, set_vectors, }; use crate::grid_series::reference::{ Dense, OwnedHistogram, Points, assert_histogram_close, assert_result_eq, golden_cases, @@ -1890,8 +1889,12 @@ fn a_pairings_scratch_is_replaced_by_the_next_one() { } /// A group the one side holds twice is charged the rows it files: the kernel pairs every member of -/// the group and keeps those rows until it reports the duplicate, while a point its group has -/// already matched costs no second slot of the match set. +/// the group and keeps those rows until it reports the duplicate, which is what the run costs on top +/// of the same group with one member. +/// +/// The figures are coarse on purpose -- rows are reserved in the blocks their vectors grow into, so a +/// run costs a block rather than a row -- and what is checked is the order of the growth, not an +/// exact figure per point. #[test] fn a_duplicated_group_is_charged_the_rows_it_files() { let points = 512; @@ -1928,24 +1931,32 @@ fn a_duplicated_group_is_charged_the_rows_it_files() { Violation::ImplicitManyToOne, "{error}" ); - // The rows of the second member are what the run costs on top of the single pairing. + // The rows of the second member are what the run costs on top of the single pairing, and they + // are what its result table holds rather than a fraction of it. let single = *single_seen .iter() .max() .expect("the operation asks for its workspace"); let duplicated = *seen.iter().max().unwrap(); + let rows = points * FILED_ROW; assert!( - duplicated >= single + points * FILED_ROW, - "single {single}, duplicated {duplicated}" + duplicated >= single + rows / 2, + "single {single}, duplicated {duplicated}: the second member's {rows} bytes of rows are not \ + charged" ); - // The match set holds one slot per matched point, not one per attempt: a slot charged for the - // second member would show up in what the results keep after the field releases its tables. - let floor = retained_total(&seen); - let rows = 2 * points * RESULT_ROW_BYTES; - assert!(floor >= rows, "floor {floor}, rows {rows}"); assert!( - floor <= rows + 8 * 1024, - "floor {floor}: a matched point is charged twice, or the set is kept past its field" + duplicated <= single + 4 * rows, + "single {single}, duplicated {duplicated}: the second member's {rows} bytes of rows are \ + charged several times over" + ); + // What the operation holds once its tables are gone is the rows the two members filed, and no + // match set kept past its field: the set is part of the field's phase and goes with it. + let floor = retained_total(&seen); + let filed = 2 * points * RESULT_ROW_BYTES; + assert!(floor >= filed, "floor {floor}, rows {filed}"); + assert!( + floor <= 4 * filed + 64 * 1024, + "floor {floor}: a matched point is charged per attempt, or the set is kept past its field" ); } @@ -2044,6 +2055,142 @@ fn a_pairing_the_operator_skips_files_no_row() { ); } +/// A kept histogram's payload is what the operation genuinely holds, and a budget one byte short of +/// it refuses the run; the same operands with float values answer under a budget of a few kilobytes. +/// +/// The two sides of one figure: a result the operation really allocates is refused by a pool that +/// cannot hold it, and a small result is not refused by a reservation nothing allocates for. +#[test] +fn a_kept_histogram_payload_is_refused_and_a_small_result_is_not() { + let grid = budget_grid(1); + let schema = Arc::new(schema(1, &grid)); + let buckets = 4_096; + let matching = budget_matching(BinaryOp::Add, MatchingLabels::On(vec![Arc::from("job")])); + // One point holding a histogram of `buckets` buckets on both sides: the row is kept and the + // payload of the union is retained until the operation returns. + let lhs = vec![wide_histogram_operand( + &grid, + "a", + 1, + buckets, + "4014000000000000", + )]; + let rhs = vec![wide_histogram_operand( + &grid, + "b", + 1, + buckets, + "4014000000000000", + )]; + let (output, seen) = budgeted(&matching, &schema, &lhs, &schema, &rhs, usize::MAX); + let output = output.expect("`+` of two histograms keeps the union"); + assert_eq!(output.len(), 1, "{output:?}"); + match first_sample(&output[0]) { + SampleRef::Histogram(view) => assert_eq!( + view.positive.buckets.len(), + buckets, + "the row keeps the union of both bucket lists" + ), + _ => panic!("`+` of two histograms keeps a histogram"), + } + // What the operation holds is at least the payload of the value it keeps. + let payload = buckets * std::mem::size_of::<(i32, f64)>(); + let peak = *seen + .iter() + .max() + .expect("the operation asks for its workspace"); + assert!( + peak >= payload, + "peak {peak} does not hold the {payload} bytes of the kept histogram" + ); + // A pool one byte short of the widest figure refuses the run rather than allocate past it. + let error = budgeted(&matching, &schema, &lhs, &schema, &rhs, peak - 1) + .0 + .expect_err("the kept payload is reserved before it is filed"); + assert!( + matches!(error, DataFusionError::ResourcesExhausted(_)), + "{error}" + ); + + // The same shape with float values: two operands of one point, one result row. Nothing of that + // is measured per grid point, so a budget of a few kilobytes admits it. + let small = budget_matching(BinaryOp::Add, MatchingLabels::Ignoring(Vec::new())); + let floats = |name: &str| vec![budget_operand(&schema, name, &[0])]; + let (output, _) = budgeted( + &small, + &schema, + &floats("a"), + &schema, + &floats("b"), + 16 * 1024, + ); + assert_eq!( + output + .expect("a one-row float result fits a few kilobytes") + .len(), + 1 + ); +} + +/// A large result is genuinely refused by a bounded pool, and a sparse small result over the same +/// grid is not: the reservation follows the rows and columns the operation writes, not the grid. +/// +/// Both operands sit on a grid of a hundred thousand points. The dense pair answers every point, so +/// its result columns are millions of bytes and a pool of a few hundred kilobytes refuses it. The +/// sparse pair shares eight points, so its result is a handful of rows and the same pool admits it -- +/// a figure that reserved for the grid rather than for the rows would refuse it too. +#[test] +fn a_dense_result_is_refused_where_a_sparse_one_is_admitted() { + let points = 100_000; + let grid = budget_grid(points); + let schema = Arc::new(schema(1, &grid)); + let matching = budget_matching(BinaryOp::Add, MatchingLabels::On(vec![Arc::from("job")])); + let dense: Vec = (0..points).collect(); + let sparse: Vec = (0..8).map(|point| 1_000 * point).collect(); + let limit = 512 * 1024; + + // The dense pair answers every point: its columns are far past the pool, and it is refused. + let (output, _) = budgeted( + &matching, + &schema, + &[budget_operand(&schema, "a", &dense)], + &schema, + &[budget_operand(&schema, "b", &dense)], + limit, + ); + let error = output.expect_err("a dense result over the whole grid is refused"); + assert!( + matches!(error, DataFusionError::ResourcesExhausted(_)), + "{error}" + ); + + // The sparse pair shares eight points on the same grid: a few rows, and the same pool holds it. + let (output, seen) = budgeted( + &matching, + &schema, + &[budget_operand(&schema, "a", &sparse)], + &schema, + &[budget_operand(&schema, "b", &sparse)], + limit, + ); + let output = output.expect("a sparse result of eight rows fits a few hundred kilobytes"); + assert_eq!(output.len(), 1, "{output:?}"); + assert_eq!( + run_ranges(&output[0]).len(), + sparse.len(), + "the sparse pair keeps its eight points, one run each" + ); + // The widest figure is the presence flags plus those rows, not a column per grid point. + let peak = *seen + .iter() + .max() + .expect("the operation asks for its workspace"); + assert!( + peak < limit, + "the sparse run asked for {peak} bytes, past the {limit} it ran under" + ); +} + /// A field that meets its first histogram after a float backfills the index column of the rows /// before it, and the budget covers the column all the same. #[test] @@ -2175,153 +2322,6 @@ fn fields_holding_different_positions_merge_into_one_series() { ); } -/// One pairing whose wide histograms the comparison discards, followed by the points it keeps rows -/// from: the discarded step is released before the later rows are filed, so the operation's widest -/// figure is the larger step and not the two added up. -#[test] -fn a_discarded_wide_histogram_is_not_added_to_the_rows_filed_after_it() { - let points = 256; - let grid = budget_grid(points); - let schema = Arc::new(schema(1, &grid)); - let per_side = 1_024; - let matching = budget_matching(BinaryOp::Eq, MatchingLabels::On(vec![Arc::from("job")])); - // The counts tell the two sides apart, so `==` keeps neither histogram at the first point. One - // series per side is one pairing, walked in axis order. - let lhs = vec![wide_histogram_operand( - &grid, - "a", - points, - per_side, - "4014000000000000", - )]; - let rhs = vec![wide_histogram_operand( - &grid, - "b", - points, - per_side, - "401c000000000000", - )]; - let (output, seen) = budgeted(&matching, &schema, &lhs, &schema, &rhs, usize::MAX); - let output = output.expect("a filtered comparison holds"); - assert_eq!(output.len(), 1, "{output:?}"); - // The histogram point is filed with no value, so the series starts after it. - assert_eq!(run_ranges(&output[0]), vec![(1, points - 1)]); - // The step that reads the wide histograms is followed by one that does not, so the report falls: - // a figure that kept the discarded step would never come back down. - assert!(seen.windows(2).any(|pair| pair[0] > pair[1]), "{seen:?}"); - let peak = *seen.iter().max().unwrap(); - // A lower bound on the two phases: the owned buckets of both sides and the union they are - // merged into, which the discarded row still pays, and the slots of the rows that are kept. - let owned = 2 * per_side * std::mem::size_of::<(i32, f64)>(); - let growth = 2 * per_side * 2 * std::mem::size_of::<(i32, f64)>(); - let histograms = owned + growth; - let kept_rows = (points - 1) * RESULT_ROW_BYTES; - assert!( - peak < histograms + kept_rows, - "peak {peak} reaches the sum of the phases, {histograms} + {kept_rows}" - ); - let tight = peak + (histograms + kept_rows - peak) / 2; - assert!( - budgeted(&matching, &schema, &lhs, &schema, &rhs, tight) - .0 - .is_ok(), - "a limit between the widest step and the sum of the phases, {tight}, is enough" - ); - - // A `bool` comparison keeps a float, so the histogram it built is dropped and its step falls - // there too: the discarded value does not stay charged to the end of the row. - let booled = BinaryMatching { - op: BinaryOp::Ne, - return_bool: true, - ..matching.clone() - }; - let (output, seen) = budgeted(&booled, &schema, &lhs, &schema, &rhs, usize::MAX); - let output = output.expect("a `bool` comparison holds"); - assert_eq!(output.len(), 1, "{output:?}"); - assert_eq!( - run_ranges(&output[0]), - vec![(0, points)], - "a `bool` comparison answers every shared point" - ); - assert!(seen.windows(2).any(|pair| pair[0] > pair[1]), "{seen:?}"); -} - -/// A histogram `+` the row keeps hands its value over instead of holding it beside the operator's -/// temporary: the figure falls by exactly what the operator allocated and did not keep. -#[test] -fn a_kept_wide_histogram_is_transferred_not_held_beside_the_operator_temporary() { - let grid = budget_grid(1); - let schema = Arc::new(schema(1, &grid)); - // One point whose value is a histogram on both sides, in one match group, so the single row is - // kept and its value is the union of the two bucket lists. - let lhs = vec![wide_histogram_operand( - &grid, - "a", - 1, - 1_024, - "4014000000000000", - )]; - let rhs = vec![wide_histogram_operand( - &grid, - "b", - 1, - 4_096, - "4014000000000000", - )]; - let matching = BinaryMatching { - op: BinaryOp::Add, - return_bool: false, - labels: MatchingLabels::On(vec![Arc::from("job")]), - cardinality: Cardinality::ManyToOne(Vec::new()), - fill: FillValues::default(), - }; - let (output, seen) = budgeted(&matching, &schema, &lhs, &schema, &rhs, usize::MAX); - let output = output.expect("`+` of two histograms keeps the union"); - assert_eq!(output.len(), 1, "{output:?}"); - // What the row holds, read from its view: every bucket of the wider side is present and the - // counts are added. This says the value is the union the operator computed, so the figure below - // is measured on the object the kernel really kept. - match first_sample(&output[0]) { - SampleRef::Histogram(view) => { - assert_eq!(view.count, 10.0, "the counts of both sides are added"); - assert_eq!( - view.positive.buckets.len(), - 4_096, - "the row keeps the union of both bucket lists" - ); - } - SampleRef::Float(_) => panic!("`+` of two histograms keeps a histogram"), - SampleRef::Missing => panic!("the row holds a value"), - } - // The ownership figures, taken from the same operands the kernel read: what the operator - // allocates to read the pair, and what the object it hands back holds. The value is measured on - // the object the operator returned -- not on one rebuilt from its serialized view, whose - // buffers and span layout a round trip allocates differently. - let transient = histogram_pair_bytes(&first_sample(&lhs[0]), &first_sample(&rhs[0])); - let (result, keep) = elem_binop(matching.op, first_sample(&lhs[0]), first_sample(&rhs[0])) - .expect("the operator reads both histograms") - .expect("`+` of two histograms keeps the union"); - assert!(keep, "`+` of two histograms keeps its row"); - let kept = match &result { - GridSample::Histogram(histogram) => result_histogram_bytes(histogram), - GridSample::Float(_) => panic!("`+` of two histograms keeps a histogram"), - }; - assert!(transient > kept, "transient {transient}, kept {kept}"); - // One fall in the report is exactly what the operator allocated and the row did not keep: the - // operator's temporary is replaced by the value it handed back, so the figure holds that value - // once -- never beside the copy the operator built it from, and never twice. - assert!( - seen.windows(2) - .any(|pair| pair[0] > pair[1] && pair[0] - pair[1] == transient - kept), - "the operator's temporary was not released exactly: {seen:?}" - ); - // The value that survived that fall is still charged: it is part of the rows the merge writes. - assert!( - *seen.last().expect("the operation asks for its workspace") >= kept, - "the kept value is not part of what the operation finally holds: {seen:?}" - ); -} - /// Only the field that matches keeps storage: a second field whose operands share no value files /// nothing, and the match slots of the first field are released with it. #[test] @@ -2472,43 +2472,37 @@ fn the_budget_charges_the_matching_names_it_collects() { .map(|index| Arc::from(format!("absent_{index}"))) .collect() }; - // The peak of the whole run and the figure of its pairing step: no row is filed here, so the - // tables request comes first, the pairing's request second, and only then the sort, the field's - // release and the merge. - let measure = |labels: fn(Vec>) -> MatchingLabels, n: usize| -> (usize, usize) { + // The widest figure of the whole run: no row is filed here, so what it holds is the field's + // phase, which is where the names are collected. + let measure = |labels: fn(Vec>) -> MatchingLabels, n: usize| -> usize { let matching = budget_matching(BinaryOp::Add, labels(absent(n))); let (output, seen) = budgeted(&matching, &schema, &lhs, &schema, &rhs, usize::MAX); assert!( output.expect("the operands are readable").is_empty(), "no position is shared" ); - let peak = *seen + *seen .iter() .max() - .expect("the operation asks for its workspace"); - let pairing = *seen - .get(2) - .expect("the pairing's request follows the field's tables"); - (peak, pairing) + .expect("the operation asks for its workspace") }; - let names = 2_000 * std::mem::size_of::<&str>(); let kinds = [ MatchingLabels::On as fn(Vec>) -> MatchingLabels, MatchingLabels::Ignoring, ]; for labels in kinds { - let (none_peak, none_pair) = measure(labels, 0); - let (many_peak, many_pair) = measure(labels, 2_000); - // The names are charged in the pairing step itself, where the label list is collected - // before it is rewritten: exactly one reference per name, and nothing else moves. - assert_eq!( - many_pair, - none_pair + names, - "pairing without the names {none_pair}, with them {many_pair}" + let (none_peak, many_peak) = (measure(labels, 0), measure(labels, 2_000)); + // Two thousand names are collected before the rewrite, so the widest figure grows with + // them, in the order of one reference per name: a figure that never read them would not + // move at all, and one that charged them per row would grow by far more. + let names = 2_000 * std::mem::size_of::<&str>(); + assert!( + many_peak >= none_peak + names / 2, + "the names are not in the widest figure: {none_peak} vs {many_peak}" ); assert!( - many_peak > none_peak, - "the names are not in the widest figure: {none_peak} vs {many_peak}" + many_peak <= none_peak + 4 * names, + "the names are charged more than once each: {none_peak} vs {many_peak}" ); // A limit the run without the names admits is refused once they are collected. let matching = budget_matching(BinaryOp::Add, labels(absent(2_000))); @@ -2716,8 +2710,12 @@ fn singleton_groups_are_charged_no_duplicate_point_list() { } /// The duplicate scan's list of present points is one group's workspace: the next group's scan -/// replaces it instead of adding to it, and the figure charges the points the group's members -/// carry rather than one slot per row of every series of the field. +/// replaces it instead of adding to it, and the figure charges the points the group's members carry +/// rather than one slot per row of every series of the field. +/// +/// The list is charged by the block it grows into, so the figures are compared as growth rather than +/// as an exact count of slots: what is checked is that the points a group carries are in the figure +/// and that eight groups do not hold eight lists. #[test] fn a_groups_duplicate_scan_is_replaced_by_the_one_after_it() { // Two members of a group hold the even and the odd points of at most the first half, so the @@ -2764,13 +2762,18 @@ fn a_groups_duplicate_scan_is_replaced_by_the_one_after_it() { .max() .expect("the operation asks for its workspace") }; - // The list is charged: a group whose two members carry `rows_max` points each costs the slots - // of those points, at least one per point. + // The list is charged: a group whose two members carry `rows_max` points each costs more than + // the same shape with eight points a member, and the growth is in the order of those points. let short = measure(1, 8); let long = measure(1, rows_max); + let slots = (2 * rows_max - 16) * std::mem::size_of::(); assert!( - long >= short + 2 * (rows_max - 16) * std::mem::size_of::(), - "short {short}, long {long}: the scan's list is not charged" + long >= short + slots / 4, + "short {short}, long {long}: the scan's {slots} bytes of points are not charged" + ); + assert!( + long <= short + 4 * slots + 16 * 1024, + "short {short}, long {long}: the scan's points are charged as the whole grid" ); // And it is replaced, not accumulated: eight groups hold the same list as one group does, so // what the eight add is their table entries, never their sequences of points. @@ -3022,9 +3025,10 @@ fn a_duplicate_scan_is_charged_the_points_it_collects() { ); } -/// A field's histogram index column is charged as it grows: the builder creates it over the rows -/// before the field's first histogram and grows it with every row after, so a wide result whose -/// fields meet their histograms late holds those columns beside its value columns. +/// A field's histogram index column is charged with the value columns, block of rows by block of +/// rows: a wide result whose fields meet their histograms late still holds those columns beside its +/// value columns, and a figure that only charged a column once its first histogram arrived would +/// miss the rows the builder backfills it over. #[test] fn sparse_wide_results_are_charged_their_index_columns() { let points = 1_025; @@ -3106,38 +3110,13 @@ fn sparse_wide_results_are_charged_their_index_columns() { } } assert_eq!(rows, points, "every point is a row"); - // The capacity Rust gives these columns for this fixture, from its growth rule alone: a push - // into a full list allocates at least four entries and otherwise doubles. One value column per - // field, pushed once per row, and one index column per field, created over the rows before its - // first histogram and pushed once per row after. - let grow = |capacity: usize, len: usize| -> usize { - if len < capacity { - capacity - } else { - (2 * capacity).max(4) - } - }; - let mut values = 0usize; - let mut index = vec![0usize; fields]; - for pushed in 0..points { - values = grow(values, pushed); - let late = pushed >= points - fields; - for (field, capacity) in index.iter_mut().enumerate() { - if *capacity == 0 { - if late && pushed % fields == field { - // Created over the `pushed` rows before the field's first histogram, then - // pushed to, so its length is `pushed + 1` like every column made before it. - *capacity = grow(pushed, pushed); - } - } else { - *capacity = grow(*capacity, pushed); - } - } - } - let columns = fields * values * std::mem::size_of::() - + index.iter().fold(0usize, |bytes, capacity| { - bytes.saturating_add(capacity.saturating_mul(std::mem::size_of::>())) - }); + // One value column and one index column per field, one entry per output row. The figure the + // merge reserves is a coarse one -- the block of rows times every field's columns -- so what is + // checked is that it covers the columns' order of magnitude: every field's index entries are + // reserved for every row, including the rows before that field's first histogram, and no + // capacity rule of the builder's is reproduced here. + let columns = + fields * points * (std::mem::size_of::() + std::mem::size_of::>()); let peak = *seen .iter() .max() @@ -3147,7 +3126,7 @@ fn sparse_wide_results_are_charged_their_index_columns() { "peak {peak} below the {columns} bytes of value and index columns the result holds" ); assert!( - peak <= 2 * columns, - "peak {peak} is more than the {columns} bytes of columns the result holds" + peak <= 4 * columns, + "peak {peak} is far more than the {columns} bytes of columns the result holds" ); }