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

wrap Arc<Backtrace> in Option, saving memory when backtraces are disabled #252

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 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
8 changes: 5 additions & 3 deletions src/allocator/dedicated_block_allocator/mod.rs
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ pub(crate) struct DedicatedBlockAllocator {
allocated: u64,
/// Only used if [`crate::AllocatorDebugSettings::store_stack_traces`] is [`true`]
name: Option<String>,
backtrace: Arc<Backtrace>,
backtrace: Option<Arc<Backtrace>>,
}

impl DedicatedBlockAllocator {
@@ -25,7 +25,7 @@ impl DedicatedBlockAllocator {
size,
allocated: 0,
name: None,
backtrace: Arc::new(Backtrace::disabled()),
backtrace: None,
}
}
}
@@ -39,7 +39,7 @@ impl SubAllocator for DedicatedBlockAllocator {
_allocation_type: AllocationType,
_granularity: u64,
name: &str,
backtrace: Arc<Backtrace>,
backtrace: Option<Arc<Backtrace>>,
) -> Result<(u64, std::num::NonZeroU64)> {
if self.allocated != 0 {
return Err(AllocationError::OutOfMemory);
@@ -107,6 +107,8 @@ impl SubAllocator for DedicatedBlockAllocator {
self.size,
name,
self.backtrace
.as_ref()
.map_or(&Backtrace::disabled(), |b| b)
)
}

13 changes: 8 additions & 5 deletions src/allocator/free_list_allocator/mod.rs
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ pub(crate) struct MemoryChunk {
pub(crate) allocation_type: AllocationType,
pub(crate) name: Option<String>,
/// Only used if [`crate::AllocatorDebugSettings::store_stack_traces`] is [`true`]
pub(crate) backtrace: Arc<Backtrace>,
pub(crate) backtrace: Option<Arc<Backtrace>>,
next: Option<std::num::NonZeroU64>,
prev: Option<std::num::NonZeroU64>,
}
@@ -79,7 +79,7 @@ impl FreeListAllocator {
offset: 0,
allocation_type: AllocationType::Free,
name: None,
backtrace: Arc::new(Backtrace::disabled()),
backtrace: None,
prev: None,
next: None,
},
@@ -162,7 +162,7 @@ impl SubAllocator for FreeListAllocator {
allocation_type: AllocationType,
granularity: u64,
name: &str,
backtrace: Arc<Backtrace>,
backtrace: Option<Arc<Backtrace>>,
) -> Result<(u64, std::num::NonZeroU64)> {
let free_size = self.size - self.allocated;
if size > free_size {
@@ -302,7 +302,7 @@ impl SubAllocator for FreeListAllocator {
})?;
chunk.allocation_type = AllocationType::Free;
chunk.name = None;
chunk.backtrace = Arc::new(Backtrace::disabled());
chunk.backtrace = None;

self.allocated -= chunk.size;

@@ -384,7 +384,10 @@ impl SubAllocator for FreeListAllocator {
chunk.offset,
chunk.allocation_type,
name,
chunk.backtrace
chunk
.backtrace
.as_ref()
.map_or(&Backtrace::disabled(), |b| b)
);
}
}
4 changes: 2 additions & 2 deletions src/allocator/mod.rs
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ pub struct AllocationReport {
/// The size in bytes of the allocation.
pub size: u64,
#[cfg(feature = "visualizer")]
pub(crate) backtrace: Arc<Backtrace>,
pub(crate) backtrace: Option<Arc<Backtrace>>,
}

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

fn free(&mut self, chunk_id: Option<std::num::NonZeroU64>) -> Result<()>;
10 changes: 5 additions & 5 deletions src/d3d12/mod.rs
Original file line number Diff line number Diff line change
@@ -435,7 +435,7 @@ impl MemoryType {
&mut self,
device: &ID3D12DeviceVersion,
desc: &AllocationCreateDesc<'_>,
backtrace: Arc<Backtrace>,
backtrace: Option<Arc<Backtrace>>,
allocation_sizes: &AllocationSizes,
) -> Result<Allocation> {
let allocation_type = AllocationType::Linear;
@@ -728,11 +728,11 @@ impl Allocator {
let size = desc.size;
let alignment = desc.alignment;

let backtrace = Arc::new(if self.debug_settings.store_stack_traces {
Backtrace::force_capture()
let backtrace = if self.debug_settings.store_stack_traces {
Some(Arc::new(Backtrace::force_capture()))
} else {
Backtrace::disabled()
});
None
};

if self.debug_settings.log_allocations {
debug!(
10 changes: 5 additions & 5 deletions src/metal/mod.rs
Original file line number Diff line number Diff line change
@@ -243,7 +243,7 @@ impl MemoryType {
&mut self,
device: &ProtocolObject<dyn MTLDevice>,
desc: &AllocationCreateDesc<'_>,
backtrace: Arc<Backtrace>,
backtrace: Option<Arc<Backtrace>>,
allocation_sizes: &AllocationSizes,
) -> Result<Allocation> {
let allocation_type = allocator::AllocationType::Linear;
@@ -470,11 +470,11 @@ impl Allocator {
let size = desc.size;
let alignment = desc.alignment;

let backtrace = Arc::new(if self.debug_settings.store_stack_traces {
Backtrace::force_capture()
let backtrace = if self.debug_settings.store_stack_traces {
Some(Arc::new(Backtrace::force_capture()))
} else {
Backtrace::disabled()
});
None
};

if self.debug_settings.log_allocations {
debug!(
10 changes: 6 additions & 4 deletions src/visualizer/allocation_reports.rs
Original file line number Diff line number Diff line change
@@ -126,10 +126,12 @@ pub(crate) fn render_allocation_reports_ui(
ui.label(name);
});

if backtrace.status() == BacktraceStatus::Captured {
resp.1.on_hover_ui(|ui| {
ui.label(backtrace.to_string());
});
if let Some(backtrace) = backtrace {
if backtrace.status() == BacktraceStatus::Captured {
resp.1.on_hover_ui(|ui| {
ui.label(backtrace.to_string());
});
}
}

row.col(|ui| {
10 changes: 6 additions & 4 deletions src/visualizer/memory_chunks.rs
Original file line number Diff line number Diff line change
@@ -114,10 +114,12 @@ pub(crate) fn render_memory_chunks_ui<'a>(
if let Some(name) = &chunk.name {
ui.label(format!("name: {}", name));
}
if settings.show_backtraces
&& chunk.backtrace.status() == BacktraceStatus::Captured
{
ui.label(chunk.backtrace.to_string());
if settings.show_backtraces {
if let Some(backtrace) = chunk.backtrace.as_ref() {
if backtrace.status() == BacktraceStatus::Captured {
ui.label(backtrace.to_string());
}
}
}
});
}
10 changes: 5 additions & 5 deletions src/vulkan/mod.rs
Original file line number Diff line number Diff line change
@@ -452,7 +452,7 @@ impl MemoryType {
device: &ash::Device,
desc: &AllocationCreateDesc<'_>,
granularity: u64,
backtrace: Arc<Backtrace>,
backtrace: Option<Arc<Backtrace>>,
allocation_sizes: &AllocationSizes,
) -> Result<Allocation> {
let allocation_type = if desc.linear {
@@ -765,11 +765,11 @@ impl Allocator {
let size = desc.requirements.size;
let alignment = desc.requirements.alignment;

let backtrace = Arc::new(if self.debug_settings.store_stack_traces {
Backtrace::force_capture()
let backtrace = if self.debug_settings.store_stack_traces {
Some(Arc::new(Backtrace::force_capture()))
} else {
Backtrace::disabled()
});
None
};

if self.debug_settings.log_allocations {
debug!(