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

Misc benchmarks covering some of #7532 #7980

Closed
wants to merge 9 commits into from
24 changes: 24 additions & 0 deletions src/libstd/num/strconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,3 +703,27 @@ mod test {
assert_eq!(n, None);
}
}

#[cfg(test)]
mod bench {
use extra::test::BenchHarness;
use rand::{XorShiftRng,RngUtil};
use uint;
use float;

#[bench]
fn uint_to_str_rand(bh: &mut BenchHarness) {
let mut rng = XorShiftRng::new();
do bh.iter {
uint::to_str(rng.gen());
}
}

#[bench]
fn float_to_str_rand(bh: &mut BenchHarness) {
let mut rng = XorShiftRng::new();
do bh.iter {
float::to_str(rng.gen());
}
}
}
25 changes: 25 additions & 0 deletions src/libstd/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,28 @@ pub trait Shr<RHS,Result> {
pub trait Index<Index,Result> {
fn index(&self, index: &Index) -> Result;
}

#[cfg(test)]
mod bench {

use extra::test::BenchHarness;
use ops::Drop;

// Overhead of dtors

struct HasDtor {
x: int
}

impl Drop for HasDtor {
fn drop(&self) {
}
}

#[bench]
fn alloc_obj_with_dtor(bh: &mut BenchHarness) {
do bh.iter {
HasDtor { x : 10 };
}
}
}
36 changes: 35 additions & 1 deletion src/libstd/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ pub fn random<T: Rand>() -> T {
}

#[cfg(test)]
mod tests {
mod test {
use option::{Option, Some};
use super::*;

Expand Down Expand Up @@ -1109,3 +1109,37 @@ mod tests {
}
}
}

#[cfg(test)]
mod bench {
use extra::test::BenchHarness;
use rand::*;
use sys::size_of;

#[bench]
fn rand_xorshift(bh: &mut BenchHarness) {
let mut rng = XorShiftRng::new();
do bh.iter {
rng.gen::<uint>();
}
bh.bytes = size_of::<uint>() as u64;
}

#[bench]
fn rand_isaac(bh: &mut BenchHarness) {
let mut rng = IsaacRng::new();
do bh.iter {
rng.gen::<uint>();
}
bh.bytes = size_of::<uint>() as u64;
}

#[bench]
fn rand_shuffle_100(bh: &mut BenchHarness) {
let mut rng = XorShiftRng::new();
let x : &mut[uint] = [1,..100];
do bh.iter {
rng.shuffle_mut(x);
}
}
}
19 changes: 19 additions & 0 deletions src/libstd/rt/global_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,22 @@ pub unsafe fn exchange_free_(ptr: *c_char) {
pub unsafe fn exchange_free(ptr: *c_char) {
free(ptr as *c_void);
}

#[cfg(test)]
mod bench {
use extra::test::BenchHarness;

#[bench]
fn alloc_owned_small(bh: &mut BenchHarness) {
do bh.iter {
~10;
}
}

#[bench]
fn alloc_owned_big(bh: &mut BenchHarness) {
do bh.iter {
~[10, ..1000];
}
}
}
19 changes: 19 additions & 0 deletions src/libstd/rt/local_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,22 @@ extern {
fn rust_boxed_region_free(region: *BoxedRegion, box: *OpaqueBox);
fn rust_current_boxed_region() -> *BoxedRegion;
}

#[cfg(test)]
mod bench {
use extra::test::BenchHarness;

#[bench]
fn alloc_managed_small(bh: &mut BenchHarness) {
do bh.iter {
@10;
}
}

#[bench]
fn alloc_managed_big(bh: &mut BenchHarness) {
do bh.iter {
@[10, ..1000];
}
}
}
3 changes: 3 additions & 0 deletions src/libstd/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ they contained the following prologue:
#[deny(non_camel_case_types)];
#[deny(missing_doc)];

// Make extra accessible for benchmarking
#[cfg(test)] extern mod extra(vers="0.8-pre");

// Make std testable by not duplicating lang items. See #2912
#[cfg(test)] extern mod realstd(name = "std");
#[cfg(test)] pub use kinds = realstd::kinds;
Expand Down
47 changes: 47 additions & 0 deletions src/libstd/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3532,3 +3532,50 @@ mod tests {
assert_eq!(5, sum_len([s.as_slice()]));
}
}

#[cfg(test)]
mod bench {
use extra::test::BenchHarness;
use str;

#[bench]
fn is_utf8_100_ascii(bh: &mut BenchHarness) {

let s = bytes!("Hello there, the quick brown fox jumped over the lazy dog! \
Lorem ipsum dolor sit amet, consectetur. ");

assert_eq!(100, s.len());
do bh.iter {
str::is_utf8(s);
}
}

#[bench]
fn is_utf8_100_multibyte(bh: &mut BenchHarness) {
let s = bytes!("𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰");
assert_eq!(100, s.len());
do bh.iter {
str::is_utf8(s);
}
}

#[bench]
fn map_chars_100_ascii(bh: &mut BenchHarness) {
let s = "HelloHelloHelloHelloHelloHelloHelloHelloHelloHello\
HelloHelloHelloHelloHelloHelloHelloHelloHelloHello";
do bh.iter {
s.map_chars(|c| ((c as uint) + 1) as char);
}
}

#[bench]
fn map_chars_100_multibytes(bh: &mut BenchHarness) {
let s = "𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑\
𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑\
𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑\
𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑";
do bh.iter {
s.map_chars(|c| ((c as uint) + 1) as char);
}
}
}
65 changes: 65 additions & 0 deletions src/libstd/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,68 @@ mod tests {
unsafe { assert_eq!(did_run, true); }
}
}

/// Completely miscellaneous language-construct benchmarks.
#[cfg(test)]
mod bench {

use extra::test::BenchHarness;
use option::{Some,None};

// Static/dynamic method dispatch

struct Struct {
field: int
}

trait Trait {
fn method(&self) -> int;
}

impl Trait for Struct {
fn method(&self) -> int {
self.field
}
}

#[bench]
fn trait_vtable_method_call(bh: &mut BenchHarness) {
let s = Struct { field: 10 };
let t = &s as &Trait;
do bh.iter {
t.method();
}
}

#[bench]
fn trait_static_method_call(bh: &mut BenchHarness) {
let s = Struct { field: 10 };
do bh.iter {
s.method();
}
}

// Overhead of various match forms

#[bench]
fn match_option_some(bh: &mut BenchHarness) {
let x = Some(10);
do bh.iter {
let _q = match x {
Some(y) => y,
None => 11
};
}
}

#[bench]
fn match_vec_pattern(bh: &mut BenchHarness) {
let x = [1,2,3,4,5,6];
do bh.iter {
let _q = match x {
[1,2,3,.._] => 10,
_ => 11
};
}
}
}
2 changes: 1 addition & 1 deletion src/rt/rust_crate_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class cratemap {
return &reinterpret_cast<const cratemap_v0 *>(this)->
m_children[0];
case 1:
return &m_children[1];
return &m_children[0];
default: assert(false && "Unknown crate map version!");
return NULL; // Appease -Werror=return-type
}
Expand Down