Skip to content

Commit

Permalink
Start working with boxed slices earlier
Browse files Browse the repository at this point in the history
Currently it is the SymbolTableCache type's job to convert a Vec of
symbols into a boxed slice. Let's move this task earlier, which will
pave the way for upcoming changes but it also means one less potential
copy of a Vec while we convert it down into a boxed slice.

Signed-off-by: Daniel Müller <deso@posteo.net>
  • Loading branch information
d-e-s-o committed Dec 5, 2024
1 parent 6ba35c9 commit f6eb4ad
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/elf/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ struct SymbolTableCache<'mmap> {
}

impl<'mmap> SymbolTableCache<'mmap> {
fn new(syms: Vec<&'mmap Elf64_Sym>, strs: &'mmap [u8]) -> Self {
fn new(syms: Box<[&'mmap Elf64_Sym]>, strs: &'mmap [u8]) -> Self {
Self {
syms: syms.into_boxed_slice(),
syms,
strs,
str2sym: OnceCell::new(),
}
Expand Down Expand Up @@ -464,12 +464,12 @@ impl<'mmap> Cache<'mmap> {
Ok(None)
}

fn parse_syms(&self, section: &str) -> Result<Vec<&'mmap Elf64_Sym>> {
fn parse_syms(&self, section: &str) -> Result<Box<[&'mmap Elf64_Sym]>> {
let idx = if let Some(idx) = self.find_section(section)? {
idx
} else {
// The symbol table does not exists. Fake an empty one.
return Ok(Vec::new())
return Ok(Box::new([]))
};
let mut syms = self.section_data(idx)?;

Expand All @@ -483,15 +483,15 @@ impl<'mmap> Cache<'mmap> {
// Short-circuit if there are no symbols. The data may not actually be
// properly aligned in this case either, so don't attempt to even read.
if count == 0 {
return Ok(Vec::new())
return Ok(Box::new([]))
}
let mut syms = syms
.read_pod_slice_ref::<Elf64_Sym>(count)
.ok_or_invalid_data(|| format!("failed to read {section} symbol table contents"))?
.iter()
// Filter out any symbols that we do not support.
.filter(|sym| sym.matches(SymType::Undefined))
.collect::<Vec<&Elf64_Sym>>();
.collect::<Box<[&Elf64_Sym]>>();
// Order symbols by address and those with equal address descending by
// size.
let () = syms.sort_by(|sym1, sym2| {
Expand Down

0 comments on commit f6eb4ad

Please sign in to comment.