Skip to content

Commit

Permalink
Optimise a particularly clown shoes example of DST codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Aug 26, 2014
1 parent 52ef462 commit 08364a4
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions src/librustc/middle/trans/expr.rs
Expand Up @@ -193,24 +193,46 @@ fn apply_adjustments<'a>(bcx: &'a Block<'a>,
datum = unpack_datum!(bcx, add_env(bcx, expr, datum));
}
AutoDerefRef(ref adj) => {
// Extracting a value from a box counts as a deref, but if we are
// just converting Box<[T, ..n]> to Box<[T]> we aren't really doing
// a deref (and wouldn't if we could treat Box like a normal struct).
let autoderefs = match adj.autoref {
Some(ty::AutoUnsizeUniq(..)) => adj.autoderefs - 1,
_ => adj.autoderefs
let (autoderefs, use_autoref) = match adj.autoref {
// Extracting a value from a box counts as a deref, but if we are
// just converting Box<[T, ..n]> to Box<[T]> we aren't really doing
// a deref (and wouldn't if we could treat Box like a normal struct).
Some(ty::AutoUnsizeUniq(..)) => (adj.autoderefs - 1, true),
// We are a bit paranoid about adjustments and thus might have a re-
// borrow here which merely derefs and then refs again (it might have
// a different region or mutability, but we don't care here. It might
// also be just in case we need to unsize. But if there are no nested
// adjustments then it should be a no-op).
Some(ty::AutoPtr(_, _, None)) if adj.autoderefs == 1 => {
match ty::get(datum.ty).sty {
// Don't skip a conversion from Box<T> to &T, etc.
ty::ty_rptr(..) => {
let method_call = MethodCall::autoderef(expr.id, adj.autoderefs-1);
let method = bcx.tcx().method_map.borrow().find(&method_call).is_some();
if method {
// Don't skip an overloaded deref.
(adj.autoderefs, true)
} else {
(adj.autoderefs - 1, false)
}
}
_ => (adj.autoderefs, true),
}
}
_ => (adj.autoderefs, true)
};

if autoderefs > 0 {
let lval = unpack_datum!(bcx,
datum.to_lvalue_datum(bcx, "auto_deref", expr.id));

// Schedule cleanup.
let lval = unpack_datum!(bcx, datum.to_lvalue_datum(bcx, "auto_deref", expr.id));
datum = unpack_datum!(
bcx, deref_multiple(bcx, expr, lval.to_expr_datum(), autoderefs));
}

match adj.autoref {
Some(ref a) => {
// (You might think there is a more elegant way to do this than a
// use_autoref bool, but then you remember that the borrow checker exists).
match (use_autoref, &adj.autoref) {
(true, &Some(ref a)) => {
datum = unpack_datum!(bcx, apply_autoref(a,
bcx,
expr,
Expand All @@ -221,7 +243,7 @@ fn apply_adjustments<'a>(bcx: &'a Block<'a>,
}
}
debug!("after adjustments, datum={}", datum.to_string(bcx.ccx()));
return DatumBlock {bcx: bcx, datum: datum};
return DatumBlock::new(bcx, datum);

fn apply_autoref<'a>(autoref: &ty::AutoRef,
bcx: &'a Block<'a>,
Expand Down

5 comments on commit 08364a4

@bors
Copy link
Contributor

@bors bors commented on 08364a4 Aug 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pnkfelix
at nrc@08364a4

@bors
Copy link
Contributor

@bors bors commented on 08364a4 Aug 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging nick29581/rust/coerce = 08364a4 into auto

@bors
Copy link
Contributor

@bors bors commented on 08364a4 Aug 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nick29581/rust/coerce = 08364a4 merged ok, testing candidate = 7932b71

@bors
Copy link
Contributor

@bors bors commented on 08364a4 Aug 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 7932b71

Please sign in to comment.