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 #9297

Closed
wants to merge 13 commits into from
Closed
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
4 changes: 2 additions & 2 deletions src/libextra/num/bigint.rs
Original file line number Diff line number Diff line change
@@ -115,8 +115,8 @@ impl TotalOrd for BigUint {
if s_len > o_len { return Greater; }

for (&self_i, &other_i) in self.data.rev_iter().zip(other.data.rev_iter()) {
cond!((self_i < other_i) { return Less; }
(self_i > other_i) { return Greater; })
if self_i < other_i { return Less; }
if self_i > other_i { return Greater; }
}
return Equal;
}
39 changes: 30 additions & 9 deletions src/librustc/middle/trans/expr.rs
Original file line number Diff line number Diff line change
@@ -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);

@@ -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
19 changes: 14 additions & 5 deletions src/librustc/middle/trans/meth.rs
Original file line number Diff line number Diff line change
@@ -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,
23 changes: 6 additions & 17 deletions src/librustc/middle/trans/type_.rs
Original file line number Diff line number Diff line change
@@ -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 {
16 changes: 15 additions & 1 deletion src/libstd/borrow.rs
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ pub fn to_uint<T>(thing: &T) -> uint {
/// Determine if two borrowed pointers point to the same thing.
#[inline]
pub fn ref_eq<'a, 'b, T>(thing: &'a T, other: &'b T) -> bool {
to_uint(thing) == to_uint(other)
(thing as *T) == (other as *T)
}

// Equality for region pointers
@@ -70,3 +70,17 @@ impl<'self, T: TotalEq> TotalEq for &'self T {
#[inline]
fn equals(&self, other: & &'self T) -> bool { (**self).equals(*other) }
}

#[cfg(test)]
mod tests {
use super::ref_eq;

#[test]
fn test_ref_eq() {
let x = 1;
let y = 1;

assert!(ref_eq(&x, &x));
assert!(!ref_eq(&x, &y));
}
}
24 changes: 12 additions & 12 deletions src/libstd/char.rs
Original file line number Diff line number Diff line change
@@ -281,11 +281,11 @@ pub fn escape_unicode(c: char, f: &fn(char)) {
// avoid calling str::to_str_radix because we don't really need to allocate
// here.
f('\\');
let pad = cond!(
(c <= '\xff') { f('x'); 2 }
(c <= '\uffff') { f('u'); 4 }
_ { f('U'); 8 }
);
let pad = match () {
_ if c <= '\xff' => { f('x'); 2 }
_ if c <= '\uffff' => { f('u'); 4 }
_ => { f('U'); 8 }
};
for offset in range_step::<i32>(4 * (pad - 1), -1, -4) {
unsafe {
match ((c as i32) >> offset) & 0xf {
@@ -329,13 +329,13 @@ pub fn len_utf8_bytes(c: char) -> uint {
static MAX_FOUR_B: uint = 2097152u;

let code = c as uint;
cond!(
(code < MAX_ONE_B) { 1u }
(code < MAX_TWO_B) { 2u }
(code < MAX_THREE_B) { 3u }
(code < MAX_FOUR_B) { 4u }
_ { fail!("invalid character!") }
)
match () {
_ if code < MAX_ONE_B => 1u,
_ if code < MAX_TWO_B => 2u,
_ if code < MAX_THREE_B => 3u,
_ if code < MAX_FOUR_B => 4u,
_ => fail!("invalid character!"),
}
}

impl ToStr for char {
36 changes: 18 additions & 18 deletions src/libstd/num/f32.rs
Original file line number Diff line number Diff line change
@@ -206,35 +206,35 @@ impl Orderable for f32 {
/// Returns `NaN` if either of the numbers are `NaN`.
#[inline]
fn min(&self, other: &f32) -> f32 {
cond!(
(self.is_NaN()) { *self }
(other.is_NaN()) { *other }
(*self < *other) { *self }
_ { *other }
)
match () {
_ if self.is_NaN() => *self,
_ if other.is_NaN() => *other,
_ if *self < *other => *self,
_ => *other,
}
}

/// Returns `NaN` if either of the numbers are `NaN`.
#[inline]
fn max(&self, other: &f32) -> f32 {
cond!(
(self.is_NaN()) { *self }
(other.is_NaN()) { *other }
(*self > *other) { *self }
_ { *other }
)
match () {
_ if self.is_NaN() => *self,
_ if other.is_NaN() => *other,
_ if *self > *other => *self,
_ => *other,
}
}

/// Returns the number constrained within the range `mn <= self <= mx`.
/// If any of the numbers are `NaN` then `NaN` is returned.
#[inline]
fn clamp(&self, mn: &f32, mx: &f32) -> f32 {
cond!(
(self.is_NaN()) { *self }
(!(*self <= *mx)) { *mx }
(!(*self >= *mn)) { *mn }
_ { *self }
)
match () {
_ if self.is_NaN() => *self,
_ if !(*self <= *mx) => *mx,
_ if !(*self >= *mn) => *mn,
_ => *self,
}
}
}

36 changes: 18 additions & 18 deletions src/libstd/num/f64.rs
Original file line number Diff line number Diff line change
@@ -229,35 +229,35 @@ impl Orderable for f64 {
/// Returns `NaN` if either of the numbers are `NaN`.
#[inline]
fn min(&self, other: &f64) -> f64 {
cond!(
(self.is_NaN()) { *self }
(other.is_NaN()) { *other }
(*self < *other) { *self }
_ { *other }
)
match () {
_ if self.is_NaN() => *self,
_ if other.is_NaN() => *other,
_ if *self < *other => *self,
_ => *other,
}
}

/// Returns `NaN` if either of the numbers are `NaN`.
#[inline]
fn max(&self, other: &f64) -> f64 {
cond!(
(self.is_NaN()) { *self }
(other.is_NaN()) { *other }
(*self > *other) { *self }
_ { *other }
)
match () {
_ if self.is_NaN() => *self,
_ if other.is_NaN() => *other,
_ if *self > *other => *self,
_ => *other,
}
}

/// Returns the number constrained within the range `mn <= self <= mx`.
/// If any of the numbers are `NaN` then `NaN` is returned.
#[inline]
fn clamp(&self, mn: &f64, mx: &f64) -> f64 {
cond!(
(self.is_NaN()) { *self }
(!(*self <= *mx)) { *mx }
(!(*self >= *mn)) { *mn }
_ { *self }
)
match () {
_ if self.is_NaN() => *self,
_ if !(*self <= *mx) => *mx,
_ if !(*self >= *mn) => *mn,
_ => *self,
}
}
}

10 changes: 5 additions & 5 deletions src/libstd/num/uint_macros.rs
Original file line number Diff line number Diff line change
@@ -70,11 +70,11 @@ impl Orderable for $T {
/// Returns the number constrained within the range `mn <= self <= mx`.
#[inline]
fn clamp(&self, mn: &$T, mx: &$T) -> $T {
cond!(
(*self > *mx) { *mx }
(*self < *mn) { *mn }
_ { *self }
)
match () {
_ if (*self > *mx) => *mx,
_ if (*self < *mn) => *mn,
_ => *self,
}
}
}

36 changes: 0 additions & 36 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
@@ -894,42 +894,6 @@ pub fn std_macros() -> @str {
}
)

//
// A scheme-style conditional that helps to improve code clarity in some instances when
// the `if`, `else if`, and `else` keywords obscure predicates undesirably.
//
// # Example
//
// ~~~
// let clamped =
// if x > mx { mx }
// else if x < mn { mn }
// else { x };
// ~~~
//
// Using `cond!`, the above could be written as:
//
// ~~~
// let clamped = cond!(
// (x > mx) { mx }
// (x < mn) { mn }
// _ { x }
// );
// ~~~
//
// The optional default case is denoted by `_`.
//
macro_rules! cond (
( $(($pred:expr) $body:block)+ _ $default:block ) => (
$(if $pred $body else)+
$default
);
// for if the default case was ommitted
( $(($pred:expr) $body:block)+ ) => (
$(if $pred $body)else+
);
)

// NOTE(acrichto): start removing this after the next snapshot
macro_rules! printf (
($arg:expr) => (
23 changes: 0 additions & 23 deletions src/test/run-pass/cond-macro-no-default.rs

This file was deleted.

23 changes: 0 additions & 23 deletions src/test/run-pass/cond-macro.rs

This file was deleted.

1 change: 1 addition & 0 deletions src/test/run-pass/core-run-destroy.rs
Original file line number Diff line number Diff line change
@@ -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};