Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a couple trait method call optimizations #9332

Merged
merged 4 commits into from
Sep 20, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions src/librustc/middle/trans/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,36 @@ pub fn trans_to_datum(bcx: @mut Block, expr: @ast::Expr) -> DatumBlock {
let target_obj_ty = expr_ty_adjusted(bcx, expr);
debug!("auto_borrow_obj(target=%s)",
target_obj_ty.repr(tcx));

// Extract source store information
let (source_store, source_mutbl) = match ty::get(source_datum.ty).sty {
ty::ty_trait(_, _, s, m, _) => (s, m),
_ => {
bcx.sess().span_bug(
expr.span,
fmt!("auto_borrow_trait_obj expected a trait, found %s",
source_datum.ty.repr(bcx.tcx())));
}
};

// check if any borrowing is really needed or we could reuse the source_datum instead
match ty::get(target_obj_ty).sty {
ty::ty_trait(_, _, ty::RegionTraitStore(target_scope), target_mutbl, _) => {
if target_mutbl == ast::MutImmutable && target_mutbl == source_mutbl {
match source_store {
ty::RegionTraitStore(source_scope) => {
if tcx.region_maps.is_subregion_of(target_scope, source_scope) {
return DatumBlock { bcx: bcx, datum: source_datum };
}
},
_ => {}

};
}
},
_ => {}
}

let scratch = scratch_datum(bcx, target_obj_ty,
"__auto_borrow_obj", false);

Expand All @@ -331,15 +361,6 @@ pub fn trans_to_datum(bcx: @mut Block, expr: @ast::Expr) -> DatumBlock {
// ~T, or &T, depending on source_obj_ty.
let source_data_ptr = GEPi(bcx, source_llval, [0u, abi::trt_field_box]);
let source_data = Load(bcx, source_data_ptr); // always a ptr
let (source_store, source_mutbl) = match ty::get(source_datum.ty).sty {
ty::ty_trait(_, _, s, m, _) => (s, m),
_ => {
bcx.sess().span_bug(
expr.span,
fmt!("auto_borrow_trait_obj expected a trait, found %s",
source_datum.ty.repr(bcx.tcx())));
}
};
let target_data = match source_store {
ty::BoxTraitStore(*) => {
// For deref of @T or @mut T, create a dummy datum and
Expand Down
19 changes: 14 additions & 5 deletions src/librustc/middle/trans/meth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,22 @@ pub fn trans_trait_callee(bcx: @mut Block,
let _icx = push_ctxt("impl::trans_trait_callee");
let mut bcx = bcx;

// make a local copy for trait if needed
let self_ty = expr_ty_adjusted(bcx, self_expr);
let self_scratch = scratch_datum(bcx, self_ty, "__trait_callee", false);
bcx = expr::trans_into(bcx, self_expr, expr::SaveIn(self_scratch.val));
let self_scratch = match ty::get(self_ty).sty {
ty::ty_trait(_, _, ty::RegionTraitStore(*), _, _) => {
unpack_datum!(bcx, expr::trans_to_datum(bcx, self_expr))
}
_ => {
let d = scratch_datum(bcx, self_ty, "__trait_callee", false);
bcx = expr::trans_into(bcx, self_expr, expr::SaveIn(d.val));
// Arrange a temporary cleanup for the object in case something
// should go wrong before the method is actually *invoked*.
d.add_clean(bcx);
d
}
};

// Arrange a temporary cleanup for the object in case something
// should go wrong before the method is actually *invoked*.
self_scratch.add_clean(bcx);

let callee_ty = node_id_type(bcx, callee_id);
trans_trait_callee_from_llval(bcx,
Expand Down
23 changes: 6 additions & 17 deletions src/librustc/middle/trans/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,23 +278,12 @@ impl Type {

pub fn opaque_trait(ctx: &CrateContext, store: ty::TraitStore) -> Type {
let tydesc_ptr = ctx.tydesc_type.ptr_to();
match store {
ty::BoxTraitStore => {
Type::struct_(
[ tydesc_ptr, Type::opaque_box(ctx).ptr_to() ],
false)
}
ty::UniqTraitStore => {
Type::struct_(
[ tydesc_ptr, Type::unique(ctx, &Type::i8()).ptr_to()],
false)
}
ty::RegionTraitStore(*) => {
Type::struct_(
[ tydesc_ptr, Type::i8().ptr_to() ],
false)
}
}
let box_ty = match store {
ty::BoxTraitStore => Type::opaque_box(ctx),
ty::UniqTraitStore => Type::unique(ctx, &Type::i8()),
ty::RegionTraitStore(*) => Type::i8()
};
Type::struct_([tydesc_ptr, box_ty.ptr_to()], false)
}

pub fn kind(&self) -> TypeKind {
Expand Down
1 change: 1 addition & 0 deletions src/test/run-pass/core-run-destroy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fn test_destroy_actually_kills(force: bool) {

#[cfg(windows)]
fn process_exists(pid: libc::pid_t) -> bool {
#[fixed_stack_segment];

use std::libc::types::os::arch::extra::DWORD;
use std::libc::funcs::extra::kernel32::{CloseHandle, GetExitCodeProcess, OpenProcess};
Expand Down