Skip to content

Commit

Permalink
switch to 'header_from_offset' instead of .nth, huge speedup
Browse files Browse the repository at this point in the history
  • Loading branch information
zolutal committed Jan 4, 2024
1 parent 36fefeb commit a73f511
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
34 changes: 17 additions & 17 deletions src/dwarf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,15 @@ impl<'a> Dwarf<'a> {
pub(crate) fn unit_context<F,R>(&self, loc: &Location, f: F) -> Result<R, Error>
where F: FnOnce(&CU) -> R {
self.borrow_dwarf(|dwarf| {
let mut unit_headers = dwarf.units();
let unit = if let Ok(Some(header)) = unit_headers.nth(loc.header) {
if let Ok(unit) = dwarf.unit(header) {
unit
} else {
return Err(Error::CUError(
format!("Failed to find CU at location: {:?}", loc)
));
}
} else {
return Err(Error::CUError(
format!("Failed to find CU header at location: {:?}", loc)
));
let debug_info = dwarf.debug_info;
let unit_header = match debug_info.header_from_offset(loc.header) {
Ok(header) => header,
Err(e) => return Err(
Error::CUError(
format!("Failed to seek to UnitHeader, error: {}", e)
))
};
let unit = gimli::Unit::new(dwarf, unit_header).unwrap();
Ok(f(&unit))
})
}
Expand All @@ -110,8 +105,7 @@ impl<'a> Dwarf<'a> {
-> Result<(), Error>
where F: FnMut(&DIE, Location) -> bool {
self.borrow_dwarf(|dwarf| {
let mut header_idx = 0;
let mut unit_headers = dwarf.units();
let mut unit_headers = dwarf.debug_info.units();
while let Ok(Some(header)) = unit_headers.next() {
let unit = match dwarf.unit(header) {
Ok(unit) => unit,
Expand All @@ -131,8 +125,15 @@ impl<'a> Dwarf<'a> {
}
}

let header_offset =
match header.offset().as_debug_info_offset() {
Some(offset) => offset,
// should be unreachable
None => return Err(Error::HeaderOffsetError)
};

let location = Location {
header: header_idx,
header: header_offset,
offset: entry.offset(),
};

Expand All @@ -141,7 +142,6 @@ impl<'a> Dwarf<'a> {
return Ok(())
}
}
header_idx += 1;
}
Ok(())
})
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub enum Error {
#[error("object failed to parse file")]
ObjectError(#[from] object::Error),

#[error("failed when attempting to get offset of a UnitHeader")]
HeaderOffsetError,

#[error("failed when attempting to get some CU")]
CUError(String),

Expand Down
2 changes: 1 addition & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) type GimliDwarf<'a> = gimli::Dwarf<R<'a>>;
/// Represents a location of some type/tag in the DWARF information
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Location {
pub header: usize,
pub header: gimli::DebugInfoOffset,
pub offset: gimli::UnitOffset,
}

Expand Down

0 comments on commit a73f511

Please sign in to comment.