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
Show file tree
Hide file tree
Changes from all 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: 4 additions & 4 deletions src/allocator/dedicated_block_allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,7 +25,7 @@ impl DedicatedBlockAllocator {
size,
allocated: 0,
name: None,
backtrace: Arc::new(Backtrace::disabled()),
backtrace: None,
}
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -106,7 +106,7 @@ impl SubAllocator for DedicatedBlockAllocator {
memory_block_index,
self.size,
name,
self.backtrace
self.backtrace.as_deref().unwrap_or(&Backtrace::disabled())
)
}

Expand Down
10 changes: 5 additions & 5 deletions src/allocator/free_list_allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -384,7 +384,7 @@ impl SubAllocator for FreeListAllocator {
chunk.offset,
chunk.allocation_type,
name,
chunk.backtrace
chunk.backtrace.as_deref().unwrap_or(&Backtrace::disabled())
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down Expand Up @@ -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<()>;
Expand Down
10 changes: 5 additions & 5 deletions src/d3d12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!(
Expand Down
10 changes: 5 additions & 5 deletions src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!(
Expand Down
10 changes: 6 additions & 4 deletions src/visualizer/allocation_reports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
10 changes: 6 additions & 4 deletions src/visualizer/memory_chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
});
}
Expand Down
10 changes: 5 additions & 5 deletions src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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!(
Expand Down