Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement interval tree #656

Merged
merged 1 commit into from
Mar 22, 2024
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 Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ getset = "0.1"
opentelemetry = { version = "0.21.0", features = ["trace"] }
opentelemetry_sdk = { version = "0.21.0", features = ["trace"] }
parking_lot = { version = "0.12.1", optional = true }
petgraph = "0.6.4"
rand = "0.8.5"
serde = { version = "1.0.137", features = ["derive"] }
thiserror = "1.0.31"
tokio = { version = "0.2.23", package = "madsim-tokio", features = [
Expand Down
119 changes: 119 additions & 0 deletions crates/utils/benches/interval_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#![cfg(bench)]
#![feature(test)]

extern crate test;
extern crate utils;

use std::hint::black_box;

use test::Bencher;

use utils::interval_map::{Interval, IntervalMap};

struct Rng {
rogercloud marked this conversation as resolved.
Show resolved Hide resolved
state: u32,
}

impl Rng {
fn new() -> Self {
Self { state: 0x87654321 }
}

fn gen_u32(&mut self) -> u32 {
self.state ^= self.state << 13;
self.state ^= self.state >> 17;
self.state ^= self.state << 5;
self.state
}

fn gen_range_i32(&mut self, low: i32, high: i32) -> i32 {
let d = (high - low) as u32;
low + (self.gen_u32() % d) as i32
}
}

struct IntervalGenerator {
rng: Rng,
limit: i32,
}

impl IntervalGenerator {
fn new() -> Self {
const LIMIT: i32 = 1000;
Self {
rng: Rng::new(),
limit: LIMIT,
}
}

fn next(&mut self) -> Interval<i32> {
let low = self.rng.gen_range_i32(0, self.limit - 1);
let high = self.rng.gen_range_i32(low + 1, self.limit);
Interval::new(low, high)
}
}

fn bench_interval_map_insert(count: usize, bench: &mut Bencher) {
let mut gen = IntervalGenerator::new();
let intervals: Vec<_> = std::iter::repeat_with(|| gen.next()).take(count).collect();
bench.iter(|| {
let mut map = IntervalMap::new();
for i in intervals.clone() {
black_box(map.insert(i, ()));
}
});
}

fn bench_interval_map_insert_remove(count: usize, bench: &mut Bencher) {
let mut gen = IntervalGenerator::new();
let intervals: Vec<_> = std::iter::repeat_with(|| gen.next()).take(count).collect();
bench.iter(|| {
let mut map = IntervalMap::new();
for i in intervals.clone() {
black_box(map.insert(i, ()));
}
for i in &intervals {
black_box(map.remove(&i));
}
});
}

#[bench]
fn bench_interval_map_insert_100(bench: &mut Bencher) {
bench_interval_map_insert(100, bench);
}

#[bench]
fn bench_interval_map_insert_1000(bench: &mut Bencher) {
bench_interval_map_insert(1000, bench);
}

#[bench]
fn bench_interval_map_insert_10000(bench: &mut Bencher) {
bench_interval_map_insert(10_000, bench);
}

#[bench]
fn bench_interval_map_insert_100000(bench: &mut Bencher) {
bench_interval_map_insert(100_000, bench);
}

#[bench]
fn bench_interval_map_insert_remove_100(bench: &mut Bencher) {
bench_interval_map_insert_remove(100, bench);
}

#[bench]
fn bench_interval_map_insert_remove_1000(bench: &mut Bencher) {
bench_interval_map_insert_remove(1000, bench);
}

#[bench]
fn bench_interval_map_insert_remove_10000(bench: &mut Bencher) {
bench_interval_map_insert_remove(10_000, bench);
}

#[bench]
fn bench_interval_map_insert_remove_100000(bench: &mut Bencher) {
bench_interval_map_insert_remove(100_000, bench);
}
12 changes: 6 additions & 6 deletions crates/utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub type LevelConfig = tracing::metadata::LevelFilter;
pub mod duration_format {
use std::time::Duration;

use serde::{self, Deserialize, Deserializer};
use serde::{Deserialize, Deserializer};

use crate::parse_duration;

Expand All @@ -63,7 +63,7 @@ pub mod duration_format {

/// batch size deserialization formatter
pub mod bytes_format {
use serde::{self, Deserialize, Deserializer};
use serde::{Deserialize, Deserializer};

use crate::parse_batch_bytes;

Expand Down Expand Up @@ -156,7 +156,7 @@ pub enum InitialClusterState {

/// `InitialClusterState` deserialization formatter
pub mod state_format {
use serde::{self, Deserialize, Deserializer};
use serde::{Deserialize, Deserializer};

use super::InitialClusterState;
use crate::parse_state;
Expand Down Expand Up @@ -774,7 +774,7 @@ impl Default for LogConfig {

/// `LevelConfig` deserialization formatter
pub mod level_format {
use serde::{self, Deserialize, Deserializer};
use serde::{Deserialize, Deserializer};

use super::LevelConfig;
use crate::parse_log_level;
Expand Down Expand Up @@ -837,7 +837,7 @@ impl std::fmt::Display for RotationConfig {

/// `RotationConfig` deserialization formatter
pub mod rotation_format {
use serde::{self, Deserialize, Deserializer};
use serde::{Deserialize, Deserializer};

use super::RotationConfig;
use crate::parse_rotation;
Expand Down Expand Up @@ -1037,7 +1037,7 @@ impl std::fmt::Display for MetricsPushProtocol {

/// Metrics push protocol format
pub mod protocol_format {
use serde::{self, Deserialize, Deserializer};
use serde::{Deserialize, Deserializer};

use super::MetricsPushProtocol;
use crate::parse_metrics_push_protocol;
Expand Down
Loading
Loading