Skip to content

Commit 955e689

Browse files
committed
ir: Cache the item's canonical name.
1 parent fe3f530 commit 955e689

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/ir/item.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::ty::{Type, TypeKind};
77
use super::function::Function;
88
use super::module::Module;
99
use super::annotations::Annotations;
10-
use std::cell::Cell;
10+
use std::cell::{Cell, RefCell};
1111
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
1212
use parse::{ClangItemParser, ClangSubItemParser, ParseError, ParseResult};
1313
use clang;
@@ -97,11 +97,20 @@ impl ItemCanonicalPath for ItemId {
9797
pub struct Item {
9898
/// This item's id.
9999
id: ItemId,
100-
/// The item's local id, unique only amongst its siblings. Only used
101-
/// for anonymous items. Lazily initialized in local_id().
100+
/// The item's local id, unique only amongst its siblings. Only used for
101+
/// anonymous items.
102+
///
103+
/// Lazily initialized in local_id().
102104
local_id: Cell<Option<usize>>,
103-
/// The next local id to use for a child.
105+
/// The next local id to use for a child..
104106
next_child_local_id: Cell<usize>,
107+
108+
/// A cached copy of the canonical name, as returned by `canonical_name`.
109+
///
110+
/// This is a fairly used operation during codegen so this makes bindgen
111+
/// considerably faster in those cases.
112+
canonical_name_cache: RefCell<Option<String>>,
113+
105114
/// A doc comment over the item, if any.
106115
comment: Option<String>,
107116
/// Annotations extracted from the doc comment, or the default ones
@@ -129,6 +138,7 @@ impl Item {
129138
id: id,
130139
local_id: Cell::new(None),
131140
next_child_local_id: Cell::new(1),
141+
canonical_name_cache: RefCell::new(None),
132142
parent_id: parent_id,
133143
comment: comment,
134144
annotations: annotations.unwrap_or_default(),
@@ -920,7 +930,13 @@ impl ItemCanonicalName for Item {
920930
if let Some(other_canon_type) = self.annotations.use_instead_of() {
921931
return other_canon_type.to_owned();
922932
}
923-
self.real_canonical_name(ctx, ctx.options().enable_cxx_namespaces, false)
933+
if self.canonical_name_cache.borrow().is_none() {
934+
*self.canonical_name_cache.borrow_mut() =
935+
Some(self.real_canonical_name(ctx,
936+
ctx.options().enable_cxx_namespaces,
937+
false));
938+
}
939+
return self.canonical_name_cache.borrow().as_ref().unwrap().clone();
924940
}
925941
}
926942

0 commit comments

Comments
 (0)