@@ -39,6 +39,11 @@ pub struct LinkageView<'state> {
39
39
/// Cache of past package addresses that have been the link context -- if a package is in this
40
40
/// set, then we will not try to load its type origin table when setting it as a context (again).
41
41
past_contexts : RefCell < HashSet < ObjectID > > ,
42
+ /// A mapping from the defining ID of a type to a valid linkage context that has already been
43
+ /// set. This is used to avoid double-loading packages for types that are already in the
44
+ /// cache. Note that there may be multiple "valid" resolutions for a given defining ID, but we
45
+ /// only care about having one of them so first one wins.
46
+ context_resolution_cache : RefCell < BTreeMap < ObjectID , ObjectID > > ,
42
47
}
43
48
44
49
#[ derive( Debug ) ]
@@ -57,6 +62,7 @@ impl<'state> LinkageView<'state> {
57
62
linkage_info : None ,
58
63
type_origin_cache : RefCell :: new ( HashMap :: new ( ) ) ,
59
64
past_contexts : RefCell :: new ( HashSet :: new ( ) ) ,
65
+ context_resolution_cache : RefCell :: new ( BTreeMap :: new ( ) ) ,
60
66
}
61
67
}
62
68
@@ -126,6 +132,16 @@ impl<'state> LinkageView<'state> {
126
132
package : defining_id,
127
133
} in context. type_origin_table ( )
128
134
{
135
+ if !self
136
+ . context_resolution_cache
137
+ . borrow ( )
138
+ . contains_key ( defining_id)
139
+ {
140
+ self . context_resolution_cache
141
+ . borrow_mut ( )
142
+ . insert ( * defining_id, storage_id) ;
143
+ }
144
+
129
145
let Ok ( module_name) = Identifier :: from_str ( module_name) else {
130
146
invariant_violation ! ( "Module name isn't an identifier: {module_name}" ) ;
131
147
} ;
@@ -141,6 +157,13 @@ impl<'state> LinkageView<'state> {
141
157
Ok ( runtime_id)
142
158
}
143
159
160
+ pub fn linkage_context_for_defining_id ( & self , defining_id : ObjectID ) -> Option < ObjectID > {
161
+ self . context_resolution_cache
162
+ . borrow ( )
163
+ . get ( & defining_id)
164
+ . cloned ( )
165
+ }
166
+
144
167
pub fn original_package_id ( & self ) -> Option < AccountAddress > {
145
168
Some ( self . linkage_info . as_ref ( ) ?. runtime_id )
146
169
}
0 commit comments