Skip to content

Commit a082816

Browse files
committed
More perf tweaks (issue #2719)
1 parent 51468b6 commit a082816

File tree

6 files changed

+70
-21
lines changed

6 files changed

+70
-21
lines changed

src/libcore/dvec.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ impl extensions<A> for dvec<A> {
161161
vec::push(self.data, t);
162162
}
163163

164-
165164
#[doc = "Remove and return the first element"]
166165
fn shift() -> A {
167166
self.borrow { |v|

src/libcore/io.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl reader_util for reader {
5252
i += 1u;
5353
assert (w > 0u);
5454
if w == 1u {
55-
chars += [ b0 as char ]/~;
55+
vec::push(chars, b0 as char );
5656
cont;
5757
}
5858
// can't satisfy this char with the existing data
@@ -71,7 +71,7 @@ impl reader_util for reader {
7171
// See str::char_at
7272
val += ((b0 << ((w + 1u) as u8)) as uint)
7373
<< (w - 1u) * 6u - w - 1u;
74-
chars += [ val as char ]/~;
74+
vec::push(chars, val as char );
7575
}
7676
ret (i, 0u);
7777
}
@@ -86,7 +86,7 @@ impl reader_util for reader {
8686
// we're split in a unicode char?
8787
break;
8888
}
89-
buf += data;
89+
vec::push_all(buf, data);
9090
let (offset, nbreq) = chars_from_buf(buf, chars);
9191
let ncreq = n - vec::len(chars);
9292
// again we either know we need a certain number of bytes
@@ -110,11 +110,11 @@ impl reader_util for reader {
110110
}
111111

112112
fn read_line() -> str {
113-
let mut buf: [u8]/~ = []/~;
113+
let mut buf = []/~;
114114
loop {
115115
let ch = self.read_byte();
116116
if ch == -1 || ch == 10 { break; }
117-
buf += [ch as u8]/~;
117+
vec::push(buf, ch as u8);
118118
}
119119
str::from_bytes(buf)
120120
}
@@ -123,7 +123,7 @@ impl reader_util for reader {
123123
let mut buf: [u8]/~ = []/~;
124124
loop {
125125
let ch = self.read_byte();
126-
if ch < 1 { break; } else { buf += [ch as u8]/~; }
126+
if ch < 1 { break; } else { vec::push(buf, ch as u8); }
127127
}
128128
str::from_bytes(buf)
129129
}
@@ -158,7 +158,7 @@ impl reader_util for reader {
158158

159159
fn read_whole_stream() -> [u8]/~ {
160160
let mut buf: [u8]/~ = []/~;
161-
while !self.eof() { buf += self.read_bytes(2048u); }
161+
while !self.eof() { vec::push_all(buf, self.read_bytes(2048u)); }
162162
buf
163163
}
164164

@@ -453,7 +453,7 @@ fn u64_to_le_bytes<T>(n: u64, size: uint, f: fn([u8]/&) -> T) -> T {
453453

454454
let mut bytes: [u8]/~ = []/~, i = size, n = n;
455455
while i > 0u {
456-
bytes += [(n & 255_u64) as u8]/~;
456+
vec::push(bytes, (n & 255_u64) as u8);
457457
n >>= 8_u64;
458458
i -= 1u;
459459
}
@@ -485,7 +485,7 @@ fn u64_to_be_bytes<T>(n: u64, size: uint, f: fn([u8]/&) -> T) -> T {
485485
let mut i = size;
486486
while i > 0u {
487487
let shift = ((i - 1u) * 8u) as u64;
488-
bytes += [(n >> shift) as u8]/~;
488+
vec::push(bytes, (n >> shift) as u8);
489489
i -= 1u;
490490
}
491491
f(bytes)

src/libcore/str.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Convert a vector of bytes to a UTF-8 string
122122
123123
Fails if invalid UTF-8
124124
"]
125-
pure fn from_bytes(vv: [u8]/~) -> str {
125+
pure fn from_bytes(+vv: [u8]/~) -> str {
126126
assert is_utf8(vv);
127127
ret unsafe { unsafe::from_bytes(vv) };
128128
}
@@ -1750,9 +1750,9 @@ mod unsafe {
17501750
17511751
Does not verify that the vector contains valid UTF-8.
17521752
"]
1753-
unsafe fn from_bytes(v: [const u8]/~) -> str {
1753+
unsafe fn from_bytes(+v: [const u8]/~) -> str {
17541754
unsafe {
1755-
let mut vcopy = ::unsafe::transmute(copy v);
1755+
let mut vcopy = ::unsafe::transmute(v);
17561756
vec::push(vcopy, 0u8);
17571757
::unsafe::transmute(vcopy)
17581758
}

src/libcore/vec.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,12 @@ fn shift<T>(&v: [T]/~) -> T {
384384
let mut rr;
385385
{
386386
let vv = unsafe::to_ptr(vv);
387-
let mut r <- *vv;
387+
rr <- *vv;
388388

389389
for uint::range(1u, ln) {|i|
390390
let r <- *ptr::offset(vv, i);
391391
push(v, r);
392392
}
393-
rr <- r;
394393
}
395394
unsafe::set_len(vv, 0u);
396395

src/test/bench/core-std.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Microbenchmarks for various functions in core and std
2+
3+
use std;
4+
5+
import std::time::precise_time_s;
6+
7+
import io::{reader, reader_util};
8+
9+
fn main() {
10+
#macro[
11+
[#bench[id],
12+
run_test(#stringify(id), id)]
13+
];
14+
15+
#bench[shift_push];
16+
#bench[read_line];
17+
}
18+
19+
fn run_test(name: str, test: fn()) {
20+
let start = precise_time_s();
21+
test();
22+
let stop = precise_time_s();
23+
24+
io::println(#fmt("%s:\t\t%f ms", name, (stop - start) * 1000f));
25+
}
26+
27+
fn shift_push() {
28+
let mut v1 = vec::from_elem(30000, 1);
29+
let mut v2 = []/~;
30+
31+
while v1.len() > 0 {
32+
vec::push(v2, vec::shift(v1));
33+
}
34+
}
35+
36+
fn read_line() {
37+
let path = path::connect(
38+
#env("CFG_SRC_DIR"),
39+
"src/test/bench/shootout-k-nucleotide.data"
40+
);
41+
42+
for int::range(0, 3) {|_i|
43+
let reader = result::get(io::file_reader(path));
44+
while !reader.eof() {
45+
reader.read_line();
46+
}
47+
}
48+
}

src/test/bench/shootout-k-nucleotide.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,32 @@ fn find(mm: hashmap<[u8]/~, uint>, key: str) -> uint {
6262
}
6363

6464
// given a map, increment the counter for a key
65-
fn update_freq(mm: hashmap<[u8]/~, uint>, key: [u8]/~) {
66-
alt mm.find(key) {
65+
fn update_freq(mm: hashmap<[u8]/~, uint>, key: [u8]/&) {
66+
let key = vec::slice(key, 0, key.len());
67+
alt mm.find(key) {
6768
option::none { mm.insert(key, 1u ); }
6869
option::some(val) { mm.insert(key, 1u + val); }
69-
}
70+
}
7071
}
7172

7273
// given a [u8]/~, for each window call a function
7374
// i.e., for "hello" and windows of size four,
7475
// run it("hell") and it("ello"), then return "llo"
75-
fn windows_with_carry(bb: [const u8]/~, nn: uint, it: fn(window: [u8]/~)) -> [u8]/~ {
76+
fn windows_with_carry(bb: [const u8]/~, nn: uint,
77+
it: fn(window: [u8]/&)) -> [u8]/~ {
7678
let mut ii = 0u;
7779

7880
let len = vec::len(bb);
7981
while ii < len - (nn - 1u) {
80-
it(vec::slice(bb, ii, ii+nn));
82+
it(vec::view(bb, ii, ii+nn));
8183
ii += 1u;
8284
}
8385

8486
ret vec::slice(bb, len - (nn - 1u), len);
8587
}
8688

87-
fn make_sequence_processor(sz: uint, from_parent: comm::port<[u8]/~>, to_parent: comm::chan<str>) {
89+
fn make_sequence_processor(sz: uint, from_parent: comm::port<[u8]/~>,
90+
to_parent: comm::chan<str>) {
8891

8992
let freqs: hashmap<[u8]/~, uint> = map::bytes_hash();
9093
let mut carry: [u8]/~ = []/~;

0 commit comments

Comments
 (0)