Skip to content

Commit

Permalink
Improve performance of BTreeMap::search().last()
Browse files Browse the repository at this point in the history
  • Loading branch information
yurydelendik committed Jul 8, 2019
1 parent af8a5bb commit ff218e0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
23 changes: 19 additions & 4 deletions wasmtime-debug/src/address_transform.rs
Expand Up @@ -4,7 +4,7 @@ use cranelift_entity::{EntityRef, PrimaryMap};
use cranelift_wasm::DefinedFuncIndex;
use gimli::write;
use std::collections::BTreeMap;
use std::ops::Bound::{Included, Unbounded};
use std::iter::FromIterator;
use std::vec::Vec;

pub type GeneratedAddress = usize;
Expand All @@ -26,7 +26,10 @@ pub struct FunctionMap {

#[derive(Debug)]
pub struct AddressTransform {
lookup: BTreeMap<WasmAddress, (SymbolIndex, GeneratedAddress, GeneratedAddress)>,
lookup: Vec<(
WasmAddress,
(SymbolIndex, GeneratedAddress, GeneratedAddress),
)>,
map: PrimaryMap<DefinedFuncIndex, FunctionMap>,
func_ranges: Vec<(usize, usize)>,
}
Expand Down Expand Up @@ -76,6 +79,9 @@ impl AddressTransform {
addresses: fn_map.into_boxed_slice(),
});
}

let lookup = Vec::from_iter(lookup.into_iter());

AddressTransform {
lookup,
map,
Expand All @@ -92,8 +98,17 @@ impl AddressTransform {
// It's normally 0 for debug info without the linked code.
return None;
}
let search = self.lookup.range((Unbounded, Included(addr)));
if let Some((_, value)) = search.last() {
let found = match self.lookup.binary_search_by(|entry| entry.0.cmp(&addr)) {
Ok(i) => Some(&self.lookup[i].1),
Err(i) => {
if i > 0 {
Some(&self.lookup[i - 1].1)
} else {
None
}
}
};
if let Some(value) = found {
return Some(write::Address::Symbol {
symbol: value.0,
addend: value.1 as i64,
Expand Down
23 changes: 14 additions & 9 deletions wasmtime-debug/src/transform.rs
Expand Up @@ -7,7 +7,7 @@ use cranelift_entity::{EntityRef, PrimaryMap};
use cranelift_wasm::DefinedFuncIndex;
use failure::Error;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::ops::Bound::{Included, Unbounded};
use std::iter::FromIterator;

use gimli;

Expand Down Expand Up @@ -401,21 +401,26 @@ where
saved_rows.insert(row.address(), saved_row);
}

let saved_rows = Vec::from_iter(saved_rows.into_iter());

for (i, map) in addr_tr.map() {
let symbol = i.index();
let base_addr = map.offset;
out_program.begin_sequence(Some(write::Address::Symbol { symbol, addend: 0 }));
// TODO track and place function declaration line here
let mut last_address = None;
for addr_map in map.addresses.iter() {
let mut saved_row = saved_rows.get(&addr_map.wasm);
if saved_row.is_none() {
// No direct match -- repeat search with range.
saved_row = saved_rows
.range((Unbounded, Included(addr_map.wasm)))
.last()
.map(|p| p.1);
}
let saved_row =
match saved_rows.binary_search_by(|entry| entry.0.cmp(&addr_map.wasm)) {
Ok(i) => Some(&saved_rows[i].1),
Err(i) => {
if i > 0 {
Some(&saved_rows[i - 1].1)
} else {
None
}
}
};
if let Some(SavedLineProgramRow::Normal {
address,
op_index,
Expand Down

0 comments on commit ff218e0

Please sign in to comment.