Speed up function lookup

This commit is contained in:
Robin Avery 2024-02-29 19:39:29 -05:00
parent 90ccc21f0d
commit 51b6438674
No known key found for this signature in database
GPG Key ID: 633B2D5AB640375C
2 changed files with 28 additions and 11 deletions

View File

@ -88,9 +88,13 @@ pub fn run(args: Args) -> Result<()> {
let mut count = 0usize;
for (i, obj) in project_config.objects.iter_mut().enumerate() {
resolve_paths(obj);
if load_obj(&obj.target_path)?
.and_then(|o| find_function(&o, &args.symbol))
.is_some()
if obj
.target_path
.as_deref()
.map(|o| obj::elf::has_function(o, &args.symbol))
.transpose()?
.unwrap_or_default()
{
idx = Some(i);
count += 1;
@ -175,12 +179,6 @@ pub fn run(args: Args) -> Result<()> {
Ok(())
}
fn load_obj(path: &Option<PathBuf>) -> Result<Option<ObjInfo>> {
path.as_deref()
.map(|p| obj::elf::read(p).with_context(|| format!("Loading {}", p.display())))
.transpose()
}
fn find_function(obj: &ObjInfo, name: &str) -> Option<ObjSymbol> {
for section in &obj.sections {
if section.kind != ObjSectionKind::Code {
@ -533,8 +531,16 @@ impl FunctionDiffUi {
}
fn reload(&mut self) -> Result<()> {
let mut target = load_obj(&self.target_path)?;
let mut base = load_obj(&self.base_path)?;
let mut target = self
.target_path
.as_deref()
.map(|p| obj::elf::read(p).with_context(|| format!("Loading {}", p.display())))
.transpose()?;
let mut base = self
.base_path
.as_deref()
.map(|p| obj::elf::read(p).with_context(|| format!("Loading {}", p.display())))
.transpose()?;
let config = diff::DiffObjConfig::default();
diff::diff_objs(&config, target.as_mut(), base.as_mut())?;

View File

@ -394,6 +394,17 @@ pub fn read(obj_path: &Path) -> Result<ObjInfo> {
Ok(result)
}
pub fn has_function(obj_path: &Path, symbol_name: &str) -> Result<bool> {
let data = {
let file = fs::File::open(obj_path)?;
unsafe { memmap2::Mmap::map(&file) }?
};
Ok(File::parse(&*data)?
.symbol_by_name(symbol_name)
.filter(|o| o.kind() == SymbolKind::Text)
.is_some())
}
fn split_meta(obj_file: &File<'_>) -> Result<Option<SplitMeta>> {
Ok(if let Some(section) = obj_file.section_by_name(SPLITMETA_SECTION) {
if section.size() != 0 {