Skip to content

Commit

Permalink
Merge pull request #310 from Nadrieril/cache-name
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril authored Aug 14, 2024
2 parents 67ab046 + 972fb92 commit d8a0249
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ pub fn translate<'tcx, 'ctx>(
translate_stack: Default::default(),
cached_defs: Default::default(),
cached_path_elems: Default::default(),
cached_names: Default::default(),
};

// First push all the items in the stack of items to translate.
Expand Down
9 changes: 8 additions & 1 deletion charon/src/bin/charon-driver/translate/translate_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ pub struct TranslateCtx<'tcx, 'ctx> {
/// Cache the `PathElem`s to compute them only once each. It's an `Option` because some
/// `DefId`s (e.g. `extern {}` blocks) don't appear in the `Name`.
pub cached_path_elems: HashMap<DefId, Option<PathElem>>,
/// Cache the names to compute them only once each.
pub cached_names: HashMap<DefId, Name>,
}

/// A translation context for type/global/function bodies.
Expand Down Expand Up @@ -342,6 +344,9 @@ impl<'tcx, 'ctx> TranslateCtx<'tcx, 'ctx> {

/// Retrieve an item name from a [DefId].
pub fn def_id_to_name(&mut self, def_id: DefId) -> Result<Name, Error> {
if let Some(name) = self.cached_names.get(&def_id) {
return Ok(name.clone());
}
trace!("{:?}", def_id);
let tcx = self.tcx;
let span = tcx.def_span(def_id);
Expand Down Expand Up @@ -411,9 +416,11 @@ impl<'tcx, 'ctx> TranslateCtx<'tcx, 'ctx> {
name.push(PathElem::Ident(crate_name, Disambiguator::new(0)));

name.reverse();
let name = Name { name };

trace!("{:?}", name);
Ok(Name { name })
self.cached_names.insert(def_id, name.clone());
Ok(name)
}

pub fn hax_def_id_to_name(&mut self, def_id: &hax::DefId) -> Result<Name, Error> {
Expand Down

0 comments on commit d8a0249

Please sign in to comment.