Skip to content

Commit bea9499

Browse files
committed
rustc: Remove root_map entirely
1 parent 4871a16 commit bea9499

File tree

1 file changed

+33
-48
lines changed
  • src/librustc/middle/borrowck

1 file changed

+33
-48
lines changed

src/librustc/middle/borrowck/mod.rs

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use middle::dataflow::DataFlowOperator;
2121
use util::nodemap::{NodeMap, NodeSet};
2222
use util::ppaux::{note_and_explain_region, Repr, UserString};
2323

24-
use std::cell::RefCell;
24+
use std::cell::{Cell, RefCell};
2525
use std::ops::{BitOr, BitAnd};
2626
use std::rc::Rc;
2727
use std::strbuf::StrBuf;
@@ -79,19 +79,39 @@ pub fn check_crate(tcx: &ty::ctxt,
7979
moves_map: &NodeSet,
8080
moved_variables_set: &NodeSet,
8181
capture_map: &moves::CaptureMap,
82-
krate: &ast::Crate)
83-
-> root_map {
82+
krate: &ast::Crate) {
8483
let mut bccx = BorrowckCtxt {
8584
tcx: tcx,
8685
moves_map: moves_map,
8786
moved_variables_set: moved_variables_set,
8887
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+
}
9094
};
9195

9296
visit::walk_crate(&mut bccx, krate, ());
9397

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+
}
95115
}
96116

97117
fn borrowck_item(this: &mut BorrowckCtxt, item: &ast::Item) {
@@ -150,26 +170,16 @@ pub struct BorrowckCtxt<'a> {
150170
moves_map: &'a NodeSet,
151171
moved_variables_set: &'a NodeSet,
152172
capture_map: &'a moves::CaptureMap,
153-
root_map: RefCell<root_map>,
173+
174+
// Statistics:
175+
stats: @BorrowStats
154176
}
155177

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>,
173183
}
174184

175185
pub type BckResult<T> = Result<T, BckError>;
@@ -317,31 +327,6 @@ impl Repr for RestrictionSet {
317327
}
318328
}
319329

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-
345330
///////////////////////////////////////////////////////////////////////////
346331
// Errors
347332

0 commit comments

Comments
 (0)