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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

## git

- Only read the clock for build timings when trace logging is on, and never on
`wasm32-unknown-unknown`, where `std::time::Instant::now` panics.

## 2.1.1

- Bump `cacheline_ef` to also use version `13` of `epserde`.
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ use stats::BucketStats;
use std::array::from_fn;
use std::{borrow::Borrow, default::Default, marker::PhantomData, time::Instant};

use crate::{hash::*, pack::Packed, reduce::*, util::log_duration};
use crate::{hash::*, pack::Packed, reduce::*, util::{log_duration, Timer}};

/// Parameters for PtrHash construction.
///
Expand Down Expand Up @@ -592,7 +592,7 @@ impl<
&mut self,
keys: impl ParallelIterator<Item = impl Borrow<Key>> + Clone + 'a,
) -> Option<BucketStats> {
let overall_start = std::time::Instant::now();
let overall_start = Timer::start();
// Initialize arrays;
let mut taken: Vec<BitVec> = vec![];
let mut pilots: Vec<u8> = vec![];
Expand Down Expand Up @@ -643,7 +643,7 @@ impl<
izip!(shard_hashes, shard_pilots, shard_taken).enumerate()
{
// Determine the buckets.
let start = std::time::Instant::now();
let start = Timer::start();
let Some((hashes, part_starts)) = self.sort_parts(shard, hashes) else {
trace!("Found duplicate hashes");
// Found duplicate hashes.
Expand All @@ -663,7 +663,7 @@ impl<
}
}

let start = std::time::Instant::now();
let start = Timer::start();
let remap = self.remap_free_slots(&taken);
log_duration("remap free", start);
if remap.is_err() {
Expand Down
8 changes: 4 additions & 4 deletions src/shard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl<
keys: impl ParallelIterator<Item = impl Borrow<Key>> + Clone + 'a,
) -> Box<dyn Iterator<Item = Vec<Hx::H>> + 'a> {
trace!("No sharding: collecting all {} hashes in memory.", self.n);
let start = std::time::Instant::now();
let start = Timer::start();
let hashes = keys.map(|key| self.hash_key(key.borrow())).collect();
log_duration("collect hash", start);
Box::new(std::iter::once(hashes))
Expand All @@ -101,7 +101,7 @@ impl<
);
let it = (0..self.shards).map(move |shard| {
trace!("Shard {shard:>3}/{:3}\r", self.shards);
let start = std::time::Instant::now();
let start = Timer::start();
let hashes: Vec<_> = keys
.clone()
.map(|key| self.hash_key(key.borrow()))
Expand Down Expand Up @@ -152,7 +152,7 @@ impl<
let shard_range = first_shard..(first_shard + shards_on_disk).min(self.shards);
info!("Writing keys for shards {shard_range:?}/{}", self.shards);

let start = std::time::Instant::now();
let start = Timer::start();

// Create a file writer and count for each shard.
let writers = shard_range
Expand Down Expand Up @@ -202,7 +202,7 @@ impl<
.into_iter()
.zip(shard_range)
.map(move |((f, cnt), _shard)| {
let start = std::time::Instant::now();
let start = Timer::start();
let mut v = vec![Hx::H::default(); cnt];
let mut reader = BufReader::new(f);
let (pre, data, post) = unsafe { v.align_to_mut::<u8>() };
Expand Down
3 changes: 1 addition & 2 deletions src/sort_buckets.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::*;
use crate::bucket_idx::BucketIdx;
use rdst::RadixSort;
use std::time::Instant;

impl<Key: KeyT + ?Sized, BF: BucketFn, F: Packed, Hx: KeyHasher<Key>, const SINGLE_PART: bool, const REMAP: bool>
PtrHash<Key, BF, F, Hx, Vec<u8>, SINGLE_PART, REMAP>
Expand All @@ -24,7 +23,7 @@ impl<Key: KeyT + ?Sized, BF: BucketFn, F: Packed, Hx: KeyHasher<Key>, const SING
// 32 bits.
// NOTE: This does not work for other reduction methods.

let start = Instant::now();
let start = Timer::start();
// 2. Radix sort hashes.
// HOT: This takes half the time for 128bit hashes.
// TODO: Just append each hash to its part directly, where each part has
Expand Down
32 changes: 25 additions & 7 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,40 @@ thread_local! {
static LOG: std::cell::Cell<bool> = std::cell::Cell::new(true);
}

pub(crate) fn log_duration(name: &str, start: Instant) -> Instant {
/// When a step started, for [`log_duration`].
///
/// The time is only read when trace logging is on, since it only feeds `trace!`, and never on
/// `wasm32-unknown-unknown`, where `Instant::now` panics.
#[derive(Clone, Copy)]
pub(crate) struct Timer(Option<Instant>);

impl Timer {
pub(crate) fn start() -> Self {
let enabled = log::log_enabled!(log::Level::Trace)
&& !cfg!(all(target_arch = "wasm32", target_os = "unknown"));
Timer(enabled.then(Instant::now))
}
}

pub(crate) fn log_duration(name: &str, start: Timer) -> Timer {
let Some(instant) = start.0 else {
return start;
};
if !LOG.with(|log| log.get()) {
return start;
}
trace!(
"{}",
format!("{name:>12}: {:>13.2?}s", start.elapsed().as_secs_f32()).bold()
format!("{name:>12}: {:>13.2?}s", instant.elapsed().as_secs_f32()).bold()
);
Instant::now()
Timer::start()
}

pub fn generate_keys(n: usize) -> Vec<u64> {
// TODO: Deterministic key generation.
let start = Instant::now();
let start = Timer::start();
let keys = loop {
let start = Instant::now();
let start = Timer::start();
let keys: Vec<_> = (0..n)
.into_par_iter()
.map_init(rng, |rng, _| rng.random())
Expand All @@ -53,8 +71,8 @@ pub fn generate_keys(n: usize) -> Vec<u64> {
}

pub fn generate_string_keys(n: usize) -> Vec<Vec<u8>> {
let start = Instant::now();
// let start = Instant::now();
let start = Timer::start();
// let start = Timer::start();
let keys: Vec<_> = (0..n)
.into_par_iter()
.map_init(rng, |rng, _| {
Expand Down