Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 3 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bitflags = "2"
polars = { version = "0.46", features = ["lazy", "ipc", "ipc_streaming"] }
sketch_oxide = "0.1.6"
datasketches = "0.2.0"
asap_sketchlib = { version = "0.2.2", features = ["experimental"] }
asap_sketchlib = { version = "0.3.0", features = ["experimental"] }

# Internal workspace crates.
aqpbm-datagen = { path = "aqpbm-datagen" }
Expand Down
42 changes: 42 additions & 0 deletions aqpbm-core/src/heap_footprint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! What asap_sketchlib 0.3's `HHHeap` (behind `CMSHeap`, `CSHeap` and
//! UnivMon's layers) holds for its residents: the heap array of `HHItem`s, a
//! parallel digest column (`slots: Vec<u64>`), and a position index
//! `HashMap<u64, SmallVec<[usize; 2]>>` that hashbrown sizes in power-of-two
//! buckets at 7/8 load. The residents' own key bytes, for string keys, are the
//! caller's to add.

/// One index bucket: the `u64` digest, the inline `SmallVec<[usize; 2]>`
/// (two positions plus its length word) and hashbrown's control byte.
pub const HH_HEAP_INDEX_ENTRY_BYTES: usize = 8 + 3 * std::mem::size_of::<usize>() + 1;

/// Buckets hashbrown allocates for `capacity` entries: at 7/8 load, rounded
/// up to a power of two.
pub fn hash_buckets(capacity: usize) -> usize {
if capacity == 0 {
return 0;
}
(capacity.div_ceil(7) * 8).next_power_of_two()
}

/// Bytes an `HHHeap` holding `capacity` residents keeps, given the size of
/// one `HHItem`: the item, its digest, and its share of the index.
pub fn hh_heap_bytes(capacity: usize, item_bytes: usize) -> usize {
capacity * (item_bytes + std::mem::size_of::<u64>())
+ hash_buckets(capacity) * HH_HEAP_INDEX_ENTRY_BYTES
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn a_resident_costs_more_than_its_item() {
let item = 40;
for (small, large) in [(32, 128), (128, 2048)] {
let grown = hh_heap_bytes(large, item) - hh_heap_bytes(small, item);
assert!(grown > (large - small) * item, "{small}->{large}: {grown}");
}
assert_eq!(hash_buckets(32), 64);
assert_eq!(hh_heap_bytes(0, item), 0);
}
}
2 changes: 2 additions & 0 deletions aqpbm-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod atomic_costs;
pub mod benchmark_result;
pub mod erp;
pub mod error;
pub mod heap_footprint;
pub mod measure;
pub mod metrics;

Expand All @@ -31,6 +32,7 @@ pub use erp::{
erp_artifact, erp_record, ErpArtifact, ErpRecord, ErpResourceProfile, ERP_SCHEMA_VERSION,
};
pub use error::RunError;
pub use heap_footprint::{hash_buckets, hh_heap_bytes, HH_HEAP_INDEX_ENTRY_BYTES};
pub use measure::{
measure, record_calls, runs_for, MeasureConfig, Measurement, Pass, Report, RunOutcome,
MIN_MERGE_SHARDS,
Expand Down
4 changes: 1 addition & 3 deletions aqpbm-planeval/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ asap-types = { git = "https://github.com/ProjectASAP/ASAPPlanner", rev = "3c3133
asap-aware-mapping = { git = "https://github.com/ProjectASAP/ASAPPlanner", rev = "3c31338d6fa28e31acaf0a11626a05f4d04df9b0" }
asap-frontend-promql = { git = "https://github.com/ProjectASAP/ASAPPlanner", rev = "3c31338d6fa28e31acaf0a11626a05f4d04df9b0" }

# 0.3.0, not the workspace pin (0.2.2): cdf_cached/quantile_cached do not
# exist before 0.3.0 and a 101-point probe grid re-sorts on every call there.
asap_sketchlib = { version = "0.3.0", features = ["experimental"] }
asap_sketchlib.workspace = true

aqpbm-core = { workspace = true, features = ["hdrhist"] }
aqpbm-datagen.workspace = true
Expand Down
18 changes: 3 additions & 15 deletions aqpbm-planeval/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,6 @@ impl SummaryHandle for CsHeapHandle {

const SKETCHLIB_PREALLOCATED_SLOTS: usize = 1024;

const SKETCHLIB_HEAP_INDEX_ENTRY_BYTES: usize = 8 + 24 + 1;

fn grown_capacity(reserved: usize, len: usize) -> usize {
let mut capacity = reserved.max(1);
while capacity < len {
Expand All @@ -829,14 +827,6 @@ fn grown_capacity(reserved: usize, len: usize) -> usize {
capacity
}

fn hash_buckets(capacity: usize) -> usize {
if capacity == 0 {
return 0;
}
let wanted = capacity.div_ceil(7) * 8;
wanted.next_power_of_two()
}

fn heap_bytes(heap: &HHHeap, heap_size: usize) -> usize {
let residents = heap.heap();
let capacity = grown_capacity(heap_size.min(SKETCHLIB_PREALLOCATED_SLOTS), residents.len());
Expand All @@ -848,10 +838,7 @@ fn heap_bytes(heap: &HHHeap, heap_size: usize) -> usize {
_ => 0,
})
.sum();
capacity * std::mem::size_of::<HHItem>()
+ capacity * std::mem::size_of::<u64>()
+ hash_buckets(capacity) * SKETCHLIB_HEAP_INDEX_ENTRY_BYTES
+ keys
aqpbm_core::hh_heap_bytes(capacity, std::mem::size_of::<HHItem>()) + keys
}

fn heap_topk(node: PostAsapNodeId, heap: &HHHeap, k: usize) -> Result<Answer, EvalError> {
Expand Down Expand Up @@ -1225,7 +1212,8 @@ mod tests {

let items = heap_size as usize * std::mem::size_of::<HHItem>();
let digests = heap_size as usize * std::mem::size_of::<u64>();
let index = hash_buckets(heap_size as usize) * SKETCHLIB_HEAP_INDEX_ENTRY_BYTES;
let index =
aqpbm_core::hash_buckets(heap_size as usize) * aqpbm_core::HH_HEAP_INDEX_ENTRY_BYTES;
let keys: usize = (0..heap_size as usize).map(|_| "series-000".len()).sum();
assert!(
held >= items + digests + index,
Expand Down
25 changes: 23 additions & 2 deletions sketch-bench/src/wrappers/cms_heap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ mod tests {
}

/// Footprint is `rows * cols` counters at the real width plus a full heap
/// of `heap` `HHItem`s, as UnivMon counts its heaps (#146).
/// of `heap` residents: items, digests and the position index (#146).
#[test]
fn footprint_is_counters_plus_heap() {
let p = ParamSet::of(&CmsHeapParams {
rows: 5,
cols: 2048,
heap: Some(128),
});
let heap = 128 * std::mem::size_of::<asap_sketchlib::input::HHItem>();
let heap = crate::wrappers::hh_heap_footprint(128);
let fast = build_cms_heap_lib_vector2d_fast(&p).expect("5x2048 is a valid shape");
assert_eq!(
memory_cms_heap_lib_vector2d_fast(&fast),
Expand All @@ -91,6 +91,27 @@ mod tests {
);
}

/// A resident costs more than its `HHItem`: its digest and its share of
/// the position index too.
#[test]
fn the_footprint_grows_by_more_than_an_item_per_resident() {
let at = |heap| {
let p = ParamSet::of(&CmsHeapParams {
rows: 3,
cols: 256,
heap: Some(heap),
});
memory_cms_heap_lib_vector2d_fast(&build_cms_heap_lib_vector2d_fast(&p).unwrap())
};
let item = std::mem::size_of::<asap_sketchlib::input::HHItem>();
for (small, large) in [(32, 128), (512, 2048)] {
assert!(
at(large) - at(small) > (large - small) * item,
"{small} -> {large}"
);
}
}

/// A heap smaller than the `k` it answers can't answer, and is refused.
#[test]
fn a_heap_below_k_is_refused() {
Expand Down
4 changes: 2 additions & 2 deletions sketch-bench/src/wrappers/cms_heap/sketchlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn build_cms_heap_lib_vector2d_fast(
/// UnivMon counts its heaps so the TopK rows compare like for like.
pub fn memory_cms_heap_lib_vector2d_fast(sketch: &CmsHeapLibVector2dFast) -> usize {
sketch.rows * sketch.cols * std::mem::size_of::<i32>()
+ sketch.heap * std::mem::size_of::<HHItem>()
+ crate::wrappers::hh_heap_footprint(sketch.heap)
}

// ---------- asap_sketchlib: Vector2D + RegularPath ----------
Expand Down Expand Up @@ -109,7 +109,7 @@ pub fn build_cms_heap_lib_vector2d_regular(
/// As [`memory_cms_heap_lib_vector2d_fast`].
pub fn memory_cms_heap_lib_vector2d_regular(sketch: &CmsHeapLibVector2dRegular) -> usize {
sketch.rows * sketch.cols * std::mem::size_of::<i32>()
+ sketch.heap * std::mem::size_of::<HHItem>()
+ crate::wrappers::hh_heap_footprint(sketch.heap)
}

// ---------- insert / insert_step (FastPath) ----------
Expand Down
3 changes: 1 addition & 2 deletions sketch-bench/src/wrappers/cs_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::params::{CsHeapParams, ParamSet};
use crate::wrappers::cms_heap::sketchlib::{heap_capacity, top_k, TopkAnswer};
use crate::wrappers::frequency_value::FrequencyValue;
use crate::wrappers::{require_positive, BuildError, Pass, QueryPass, Shared, StepPass};
use asap_sketchlib::input::HHItem;
use asap_sketchlib::{heap_item_to_sketch_input, CSHeap, FastPath, Vector2D};
use std::cell::RefCell;
use std::rc::Rc;
Expand Down Expand Up @@ -39,7 +38,7 @@ pub fn build_cs_heap_lib_vector2d_fast(
/// Counter matrix plus a full heap, as `cms_heap` counts it.
pub fn memory_cs_heap_lib_vector2d_fast(sketch: &CsHeapLibVector2dFast) -> usize {
sketch.rows * sketch.cols * std::mem::size_of::<i32>()
+ sketch.heap * std::mem::size_of::<HHItem>()
+ crate::wrappers::hh_heap_footprint(sketch.heap)
}

pub fn insert_cs_heap_lib_vector2d_fast<T: FrequencyValue>(
Expand Down
13 changes: 10 additions & 3 deletions sketch-bench/src/wrappers/dd/sketchlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ where
// Built, fed and folded here: the closure below asks, and only asks.
let (mut sketch, rest) = dd_lib_shards(params, &items, shards)?;
for other in rest.iter() {
sketch.inner.merge(&other.inner);
sketch
.inner
.merge(&other.inner)
.expect("both operands built from one ParamSet, so alpha matches");
}
let probes = probes.clone();
out.push(Box::new(move || {
Expand Down Expand Up @@ -142,7 +145,9 @@ where
let (mut acc, rest) = dd_lib_shards(params, &items, shards)?;
out.push(Box::new(move || {
for other in rest.iter() {
acc.inner.merge(&other.inner);
acc.inner
.merge(&other.inner)
.expect("both operands built from one ParamSet, so alpha matches");
}
memory_dd_lib::<T>(&acc)
}) as Pass);
Expand All @@ -168,7 +173,9 @@ where
steps: rest.len(),
step: Box::new(move |i| {
let acc = &mut *driven.borrow_mut();
acc.inner.merge(&rest[i].inner);
acc.inner
.merge(&rest[i].inner)
.expect("both operands built from one ParamSet, so alpha matches");
}),
footprint: Box::new(move || memory_dd_lib::<T>(&read.borrow())),
});
Expand Down
Loading
Loading