diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2012-03-02 20:06:08 -0800 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2012-03-05 16:47:52 -0800 |
| commit | 3269a4043ce3ece87708fc447b37b330661a5ed0 (patch) | |
| tree | 69203e69fa11d00f5b8e787902072880b0ac1ec6 /src/libcore | |
| parent | 0416a946b7fa256d103beab6767a88759a514f46 (diff) | |
| download | rust-3269a4043ce3ece87708fc447b37b330661a5ed0.tar.gz rust-3269a4043ce3ece87708fc447b37b330661a5ed0.zip | |
rewrite vec to be more unsafe, more inlined
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/ptr.rs | 1 | ||||
| -rw-r--r-- | src/libcore/uint.rs | 1 | ||||
| -rw-r--r-- | src/libcore/vec.rs | 19 |
3 files changed, 19 insertions, 2 deletions
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index bb48727230c..d6dda41c3a4 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -32,6 +32,7 @@ Function: offset Calculate the offset from a pointer */ +#[inline(always)] fn offset<T>(ptr: *T, count: uint) -> *T { ret rusti::ptr_offset(ptr, count); } diff --git a/src/libcore/uint.rs b/src/libcore/uint.rs index 230c6a2e90c..d337746f532 100644 --- a/src/libcore/uint.rs +++ b/src/libcore/uint.rs @@ -131,6 +131,7 @@ Function: range Iterate over the range [`lo`..`hi`) */ +#[inline(always)] fn range(lo: uint, hi: uint, it: fn(uint)) { let i = lo; while i < hi { it(i); i += 1u; } diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index 27163e9f190..0baf5a34751 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -78,6 +78,7 @@ Function: len Returns the length of a vector */ +#[inline(always)] pure fn len<T>(v: [const T]) -> uint { unchecked { rusti::vec_len(v) } } /* @@ -885,9 +886,17 @@ Iterates over vector `v` and, for each element, calls function `f` with the element's value. */ -#[inline] +#[inline(always)] fn iter<T>(v: [const T], f: fn(T)) { - iteri(v) { |_i, v| f(v) } + unsafe { + let mut n = vec::len(v); + let mut p = unsafe::to_ptr(v); + while n > 0u { + f(*p); + p = ptr::offset(p, 1u); + n -= 1u; + } + } } /* @@ -896,6 +905,7 @@ Function: iter2 Iterates over two vectors in parallel */ +#[inline] fn iter2<U, T>(v: [ U], v2: [const T], f: fn(U, T)) { let i = 0; for elt in v { f(elt, v2[i]); i += 1; } @@ -909,6 +919,7 @@ Iterates over a vector's elements and indexes Iterates over vector `v` and, for each element, calls function `f` with the element's value and index. */ +#[inline(always)] fn iteri<T>(v: [const T], f: fn(uint, T)) { let i = 0u, l = len(v); while i < l { f(i, v[i]); i += 1u; } @@ -1001,6 +1012,7 @@ fn as_mut_buf<E,T>(v: [mutable E], f: fn(*mutable E) -> T) -> T unsafe { } impl vec_len<T> for [T] { + #[inline(always)] fn len() -> uint { len(self) } } @@ -1020,6 +1032,7 @@ mod unsafe { ptr - An unsafe pointer to a buffer of `T` elts - The number of elements in the buffer */ + #[inline(always)] unsafe fn from_buf<T>(ptr: *T, elts: uint) -> [T] { ret rustrt::vec_from_buf_shared(sys::get_type_desc::<T>(), ptr, elts); @@ -1034,6 +1047,7 @@ mod unsafe { modifing its buffers, so it is up to the caller to ensure that the vector is actually the specified size. */ + #[inline(always)] unsafe fn set_len<T>(&v: [const T], new_len: uint) { let repr: **vec_repr = ::unsafe::reinterpret_cast(addr_of(v)); (**repr).fill = new_len * sys::size_of::<T>(); @@ -1050,6 +1064,7 @@ mod unsafe { Modifying the vector may cause its buffer to be reallocated, which would also make any pointers to it invalid. */ + #[inline(always)] unsafe fn to_ptr<T>(v: [const T]) -> *T { let repr: **vec_repr = ::unsafe::reinterpret_cast(addr_of(v)); ret ::unsafe::reinterpret_cast(addr_of((**repr).data)); |
