@@ -7,7 +7,7 @@ use super::ty::{Type, TypeKind};
7
7
use super :: function:: Function ;
8
8
use super :: module:: Module ;
9
9
use super :: annotations:: Annotations ;
10
- use std:: cell:: Cell ;
10
+ use std:: cell:: { Cell , RefCell } ;
11
11
use std:: sync:: atomic:: { AtomicUsize , ATOMIC_USIZE_INIT , Ordering } ;
12
12
use parse:: { ClangItemParser , ClangSubItemParser , ParseError , ParseResult } ;
13
13
use clang;
@@ -97,11 +97,20 @@ impl ItemCanonicalPath for ItemId {
97
97
pub struct Item {
98
98
/// This item's id.
99
99
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().
102
104
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..
104
106
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
+
105
114
/// A doc comment over the item, if any.
106
115
comment : Option < String > ,
107
116
/// Annotations extracted from the doc comment, or the default ones
@@ -129,6 +138,7 @@ impl Item {
129
138
id : id,
130
139
local_id : Cell :: new ( None ) ,
131
140
next_child_local_id : Cell :: new ( 1 ) ,
141
+ canonical_name_cache : RefCell :: new ( None ) ,
132
142
parent_id : parent_id,
133
143
comment : comment,
134
144
annotations : annotations. unwrap_or_default ( ) ,
@@ -920,7 +930,13 @@ impl ItemCanonicalName for Item {
920
930
if let Some ( other_canon_type) = self . annotations . use_instead_of ( ) {
921
931
return other_canon_type. to_owned ( ) ;
922
932
}
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 ( ) ;
924
940
}
925
941
}
926
942
0 commit comments