@@ -21,7 +21,7 @@ use middle::dataflow::DataFlowOperator;
21
21
use util:: nodemap:: { NodeMap , NodeSet } ;
22
22
use util:: ppaux:: { note_and_explain_region, Repr , UserString } ;
23
23
24
- use std:: cell:: RefCell ;
24
+ use std:: cell:: { Cell , RefCell } ;
25
25
use std:: ops:: { BitOr , BitAnd } ;
26
26
use std:: rc:: Rc ;
27
27
use std:: strbuf:: StrBuf ;
@@ -79,19 +79,39 @@ pub fn check_crate(tcx: &ty::ctxt,
79
79
moves_map : & NodeSet ,
80
80
moved_variables_set : & NodeSet ,
81
81
capture_map : & moves:: CaptureMap ,
82
- krate : & ast:: Crate )
83
- -> root_map {
82
+ krate : & ast:: Crate ) {
84
83
let mut bccx = BorrowckCtxt {
85
84
tcx : tcx,
86
85
moves_map : moves_map,
87
86
moved_variables_set : moved_variables_set,
88
87
capture_map : capture_map,
89
- root_map : RefCell :: new ( HashMap :: new ( ) )
88
+ stats : @BorrowStats {
89
+ loaned_paths_same : Cell :: new ( 0 ) ,
90
+ loaned_paths_imm : Cell :: new ( 0 ) ,
91
+ stable_paths : Cell :: new ( 0 ) ,
92
+ guaranteed_paths : Cell :: new ( 0 ) ,
93
+ }
90
94
} ;
91
95
92
96
visit:: walk_crate ( & mut bccx, krate, ( ) ) ;
93
97
94
- return bccx. root_map . unwrap ( ) ;
98
+ if tcx. sess . borrowck_stats ( ) {
99
+ println ! ( "--- borrowck stats ---" ) ;
100
+ println ! ( "paths requiring guarantees: {}" ,
101
+ bccx. stats. guaranteed_paths. get( ) ) ;
102
+ println ! ( "paths requiring loans : {}" ,
103
+ make_stat( & bccx, bccx. stats. loaned_paths_same. get( ) ) ) ;
104
+ println ! ( "paths requiring imm loans : {}" ,
105
+ make_stat( & bccx, bccx. stats. loaned_paths_imm. get( ) ) ) ;
106
+ println ! ( "stable paths : {}" ,
107
+ make_stat( & bccx, bccx. stats. stable_paths. get( ) ) ) ;
108
+ }
109
+
110
+ fn make_stat ( bccx : & BorrowckCtxt , stat : uint ) -> ~str {
111
+ let stat_f = stat as f64 ;
112
+ let total = bccx. stats . guaranteed_paths . get ( ) as f64 ;
113
+ format ! ( "{} ({:.0f}%)" , stat , stat_f * 100.0 / total)
114
+ }
95
115
}
96
116
97
117
fn borrowck_item ( this : & mut BorrowckCtxt , item : & ast:: Item ) {
@@ -150,26 +170,16 @@ pub struct BorrowckCtxt<'a> {
150
170
moves_map : & ' a NodeSet ,
151
171
moved_variables_set : & ' a NodeSet ,
152
172
capture_map : & ' a moves:: CaptureMap ,
153
- root_map : RefCell < root_map > ,
173
+
174
+ // Statistics:
175
+ stats : @BorrowStats
154
176
}
155
177
156
- // The keys to the root map combine the `id` of the deref expression
157
- // with the number of types that it is *autodereferenced*. So, for
158
- // example, imagine I have a variable `x: @@@T` and an expression
159
- // `(*x).f`. This will have 3 derefs, one explicit and then two
160
- // autoderefs. These are the relevant `root_map_key` values that could
161
- // appear:
162
- //
163
- // {id:*x, derefs:0} --> roots `x` (type: @@@T, due to explicit deref)
164
- // {id:*x, derefs:1} --> roots `*x` (type: @@T, due to autoderef #1)
165
- // {id:*x, derefs:2} --> roots `**x` (type: @T, due to autoderef #2)
166
- //
167
- // Note that there is no entry with derefs:3---the type of that expression
168
- // is T, which is not a box.
169
- #[ deriving( Eq , TotalEq , Hash ) ]
170
- pub struct root_map_key {
171
- pub id : ast:: NodeId ,
172
- pub derefs : uint
178
+ pub struct BorrowStats {
179
+ loaned_paths_same : Cell < uint > ,
180
+ loaned_paths_imm : Cell < uint > ,
181
+ stable_paths : Cell < uint > ,
182
+ guaranteed_paths : Cell < uint > ,
173
183
}
174
184
175
185
pub type BckResult < T > = Result < T , BckError > ;
@@ -317,31 +327,6 @@ impl Repr for RestrictionSet {
317
327
}
318
328
}
319
329
320
- ///////////////////////////////////////////////////////////////////////////
321
- // Rooting of managed boxes
322
- //
323
- // When we borrow the interior of a managed box, it is sometimes
324
- // necessary to *root* the box, meaning to stash a copy of the box
325
- // somewhere that the garbage collector will find it. This ensures
326
- // that the box is not collected for the lifetime of the borrow.
327
- //
328
- // As part of this rooting, we sometimes also freeze the box at
329
- // runtime, meaning that we dynamically detect when the box is
330
- // borrowed in incompatible ways.
331
- //
332
- // Both of these actions are driven through the `root_map`, which maps
333
- // from a node to the dynamic rooting action that should be taken when
334
- // that node executes. The node is identified through a
335
- // `root_map_key`, which pairs a node-id and a deref count---the
336
- // problem is that sometimes the box that needs to be rooted is only
337
- // uncovered after a certain number of auto-derefs.
338
-
339
- pub struct RootInfo {
340
- pub scope : ast:: NodeId ,
341
- }
342
-
343
- pub type root_map = HashMap < root_map_key , RootInfo > ;
344
-
345
330
///////////////////////////////////////////////////////////////////////////
346
331
// Errors
347
332
0 commit comments