Skip to content

Commit

Permalink
auto merge of #8121 : thestinger/rust/offset, r=alexcrichton
Browse files Browse the repository at this point in the history
Closes #8118, #7136

~~~rust
extern mod extra;

use std::vec;
use std::ptr;

fn bench_from_elem(b: &mut extra::test::BenchHarness) {
    do b.iter {
        let v: ~[u8] = vec::from_elem(1024, 0u8);
    }
}

fn bench_set_memory(b: &mut extra::test::BenchHarness) {
    do b.iter {
        let mut v: ~[u8] = vec::with_capacity(1024);
        unsafe {
            let vp = vec::raw::to_mut_ptr(v);
            ptr::set_memory(vp, 0, 1024);
            vec::raw::set_len(&mut v, 1024);
        }
    }
}

fn bench_vec_repeat(b: &mut extra::test::BenchHarness) {
    do b.iter {
        let v: ~[u8] = ~[0u8, ..1024];
    }
}
~~~

Before:

    test bench_from_elem ... bench: 415 ns/iter (+/- 17)
    test bench_set_memory ... bench: 85 ns/iter (+/- 4)
    test bench_vec_repeat ... bench: 83 ns/iter (+/- 3)

After:

    test bench_from_elem ... bench: 84 ns/iter (+/- 2)
    test bench_set_memory ... bench: 84 ns/iter (+/- 5)
    test bench_vec_repeat ... bench: 84 ns/iter (+/- 3)
  • Loading branch information
bors committed Jul 30, 2013
2 parents 7fc8c14 + ef870d3 commit 576f395
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 80 deletions.
8 changes: 4 additions & 4 deletions src/libextra/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ unsafe fn destroy_chunk(chunk: &Chunk) {
let fill = chunk.fill;

while idx < fill {
let tydesc_data: *uint = transmute(ptr::offset(buf, idx));
let tydesc_data: *uint = transmute(ptr::offset(buf, idx as int));
let (tydesc, is_done) = un_bitpack_tydesc_ptr(*tydesc_data);
let (size, align) = ((*tydesc).size, (*tydesc).align);

Expand All @@ -127,7 +127,7 @@ unsafe fn destroy_chunk(chunk: &Chunk) {
//debug!("freeing object: idx = %u, size = %u, align = %u, done = %b",
// start, size, align, is_done);
if is_done {
((*tydesc).drop_glue)(ptr::offset(buf, start) as *i8);
((*tydesc).drop_glue)(ptr::offset(buf, start as int) as *i8);
}

// Find where the next tydesc lives
Expand Down Expand Up @@ -176,7 +176,7 @@ impl Arena {
//debug!("idx = %u, size = %u, align = %u, fill = %u",
// start, n_bytes, align, head.fill);

ptr::offset(vec::raw::to_ptr(this.pod_head.data), start)
ptr::offset(vec::raw::to_ptr(this.pod_head.data), start as int)
}
}

Expand Down Expand Up @@ -233,7 +233,7 @@ impl Arena {
// start, n_bytes, align, head.fill);

let buf = vec::raw::to_ptr(self.head.data);
return (ptr::offset(buf, tydesc_start), ptr::offset(buf, start));
return (ptr::offset(buf, tydesc_start as int), ptr::offset(buf, start as int));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libextra/c_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub unsafe fn c_vec_with_dtor<T>(base: *mut T, len: uint, dtor: @fn())
pub fn get<T:Clone>(t: CVec<T>, ofs: uint) -> T {
assert!(ofs < len(t));
return unsafe {
(*ptr::mut_offset(t.base, ofs)).clone()
(*ptr::mut_offset(t.base, ofs as int)).clone()
};
}

Expand All @@ -133,7 +133,7 @@ pub fn get<T:Clone>(t: CVec<T>, ofs: uint) -> T {
*/
pub fn set<T>(t: CVec<T>, ofs: uint, v: T) {
assert!(ofs < len(t));
unsafe { *ptr::mut_offset(t.base, ofs) = v };
unsafe { *ptr::mut_offset(t.base, ofs as int) = v };
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/ebml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub mod reader {

unsafe {
let (ptr, _): (*u8, uint) = transmute(data);
let ptr = offset(ptr, start);
let ptr = offset(ptr, start as int);
let ptr: *i32 = transmute(ptr);
let val = bswap32(*ptr);
let val: u32 = transmute(val);
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/par.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn map_slices<A:Clone + Send,B:Clone + Send>(
let f = do future_spawn() || {
unsafe {
let len = end - base;
let slice = (ptr::offset(p, base),
let slice = (ptr::offset(p, base as int),
len * sys::size_of::<A>());
info!("pre-slice: %?", (base, slice));
let slice : &[A] =
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/metadata/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ fn get_metadata_section(os: os,
}
if !version_ok { return None; }

let cvbuf1 = ptr::offset(cvbuf, vlen);
let cvbuf1 = ptr::offset(cvbuf, vlen as int);
debug!("inflating %u bytes of compressed metadata",
csz - vlen);
do vec::raw::buf_as_slice(cvbuf1, csz-vlen) |bytes| {
Expand Down
5 changes: 5 additions & 0 deletions src/librustc/middle/trans/foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,11 @@ pub fn trans_intrinsic(ccx: @mut CrateContext,
let morestack_addr = PointerCast(bcx, morestack_addr, Type::nil().ptr_to());
Ret(bcx, morestack_addr);
}
"offset" => {
let ptr = get_param(decl, first_real_arg);
let offset = get_param(decl, first_real_arg + 1);
Ret(bcx, GEP(bcx, ptr, [offset]));
}
"memcpy32" => memcpy_intrinsic(bcx, "llvm.memcpy.p0i8.p0i8.i32", substs.tys[0], 32),
"memcpy64" => memcpy_intrinsic(bcx, "llvm.memcpy.p0i8.p0i8.i64", substs.tys[0], 64),
"memmove32" => memcpy_intrinsic(bcx, "llvm.memmove.p0i8.p0i8.i32", substs.tys[0], 32),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/type_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub fn type_uses_for(ccx: @mut CrateContext, fn_id: def_id, n_tps: uint)
"visit_tydesc" | "forget" | "frame_address" |
"morestack_addr" => 0,

"memcpy32" | "memcpy64" | "memmove32" | "memmove64" |
"offset" | "memcpy32" | "memcpy64" | "memmove32" | "memmove64" |
"memset32" | "memset64" => use_repr,

"sqrtf32" | "sqrtf64" | "powif32" | "powif64" |
Expand Down
14 changes: 14 additions & 0 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3606,6 +3606,20 @@ pub fn check_intrinsic_type(ccx: @mut CrateCtxt, it: @ast::foreign_item) {
"morestack_addr" => {
(0u, ~[], ty::mk_nil_ptr(ccx.tcx))
}
"offset" => {
(1,
~[
ty::mk_ptr(tcx, ty::mt {
ty: param(ccx, 0),
mutbl: ast::m_imm
}),
ty::mk_int()
],
ty::mk_ptr(tcx, ty::mt {
ty: param(ccx, 0),
mutbl: ast::m_imm
}))
}
"memcpy32" => {
(1,
~[
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/at_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ pub mod raw {
let repr: *mut Box<Vec<T>> = cast::transmute_copy(v);
let amt = v.len();
(*repr).data.fill += sys::size_of::<T>();
let p = ptr::offset(&(*repr).data.data as *T, amt) as *mut T;
let p = ptr::offset(&(*repr).data.data as *T, amt as int) as *mut T;
move_val_init(&mut(*p), initval);
}

Expand Down
12 changes: 6 additions & 6 deletions src/libstd/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub mod rustrt {
}

unsafe fn bump<T, U>(ptr: *T, count: uint) -> *U {
return ptr::offset(ptr, count) as *U;
return ptr::offset(ptr, count as int) as *U;
}

unsafe fn align_to_pointer<T>(ptr: *T) -> *T {
Expand Down Expand Up @@ -140,11 +140,11 @@ unsafe fn _walk_safe_point(fp: *Word, sp: SafePoint, visitor: Visitor) -> bool {
// Stack roots
let mut sri = 0;
while sri < num_stack_roots {
if *ptr::offset(addrspaces, sri) >= 1 {
if *ptr::offset(addrspaces, sri as int) >= 1 {
let root =
ptr::offset(fp_bytes, *ptr::offset(stack_roots, sri) as Word)
ptr::offset(fp_bytes, *ptr::offset(stack_roots, sri as int) as int)
as **Word;
let tydescpp = ptr::offset(tydescs, sri);
let tydescpp = ptr::offset(tydescs, sri as int);
let tydesc = if ptr::is_not_null(tydescpp) &&
ptr::is_not_null(*tydescpp) {
**tydescpp
Expand All @@ -159,7 +159,7 @@ unsafe fn _walk_safe_point(fp: *Word, sp: SafePoint, visitor: Visitor) -> bool {
// Register roots
let mut rri = 0;
while rri < num_reg_roots {
if *ptr::offset(addrspaces, num_stack_roots + rri) == 1 {
if *ptr::offset(addrspaces, (num_stack_roots + rri) as int) == 1 {
// FIXME(#2997): Need to find callee saved registers on the stack.
}
rri += 1;
Expand Down Expand Up @@ -246,7 +246,7 @@ unsafe fn _walk_gc_roots(mem: Memory, sentinel: **Word, visitor: Visitor) -> boo
// for this second return address is 3 greater than the
// return address for morestack.
let ret_offset = if boundary { 4 } else { 1 };
last_ret = *ptr::offset(frame.fp, ret_offset) as *Word;
last_ret = *ptr::offset(frame.fp, ret_offset as int) as *Word;

if ptr::is_null(pc) {
loop;
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ impl Writer for fd_t {
let mut count = 0u;
do v.as_imm_buf |vbuf, len| {
while count < len {
let vb = ptr::offset(vbuf, count) as *c_void;
let vb = ptr::offset(vbuf, count as int) as *c_void;
let nout = libc::write(*self, vb, len as size_t);
if nout < 0 as ssize_t {
error!("error writing buffer");
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ pub fn set_exit_status(code: int) {
unsafe fn load_argc_and_argv(argc: c_int, argv: **c_char) -> ~[~str] {
let mut args = ~[];
for uint::range(0, argc as uint) |i| {
args.push(str::raw::from_c_str(*argv.offset(i)));
args.push(str::raw::from_c_str(*argv.offset(i as int)));
}
args
}
Expand Down Expand Up @@ -1165,9 +1165,9 @@ pub fn real_args() -> ~[~str] {
for uint::range(0, nArgs as uint) |i| {
unsafe {
// Determine the length of this argument.
let ptr = *szArgList.offset(i);
let ptr = *szArgList.offset(i as int);
let mut len = 0;
while *ptr.offset(len) != 0 { len += 1; }
while *ptr.offset(len as int) != 0 { len += 1; }

// Push it onto the list.
args.push(vec::raw::buf_as_slice(ptr, len,
Expand Down
67 changes: 46 additions & 21 deletions src/libstd/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use cast;
use clone::Clone;
use option::{Option, Some, None};
#[cfg(stage0)]
use sys;
use unstable::intrinsics;
use util::swap;
Expand All @@ -25,20 +26,44 @@ use uint;

/// Calculate the offset from a pointer
#[inline]
pub fn offset<T>(ptr: *T, count: uint) -> *T {
(ptr as uint + count * sys::size_of::<T>()) as *T
#[cfg(stage0)]
pub fn offset<T>(ptr: *T, count: int) -> *T {
(ptr as uint + (count as uint) * sys::size_of::<T>()) as *T
}

/// Calculate the offset from a const pointer
#[inline]
pub fn const_offset<T>(ptr: *const T, count: uint) -> *const T {
(ptr as uint + count * sys::size_of::<T>()) as *T
#[cfg(stage0)]
pub fn const_offset<T>(ptr: *const T, count: int) -> *const T {
(ptr as uint + (count as uint) * sys::size_of::<T>()) as *T
}

/// Calculate the offset from a mut pointer
#[inline]
pub fn mut_offset<T>(ptr: *mut T, count: uint) -> *mut T {
(ptr as uint + count * sys::size_of::<T>()) as *mut T
#[cfg(stage0)]
pub fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
(ptr as uint + (count as uint) * sys::size_of::<T>()) as *mut T
}

/// Calculate the offset from a pointer
#[inline]
#[cfg(not(stage0))]
pub fn offset<T>(ptr: *T, count: int) -> *T {
unsafe { intrinsics::offset(ptr, count) }
}

/// Calculate the offset from a const pointer
#[inline]
#[cfg(not(stage0))]
pub fn const_offset<T>(ptr: *const T, count: int) -> *const T {
unsafe { intrinsics::offset(ptr as *T, count) }
}

/// Calculate the offset from a mut pointer
#[inline]
#[cfg(not(stage0))]
pub fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
unsafe { intrinsics::offset(ptr as *T, count) as *mut T }
}

/// Return the offset of the first null pointer in `buf`.
Expand All @@ -58,7 +83,7 @@ impl<T> Clone for *T {
pub unsafe fn position<T>(buf: *T, f: &fn(&T) -> bool) -> uint {
let mut i = 0;
loop {
if f(&(*offset(buf, i))) { return i; }
if f(&(*offset(buf, i as int))) { return i; }
else { i += 1; }
}
}
Expand Down Expand Up @@ -242,7 +267,7 @@ pub unsafe fn array_each_with_len<T>(arr: **T, len: uint, cb: &fn(*T)) {
}
//let start_ptr = *arr;
uint::iterate(0, len, |e| {
let n = offset(arr, e);
let n = offset(arr, e as int);
cb(*n);
true
});
Expand Down Expand Up @@ -273,7 +298,7 @@ pub trait RawPtr<T> {
fn is_null(&self) -> bool;
fn is_not_null(&self) -> bool;
unsafe fn to_option(&self) -> Option<&T>;
fn offset(&self, count: uint) -> Self;
fn offset(&self, count: int) -> Self;
}

/// Extension methods for immutable pointers
Expand Down Expand Up @@ -305,7 +330,7 @@ impl<T> RawPtr<T> for *T {

/// Calculates the offset from a pointer.
#[inline]
fn offset(&self, count: uint) -> *T { offset(*self, count) }
fn offset(&self, count: int) -> *T { offset(*self, count) }
}

/// Extension methods for mutable pointers
Expand Down Expand Up @@ -337,7 +362,7 @@ impl<T> RawPtr<T> for *mut T {

/// Calculates the offset from a mutable pointer.
#[inline]
fn offset(&self, count: uint) -> *mut T { mut_offset(*self, count) }
fn offset(&self, count: int) -> *mut T { mut_offset(*self, count) }
}

// Equality for pointers
Expand Down Expand Up @@ -378,7 +403,7 @@ impl<T, I: Int> Add<I, *T> for *T {
/// Is calculated according to the size of the type pointed to.
#[inline]
pub fn add(&self, rhs: &I) -> *T {
self.offset(rhs.to_int() as uint)
self.offset(rhs.to_int() as int)
}
}

Expand All @@ -388,7 +413,7 @@ impl<T, I: Int> Sub<I, *T> for *T {
/// Is calculated according to the size of the type pointed to.
#[inline]
pub fn sub(&self, rhs: &I) -> *T {
self.offset(-rhs.to_int() as uint)
self.offset(-rhs.to_int() as int)
}
}

Expand All @@ -398,7 +423,7 @@ impl<T, I: Int> Add<I, *mut T> for *mut T {
/// Is calculated according to the size of the type pointed to.
#[inline]
pub fn add(&self, rhs: &I) -> *mut T {
self.offset(rhs.to_int() as uint)
self.offset(rhs.to_int() as int)
}
}

Expand All @@ -408,7 +433,7 @@ impl<T, I: Int> Sub<I, *mut T> for *mut T {
/// Is calculated according to the size of the type pointed to.
#[inline]
pub fn sub(&self, rhs: &I) -> *mut T {
self.offset(-rhs.to_int() as uint)
self.offset(-rhs.to_int() as int)
}
}

Expand Down Expand Up @@ -445,14 +470,14 @@ pub mod ptr_tests {
let v0 = ~[32000u16, 32001u16, 32002u16];
let mut v1 = ~[0u16, 0u16, 0u16];

copy_memory(mut_offset(vec::raw::to_mut_ptr(v1), 1u),
offset(vec::raw::to_ptr(v0), 1u), 1u);
copy_memory(mut_offset(vec::raw::to_mut_ptr(v1), 1),
offset(vec::raw::to_ptr(v0), 1), 1);
assert!((v1[0] == 0u16 && v1[1] == 32001u16 && v1[2] == 0u16));
copy_memory(vec::raw::to_mut_ptr(v1),
offset(vec::raw::to_ptr(v0), 2u), 1u);
offset(vec::raw::to_ptr(v0), 2), 1);
assert!((v1[0] == 32002u16 && v1[1] == 32001u16 &&
v1[2] == 0u16));
copy_memory(mut_offset(vec::raw::to_mut_ptr(v1), 2u),
copy_memory(mut_offset(vec::raw::to_mut_ptr(v1), 2),
vec::raw::to_ptr(v0), 1u);
assert!((v1[0] == 32002u16 && v1[1] == 32001u16 &&
v1[2] == 32000u16));
Expand Down Expand Up @@ -495,15 +520,15 @@ pub mod ptr_tests {
assert!(p.is_null());
assert!(!p.is_not_null());

let q = offset(p, 1u);
let q = offset(p, 1);
assert!(!q.is_null());
assert!(q.is_not_null());

let mp: *mut int = mut_null();
assert!(mp.is_null());
assert!(!mp.is_not_null());

let mq = mp.offset(1u);
let mq = mp.offset(1);
assert!(!mq.is_null());
assert!(mq.is_not_null());
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl ReprVisitor {
self.writer.write_str(", ");
}
self.visit_ptr_inner(p as *c_void, inner);
p = align(ptr::offset(p, sz) as uint, al) as *u8;
p = align(ptr::offset(p, sz as int) as uint, al) as *u8;
left -= dec;
}
self.writer.write_char(']');
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/rt/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ mod imp {
unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> ~[~str] {
let mut args = ~[];
for uint::range(0, argc as uint) |i| {
args.push(str::raw::from_c_str(*(argv as **libc::c_char).offset(i)));
args.push(str::raw::from_c_str(*(argv as **libc::c_char).offset(i as int)));
}
return args;
}
Expand Down
Loading

0 comments on commit 576f395

Please sign in to comment.