1
Fork 0

More perf tweaks (issue #2719)

This commit is contained in:
Eric Holk 2012-06-26 11:13:02 -07:00
parent 51468b65a4
commit a08281616f
6 changed files with 70 additions and 21 deletions

View file

@ -161,7 +161,6 @@ impl extensions<A> for dvec<A> {
vec::push(self.data, t);
}
#[doc = "Remove and return the first element"]
fn shift() -> A {
self.borrow { |v|

View file

@ -52,7 +52,7 @@ impl reader_util for reader {
i += 1u;
assert (w > 0u);
if w == 1u {
chars += [ b0 as char ]/~;
vec::push(chars, b0 as char );
cont;
}
// can't satisfy this char with the existing data
@ -71,7 +71,7 @@ impl reader_util for reader {
// See str::char_at
val += ((b0 << ((w + 1u) as u8)) as uint)
<< (w - 1u) * 6u - w - 1u;
chars += [ val as char ]/~;
vec::push(chars, val as char );
}
ret (i, 0u);
}
@ -86,7 +86,7 @@ impl reader_util for reader {
// we're split in a unicode char?
break;
}
buf += data;
vec::push_all(buf, data);
let (offset, nbreq) = chars_from_buf(buf, chars);
let ncreq = n - vec::len(chars);
// again we either know we need a certain number of bytes
@ -110,11 +110,11 @@ impl reader_util for reader {
}
fn read_line() -> str {
let mut buf: [u8]/~ = []/~;
let mut buf = []/~;
loop {
let ch = self.read_byte();
if ch == -1 || ch == 10 { break; }
buf += [ch as u8]/~;
vec::push(buf, ch as u8);
}
str::from_bytes(buf)
}
@ -123,7 +123,7 @@ impl reader_util for reader {
let mut buf: [u8]/~ = []/~;
loop {
let ch = self.read_byte();
if ch < 1 { break; } else { buf += [ch as u8]/~; }
if ch < 1 { break; } else { vec::push(buf, ch as u8); }
}
str::from_bytes(buf)
}
@ -158,7 +158,7 @@ impl reader_util for reader {
fn read_whole_stream() -> [u8]/~ {
let mut buf: [u8]/~ = []/~;
while !self.eof() { buf += self.read_bytes(2048u); }
while !self.eof() { vec::push_all(buf, self.read_bytes(2048u)); }
buf
}
@ -453,7 +453,7 @@ fn u64_to_le_bytes<T>(n: u64, size: uint, f: fn([u8]/&) -> T) -> T {
let mut bytes: [u8]/~ = []/~, i = size, n = n;
while i > 0u {
bytes += [(n & 255_u64) as u8]/~;
vec::push(bytes, (n & 255_u64) as u8);
n >>= 8_u64;
i -= 1u;
}
@ -485,7 +485,7 @@ fn u64_to_be_bytes<T>(n: u64, size: uint, f: fn([u8]/&) -> T) -> T {
let mut i = size;
while i > 0u {
let shift = ((i - 1u) * 8u) as u64;
bytes += [(n >> shift) as u8]/~;
vec::push(bytes, (n >> shift) as u8);
i -= 1u;
}
f(bytes)

View file

@ -122,7 +122,7 @@ Convert a vector of bytes to a UTF-8 string
Fails if invalid UTF-8
"]
pure fn from_bytes(vv: [u8]/~) -> str {
pure fn from_bytes(+vv: [u8]/~) -> str {
assert is_utf8(vv);
ret unsafe { unsafe::from_bytes(vv) };
}
@ -1750,9 +1750,9 @@ mod unsafe {
Does not verify that the vector contains valid UTF-8.
"]
unsafe fn from_bytes(v: [const u8]/~) -> str {
unsafe fn from_bytes(+v: [const u8]/~) -> str {
unsafe {
let mut vcopy = ::unsafe::transmute(copy v);
let mut vcopy = ::unsafe::transmute(v);
vec::push(vcopy, 0u8);
::unsafe::transmute(vcopy)
}

View file

@ -384,13 +384,12 @@ fn shift<T>(&v: [T]/~) -> T {
let mut rr;
{
let vv = unsafe::to_ptr(vv);
let mut r <- *vv;
rr <- *vv;
for uint::range(1u, ln) {|i|
let r <- *ptr::offset(vv, i);
push(v, r);
}
rr <- r;
}
unsafe::set_len(vv, 0u);

View file

@ -0,0 +1,48 @@
// Microbenchmarks for various functions in core and std
use std;
import std::time::precise_time_s;
import io::{reader, reader_util};
fn main() {
#macro[
[#bench[id],
run_test(#stringify(id), id)]
];
#bench[shift_push];
#bench[read_line];
}
fn run_test(name: str, test: fn()) {
let start = precise_time_s();
test();
let stop = precise_time_s();
io::println(#fmt("%s:\t\t%f ms", name, (stop - start) * 1000f));
}
fn shift_push() {
let mut v1 = vec::from_elem(30000, 1);
let mut v2 = []/~;
while v1.len() > 0 {
vec::push(v2, vec::shift(v1));
}
}
fn read_line() {
let path = path::connect(
#env("CFG_SRC_DIR"),
"src/test/bench/shootout-k-nucleotide.data"
);
for int::range(0, 3) {|_i|
let reader = result::get(io::file_reader(path));
while !reader.eof() {
reader.read_line();
}
}
}

View file

@ -62,29 +62,32 @@ fn find(mm: hashmap<[u8]/~, uint>, key: str) -> uint {
}
// given a map, increment the counter for a key
fn update_freq(mm: hashmap<[u8]/~, uint>, key: [u8]/~) {
alt mm.find(key) {
fn update_freq(mm: hashmap<[u8]/~, uint>, key: [u8]/&) {
let key = vec::slice(key, 0, key.len());
alt mm.find(key) {
option::none { mm.insert(key, 1u ); }
option::some(val) { mm.insert(key, 1u + val); }
}
}
}
// given a [u8]/~, for each window call a function
// i.e., for "hello" and windows of size four,
// run it("hell") and it("ello"), then return "llo"
fn windows_with_carry(bb: [const u8]/~, nn: uint, it: fn(window: [u8]/~)) -> [u8]/~ {
fn windows_with_carry(bb: [const u8]/~, nn: uint,
it: fn(window: [u8]/&)) -> [u8]/~ {
let mut ii = 0u;
let len = vec::len(bb);
while ii < len - (nn - 1u) {
it(vec::slice(bb, ii, ii+nn));
it(vec::view(bb, ii, ii+nn));
ii += 1u;
}
ret vec::slice(bb, len - (nn - 1u), len);
}
fn make_sequence_processor(sz: uint, from_parent: comm::port<[u8]/~>, to_parent: comm::chan<str>) {
fn make_sequence_processor(sz: uint, from_parent: comm::port<[u8]/~>,
to_parent: comm::chan<str>) {
let freqs: hashmap<[u8]/~, uint> = map::bytes_hash();
let mut carry: [u8]/~ = []/~;