Skip to content

Commit 95a883f

Browse files
authored
Merge pull request #1349 from spacejam/tyler_improve_binary_search
improve binary search
2 parents a0d51f2 + 6ab5931 commit 95a883f

File tree

9 files changed

+56
-78
lines changed

9 files changed

+56
-78
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
default.sled
12
*db
23
*conf
34
*snap.*

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
## Breaking Changes
2525

26+
* #1349 The "measure_allocs" feature has been removed.
2627
* #1135 The "no_metrics" anti-feature has been replaced with
2728
the "metrics" positive feature.
2829
* #1178 the `Event` enum has become a unified struct that allows

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ event_log = []
3939
metrics = ["num-format"]
4040
no_logs = ["log/max_level_off"]
4141
no_inline = []
42-
measure_allocs = []
4342
pretty_backtrace = ["color-backtrace"]
4443
io_uring = ["rio"]
4544
docs = []

benchmarks/stress2/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ event_log = ["sled/event_log"]
2020
compression = ["sled/compression"]
2121
no_logs = ["sled/no_logs"]
2222
metrics = ["sled/metrics"]
23-
measure_allocs = ["sled/measure_allocs"]
2423
jemalloc = ["jemallocator"]
2524
logging = ["env_logger", "log", "color-backtrace"]
2625
dh = ["dhat"]
2726
memshred = []
27+
measure_allocs = []
2828

2929
[dependencies]
3030
rand = "0.7.3"

benchmarks/stress2/src/main.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ use rand::{thread_rng, Rng};
1414

1515
#[cfg(feature = "jemalloc")]
1616
mod alloc {
17-
use std::alloc::Layout;
1817
use jemallocator::Jemalloc;
18+
use std::alloc::Layout;
1919

2020
#[global_allocator]
2121
static ALLOCATOR: Jemalloc = Jemalloc;
2222
}
2323

2424
#[cfg(feature = "memshred")]
2525
mod alloc {
26-
use std::alloc::{System, Layout};
26+
use std::alloc::{Layout, System};
2727

2828
#[global_allocator]
2929
static ALLOCATOR: Alloc = Alloc;
@@ -42,11 +42,35 @@ mod alloc {
4242
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
4343
std::ptr::write_bytes(ptr, 0xde, layout.size());
4444
System.dealloc(ptr, layout)
45-
4645
}
4746
}
4847
}
4948

49+
#[cfg(feature = "measure_allocs")]
50+
mod alloc {
51+
use std::alloc::{Layout, System};
52+
use std::sync::atomic::{AtomicUsize, Ordering::Release};
53+
54+
pub static ALLOCATIONS: AtomicUsize = AtomicUsize::new(0);
55+
pub static ALLOCATED_BYTES: AtomicUsize = AtomicUsize::new(0);
56+
57+
#[global_allocator]
58+
static ALLOCATOR: Alloc = Alloc;
59+
60+
#[derive(Default, Debug, Clone, Copy)]
61+
struct Alloc;
62+
63+
unsafe impl std::alloc::GlobalAlloc for Alloc {
64+
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
65+
ALLOCATIONS.fetch_add(1, Release);
66+
ALLOCATED_BYTES.fetch_add(layout.size(), Release);
67+
System.alloc(layout)
68+
}
69+
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
70+
System.dealloc(ptr, layout)
71+
}
72+
}
73+
}
5074

5175
#[global_allocator]
5276
#[cfg(feature = "dh")]
@@ -385,6 +409,17 @@ fn main() {
385409
((ops * 1_000) / (time * 1_000)).to_formatted_string(&Locale::en)
386410
);
387411

412+
#[cfg(feature = "measure_allocs")]
413+
println!(
414+
"allocated {} bytes in {} allocations",
415+
alloc::ALLOCATED_BYTES
416+
.load(Ordering::Acquire)
417+
.to_formatted_string(&Locale::en),
418+
alloc::ALLOCATIONS
419+
.load(Ordering::Acquire)
420+
.to_formatted_string(&Locale::en),
421+
);
422+
388423
#[cfg(feature = "metrics")]
389424
sled::print_profile();
390425
}

src/lib.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,6 @@ mod flusher;
243243
/// The event log helps debug concurrency issues.
244244
pub mod event_log;
245245

246-
#[cfg(feature = "measure_allocs")]
247-
mod measure_allocs;
248-
249-
#[cfg(feature = "measure_allocs")]
250-
#[global_allocator]
251-
static ALLOCATOR: measure_allocs::TrackingAllocator =
252-
measure_allocs::TrackingAllocator;
253-
254246
/// Opens a `Db` with a default configuration at the
255247
/// specified path. This will create a new storage
256248
/// directory at the specified path if it does

src/measure_allocs.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/metrics.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,6 @@ pub struct Metrics {
145145
pub tree_traverse: Histogram,
146146
pub write_to_log: Histogram,
147147
pub written_bytes: Histogram,
148-
#[cfg(feature = "measure_allocs")]
149-
pub allocations: CachePadded<AtomicUsize>,
150-
#[cfg(feature = "measure_allocs")]
151-
pub allocated_bytes: CachePadded<AtomicUsize>,
152148
}
153149

154150
impl Metrics {
@@ -437,27 +433,6 @@ impl Metrics {
437433
sz("seg util end", &self.segment_utilization_shutdown),
438434
]));
439435

440-
#[cfg(feature = "measure_allocs")]
441-
{
442-
ret.push_str(&format!(
443-
"{}\n",
444-
std::iter::repeat("-").take(134).collect::<String>()
445-
));
446-
ret.push_str("allocation statistics:\n");
447-
ret.push_str(&format!(
448-
"total allocations: {}\n",
449-
measure_allocs::ALLOCATIONS
450-
.load(Acquire)
451-
.to_formatted_string(&Locale::en)
452-
));
453-
ret.push_str(&format!(
454-
"allocated bytes: {}\n",
455-
measure_allocs::ALLOCATED_BYTES
456-
.load(Acquire)
457-
.to_formatted_string(&Locale::en)
458-
));
459-
}
460-
461436
ret
462437
}
463438
}

src/node.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,27 +2225,25 @@ impl Inner {
22252225
if size == 0 || self.index_key(0).unwrap_slice() > key {
22262226
return Err(0);
22272227
}
2228-
let mut base = 0_usize;
2229-
while size > 1 {
2230-
let half = size / 2;
2231-
let mid = base + half;
2232-
// mid is always in [0, size), that means mid is >= 0 and < size.
2233-
// mid >= 0: by definition
2234-
// mid < size: mid = size / 2 + size / 4 + size / 8 ...
2228+
let mut left = 0;
2229+
let mut right = size;
2230+
while left < right {
2231+
let mid = left + size / 2;
2232+
22352233
let l = self.index_key(mid);
22362234
let cmp = crate::fastcmp(l.unwrap_slice(), key);
2237-
base = if cmp == Greater { base } else { mid };
2238-
size -= half;
2239-
}
2240-
// base is always in [0, size) because base <= mid.
2241-
let l = self.index_key(base);
2242-
let cmp = crate::fastcmp(l.unwrap_slice(), key);
22432235

2244-
if cmp == Equal {
2245-
Ok(base)
2246-
} else {
2247-
Err(base + (cmp == Less) as usize)
2236+
if cmp == Less {
2237+
left = mid + 1;
2238+
} else if cmp == Greater {
2239+
right = mid;
2240+
} else {
2241+
return Ok(mid);
2242+
}
2243+
2244+
size = right - left;
22482245
}
2246+
Err(left)
22492247
}
22502248

22512249
pub(crate) fn can_merge_child(&self, pid: u64) -> bool {

0 commit comments

Comments
 (0)