Skip to content

Commit 2bec38e

Browse files
bjorn3sunfishcode
authored andcommitted
Disable all debug assertions unless the debug feature is enabled (#3)
Several debug assertions are very expensive, causing allocation heavy code to be slowed down by over 100x in some cases.
1 parent c527a26 commit 2bec38e

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/dlmalloc.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33
//
44
// The original source was written by Doug Lea and released to the public domain
55

6+
macro_rules! debug_assert {
7+
($($arg:tt)*) => {
8+
if cfg!(all(feature = "debug", debug_assertions)) {
9+
assert!($($arg)*);
10+
}
11+
};
12+
}
13+
14+
macro_rules! debug_assert_eq {
15+
($($arg:tt)*) => {
16+
if cfg!(all(feature = "debug", debug_assertions)) {
17+
assert_eq!($($arg)*);
18+
}
19+
};
20+
}
21+
622
use core::cmp;
723
use core::mem;
824
use core::ptr;
@@ -1376,7 +1392,7 @@ impl<A: Allocator> Dlmalloc<A> {
13761392
// Sanity checks
13771393

13781394
unsafe fn check_any_chunk(&self, p: *mut Chunk) {
1379-
if !cfg!(debug_assertions) {
1395+
if !cfg!(all(feature = "debug", debug_assertions)) {
13801396
return;
13811397
}
13821398
debug_assert!(
@@ -1386,7 +1402,7 @@ impl<A: Allocator> Dlmalloc<A> {
13861402
}
13871403

13881404
unsafe fn check_top_chunk(&self, p: *mut Chunk) {
1389-
if !cfg!(debug_assertions) {
1405+
if !cfg!(all(feature = "debug", debug_assertions)) {
13901406
return;
13911407
}
13921408
let sp = self.segment_holding(p.cast());
@@ -1407,7 +1423,7 @@ impl<A: Allocator> Dlmalloc<A> {
14071423
}
14081424

14091425
unsafe fn check_malloced_chunk(&self, mem: *mut u8, s: usize) {
1410-
if !cfg!(debug_assertions) {
1426+
if !cfg!(all(feature = "debug", debug_assertions)) {
14111427
return;
14121428
}
14131429
if mem.is_null() {
@@ -1433,7 +1449,7 @@ impl<A: Allocator> Dlmalloc<A> {
14331449
}
14341450

14351451
unsafe fn check_mmapped_chunk(&self, p: *mut Chunk) {
1436-
if !cfg!(debug_assertions) {
1452+
if !cfg!(all(feature = "debug", debug_assertions)) {
14371453
return;
14381454
}
14391455
let sz = Chunk::size(p);
@@ -1453,7 +1469,7 @@ impl<A: Allocator> Dlmalloc<A> {
14531469
}
14541470

14551471
unsafe fn check_free_chunk(&self, p: *mut Chunk) {
1456-
if !cfg!(debug_assertions) {
1472+
if !cfg!(all(feature = "debug", debug_assertions)) {
14571473
return;
14581474
}
14591475
let sz = Chunk::size(p);
@@ -1478,7 +1494,7 @@ impl<A: Allocator> Dlmalloc<A> {
14781494
}
14791495

14801496
unsafe fn check_malloc_state(&mut self) {
1481-
if !cfg!(debug_assertions) {
1497+
if !cfg!(all(feature = "debug", debug_assertions)) {
14821498
return;
14831499
}
14841500
for i in 0..NSMALLBINS_U32 {
@@ -1506,7 +1522,7 @@ impl<A: Allocator> Dlmalloc<A> {
15061522
}
15071523

15081524
unsafe fn check_smallbin(&mut self, idx: u32) {
1509-
if !cfg!(debug_assertions) {
1525+
if !cfg!(all(feature = "debug", debug_assertions)) {
15101526
return;
15111527
}
15121528
let b = self.smallbin_at(idx);
@@ -1531,7 +1547,7 @@ impl<A: Allocator> Dlmalloc<A> {
15311547
}
15321548

15331549
unsafe fn check_treebin(&mut self, idx: u32) {
1534-
if !cfg!(debug_assertions) {
1550+
if !cfg!(all(feature = "debug", debug_assertions)) {
15351551
return;
15361552
}
15371553
let t = *self.treebin_at(idx);
@@ -1545,7 +1561,7 @@ impl<A: Allocator> Dlmalloc<A> {
15451561
}
15461562

15471563
unsafe fn check_tree(&mut self, t: *mut TreeChunk) {
1548-
if !cfg!(debug_assertions) {
1564+
if !cfg!(all(feature = "debug", debug_assertions)) {
15491565
return;
15501566
}
15511567
let tc = TreeChunk::chunk(t);

0 commit comments

Comments
 (0)