Add #[inline(never)], and also fixed inlining on vec::push

This commit is contained in:
Eric Holk
2012-07-25 17:29:34 -07:00
committed by Paul Stansifer
parent f4c093c4af
commit 0a8b794a3a
6 changed files with 30 additions and 10 deletions

View File

@@ -108,6 +108,11 @@ impl private_methods<A> for dvec<A> {
// almost nothing works without the copy bound due to limitations
// around closures.
impl extensions<A> for dvec<A> {
/// Reserves space for N elements
fn reserve(count: uint) {
vec::reserve(self.data, count)
}
/**
* Swaps out the current vector and hands it off to a user-provided
* function `f`. The function should transform it however is desired

View File

@@ -509,10 +509,7 @@ fn push<T>(&v: ~[const T], +initval: T) {
let repr: **unsafe::vec_repr = ::unsafe::reinterpret_cast(addr_of(v));
let fill = (**repr).fill;
if (**repr).alloc > fill {
(**repr).fill += sys::size_of::<T>();
let p = ptr::addr_of((**repr).data);
let p = ptr::offset(p, fill) as *mut T;
rusti::move_val_init(*p, initval);
push_fast(v, initval);
}
else {
push_slow(v, initval);
@@ -520,9 +517,21 @@ fn push<T>(&v: ~[const T], +initval: T) {
}
}
// This doesn't bother to make sure we have space.
#[inline(always)] // really pretty please
unsafe fn push_fast<T>(&v: ~[const T], +initval: T) {
let repr: **unsafe::vec_repr = ::unsafe::reinterpret_cast(addr_of(v));
let fill = (**repr).fill;
(**repr).fill += sys::size_of::<T>();
let p = ptr::addr_of((**repr).data);
let p = ptr::offset(p, fill) as *mut T;
rusti::move_val_init(*p, initval);
}
#[inline(never)]
fn push_slow<T>(&v: ~[const T], +initval: T) {
reserve_at_least(v, v.len() + 1u);
push(v, initval);
unsafe { push_fast(v, initval) }
}
// Unchecked vector indexing
@@ -644,7 +653,6 @@ fn grow_fn<T>(&v: ~[const T], n: uint, op: init_op<T>) {
* of the vector, expands the vector by replicating `initval` to fill the
* intervening space.
*/
#[inline(always)]
fn grow_set<T: copy>(&v: ~[mut T], index: uint, initval: T, val: T) {
if index >= len(v) { grow(v, index - len(v) + 1u, initval); }
v[index] = val;

View File

@@ -17,7 +17,8 @@ enum smallintmap<T:copy> {
/// Create a smallintmap
fn mk<T: copy>() -> smallintmap<T> {
ret smallintmap_(@{v: dvec()});
let v = dvec();
ret smallintmap_(@{v: v});
}
/**
@@ -26,6 +27,7 @@ fn mk<T: copy>() -> smallintmap<T> {
*/
#[inline(always)]
fn insert<T: copy>(self: smallintmap<T>, key: uint, val: T) {
//#error("inserting key %?", key);
self.v.grow_set_elt(key, none, some(val));
}

View File

@@ -364,7 +364,8 @@ fn foreign_abi(attrs: ~[ast::attribute]) -> either<~str, ast::foreign_abi> {
enum inline_attr {
ia_none,
ia_hint,
ia_always
ia_always,
ia_never,
}
/// True if something like #[inline] is found in the list of attrs.
@@ -376,6 +377,9 @@ fn find_inline_attr(attrs: ~[ast::attribute]) -> inline_attr {
ast::meta_list(@~"inline", items) {
if !vec::is_empty(find_meta_items_by_name(items, ~"always")) {
ia_always
} else if !vec::is_empty(
find_meta_items_by_name(items, ~"never")) {
ia_never
} else {
ia_hint
}

View File

@@ -545,7 +545,7 @@ fn purity_fn_family(p: purity) -> char {
fn should_inline(attrs: ~[attribute]) -> bool {
alt attr::find_inline_attr(attrs) {
attr::ia_none { false }
attr::ia_none | attr::ia_never { false }
attr::ia_hint | attr::ia_always { true }
}
}

View File

@@ -456,6 +456,7 @@ fn set_inline_hint_if_appr(attrs: ~[ast::attribute],
alt attr::find_inline_attr(attrs) {
attr::ia_hint { set_inline_hint(llfn); }
attr::ia_always { set_always_inline(llfn); }
attr::ia_never { set_no_inline(llfn); }
attr::ia_none { /* fallthrough */ }
}
}