Skip to content

Commit 0e5fafb

Browse files
committed
wrap Arc<Backtrace> in Option, saving memory when backtraces are disabled
1 parent e6dbe11 commit 0e5fafb

File tree

8 files changed

+39
-33
lines changed

8 files changed

+39
-33
lines changed

src/allocator/dedicated_block_allocator/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(crate) struct DedicatedBlockAllocator {
1616
allocated: u64,
1717
/// Only used if [`crate::AllocatorDebugSettings::store_stack_traces`] is [`true`]
1818
name: Option<String>,
19-
backtrace: Arc<Backtrace>,
19+
backtrace: Option<Arc<Backtrace>>,
2020
}
2121

2222
impl DedicatedBlockAllocator {
@@ -25,7 +25,7 @@ impl DedicatedBlockAllocator {
2525
size,
2626
allocated: 0,
2727
name: None,
28-
backtrace: Arc::new(Backtrace::disabled()),
28+
backtrace: None,
2929
}
3030
}
3131
}
@@ -39,7 +39,7 @@ impl SubAllocator for DedicatedBlockAllocator {
3939
_allocation_type: AllocationType,
4040
_granularity: u64,
4141
name: &str,
42-
backtrace: Arc<Backtrace>,
42+
backtrace: Option<Arc<Backtrace>>,
4343
) -> Result<(u64, std::num::NonZeroU64)> {
4444
if self.allocated != 0 {
4545
return Err(AllocationError::OutOfMemory);
@@ -107,6 +107,8 @@ impl SubAllocator for DedicatedBlockAllocator {
107107
self.size,
108108
name,
109109
self.backtrace
110+
.as_ref()
111+
.map_or(&Backtrace::disabled(), |b| &b)
110112
)
111113
}
112114

src/allocator/free_list_allocator/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(crate) struct MemoryChunk {
3232
pub(crate) allocation_type: AllocationType,
3333
pub(crate) name: Option<String>,
3434
/// Only used if [`crate::AllocatorDebugSettings::store_stack_traces`] is [`true`]
35-
pub(crate) backtrace: Arc<Backtrace>,
35+
pub(crate) backtrace: Option<Arc<Backtrace>>,
3636
next: Option<std::num::NonZeroU64>,
3737
prev: Option<std::num::NonZeroU64>,
3838
}
@@ -79,7 +79,7 @@ impl FreeListAllocator {
7979
offset: 0,
8080
allocation_type: AllocationType::Free,
8181
name: None,
82-
backtrace: Arc::new(Backtrace::disabled()),
82+
backtrace: None,
8383
prev: None,
8484
next: None,
8585
},
@@ -162,7 +162,7 @@ impl SubAllocator for FreeListAllocator {
162162
allocation_type: AllocationType,
163163
granularity: u64,
164164
name: &str,
165-
backtrace: Arc<Backtrace>,
165+
backtrace: Option<Arc<Backtrace>>,
166166
) -> Result<(u64, std::num::NonZeroU64)> {
167167
let free_size = self.size - self.allocated;
168168
if size > free_size {
@@ -302,7 +302,7 @@ impl SubAllocator for FreeListAllocator {
302302
})?;
303303
chunk.allocation_type = AllocationType::Free;
304304
chunk.name = None;
305-
chunk.backtrace = Arc::new(Backtrace::disabled());
305+
chunk.backtrace = None;
306306

307307
self.allocated -= chunk.size;
308308

@@ -384,7 +384,7 @@ impl SubAllocator for FreeListAllocator {
384384
chunk.offset,
385385
chunk.allocation_type,
386386
name,
387-
chunk.backtrace
387+
chunk.backtrace.as_ref().map_or(&Backtrace::disabled(), |b| &b)
388388
);
389389
}
390390
}

src/allocator/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub struct AllocationReport {
3939
/// The size in bytes of the allocation.
4040
pub size: u64,
4141
#[cfg(feature = "visualizer")]
42-
pub(crate) backtrace: Arc<Backtrace>,
42+
pub(crate) backtrace: Option<Arc<Backtrace>>,
4343
}
4444

4545
/// Describes a memory block in the [`AllocatorReport`].
@@ -113,7 +113,7 @@ pub(crate) trait SubAllocator: SubAllocatorBase + fmt::Debug + Sync + Send {
113113
allocation_type: AllocationType,
114114
granularity: u64,
115115
name: &str,
116-
backtrace: Arc<Backtrace>,
116+
backtrace: Option<Arc<Backtrace>>,
117117
) -> Result<(u64, std::num::NonZeroU64)>;
118118

119119
fn free(&mut self, chunk_id: Option<std::num::NonZeroU64>) -> Result<()>;

src/d3d12/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ impl MemoryType {
435435
&mut self,
436436
device: &ID3D12DeviceVersion,
437437
desc: &AllocationCreateDesc<'_>,
438-
backtrace: Arc<Backtrace>,
438+
backtrace: Option<Arc<Backtrace>>,
439439
allocation_sizes: &AllocationSizes,
440440
) -> Result<Allocation> {
441441
let allocation_type = AllocationType::Linear;
@@ -728,11 +728,11 @@ impl Allocator {
728728
let size = desc.size;
729729
let alignment = desc.alignment;
730730

731-
let backtrace = Arc::new(if self.debug_settings.store_stack_traces {
732-
Backtrace::force_capture()
731+
let backtrace = if self.debug_settings.store_stack_traces {
732+
Some(Arc::new(Backtrace::force_capture()))
733733
} else {
734-
Backtrace::disabled()
735-
});
734+
None
735+
};
736736

737737
if self.debug_settings.log_allocations {
738738
debug!(

src/metal/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl MemoryType {
243243
&mut self,
244244
device: &ProtocolObject<dyn MTLDevice>,
245245
desc: &AllocationCreateDesc<'_>,
246-
backtrace: Arc<Backtrace>,
246+
backtrace: Option<Arc<Backtrace>>,
247247
allocation_sizes: &AllocationSizes,
248248
) -> Result<Allocation> {
249249
let allocation_type = allocator::AllocationType::Linear;
@@ -470,11 +470,11 @@ impl Allocator {
470470
let size = desc.size;
471471
let alignment = desc.alignment;
472472

473-
let backtrace = Arc::new(if self.debug_settings.store_stack_traces {
474-
Backtrace::force_capture()
473+
let backtrace = if self.debug_settings.store_stack_traces {
474+
Some(Arc::new(Backtrace::force_capture()))
475475
} else {
476-
Backtrace::disabled()
477-
});
476+
None
477+
};
478478

479479
if self.debug_settings.log_allocations {
480480
debug!(

src/visualizer/allocation_reports.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,12 @@ pub(crate) fn render_allocation_reports_ui(
126126
ui.label(name);
127127
});
128128

129-
if backtrace.status() == BacktraceStatus::Captured {
130-
resp.1.on_hover_ui(|ui| {
131-
ui.label(backtrace.to_string());
132-
});
129+
if let Some(backtrace) = backtrace {
130+
if backtrace.status() == BacktraceStatus::Captured {
131+
resp.1.on_hover_ui(|ui| {
132+
ui.label(backtrace.to_string());
133+
});
134+
}
133135
}
134136

135137
row.col(|ui| {

src/visualizer/memory_chunks.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,12 @@ pub(crate) fn render_memory_chunks_ui<'a>(
114114
if let Some(name) = &chunk.name {
115115
ui.label(format!("name: {}", name));
116116
}
117-
if settings.show_backtraces
118-
&& chunk.backtrace.status() == BacktraceStatus::Captured
119-
{
120-
ui.label(chunk.backtrace.to_string());
117+
if settings.show_backtraces {
118+
if let Some(backtrace) = chunk.backtrace.as_ref() {
119+
if backtrace.status() == BacktraceStatus::Captured {
120+
ui.label(backtrace.to_string());
121+
}
122+
}
121123
}
122124
});
123125
}

src/vulkan/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ impl MemoryType {
452452
device: &ash::Device,
453453
desc: &AllocationCreateDesc<'_>,
454454
granularity: u64,
455-
backtrace: Arc<Backtrace>,
455+
backtrace: Option<Arc<Backtrace>>,
456456
allocation_sizes: &AllocationSizes,
457457
) -> Result<Allocation> {
458458
let allocation_type = if desc.linear {
@@ -765,11 +765,11 @@ impl Allocator {
765765
let size = desc.requirements.size;
766766
let alignment = desc.requirements.alignment;
767767

768-
let backtrace = Arc::new(if self.debug_settings.store_stack_traces {
769-
Backtrace::force_capture()
768+
let backtrace = if self.debug_settings.store_stack_traces {
769+
Some(Arc::new(Backtrace::force_capture()))
770770
} else {
771-
Backtrace::disabled()
772-
});
771+
None
772+
};
773773

774774
if self.debug_settings.log_allocations {
775775
debug!(

0 commit comments

Comments
 (0)