Skip to content

Commit

Permalink
feat: add interval tree implementation
Browse files Browse the repository at this point in the history
Signed-off-by: bsbds <[email protected]>
  • Loading branch information
bsbds authored and mergify[bot] committed Mar 22, 2024
1 parent 90f043c commit 40763a8
Show file tree
Hide file tree
Showing 8 changed files with 1,499 additions and 6 deletions.
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 {
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

0 comments on commit 40763a8

Please sign in to comment.