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

optimizations for vec/str #8412

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/libstd/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,13 +807,24 @@ pub fn from_utf16(v: &[u16]) -> ~str {

/// Allocates a new string with the specified capacity. The string returned is
/// the empty string, but has capacity for much more.
#[cfg(stage0)]
#[inline]
pub fn with_capacity(capacity: uint) -> ~str {
let mut buf = ~"";
buf.reserve(capacity);
buf
}

/// Allocates a new string with the specified capacity. The string returned is
/// the empty string, but has capacity for much more.
#[cfg(not(stage0))]
#[inline]
pub fn with_capacity(capacity: uint) -> ~str {
unsafe {
cast::transmute(vec::with_capacity::<~[u8]>(capacity))
}
}

/// As char_len but for a slice of a string
///
/// # Arguments
Expand Down Expand Up @@ -948,6 +959,14 @@ pub mod raw {
::cast::transmute(v)
}

#[lang="strdup_uniq"]
#[cfg(not(test))]
#[allow(missing_doc)]
#[inline]
pub unsafe fn strdup_uniq(ptr: *u8, len: uint) -> ~str {
from_buf_len(ptr, len)
}

/// Create a Rust string from a null-terminated C string
pub unsafe fn from_c_str(buf: *libc::c_char) -> ~str {
let mut curr = buf;
Expand Down Expand Up @@ -3700,7 +3719,7 @@ mod tests {
#[cfg(test)]
mod bench {
use extra::test::BenchHarness;
use str;
use super::*;

#[bench]
fn is_utf8_100_ascii(bh: &mut BenchHarness) {
Expand All @@ -3710,7 +3729,7 @@ mod bench {

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

Expand All @@ -3719,7 +3738,7 @@ mod bench {
let s = bytes!("𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰");
assert_eq!(100, s.len());
do bh.iter {
str::is_utf8(s);
is_utf8(s);
}
}

Expand All @@ -3742,4 +3761,11 @@ mod bench {
s.map_chars(|c| ((c as uint) + 1) as char);
}
}

#[bench]
fn bench_with_capacity(bh: &mut BenchHarness) {
do bh.iter {
with_capacity(100);
}
}
}
9 changes: 1 addition & 8 deletions src/libstd/unstable/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

use c_str::ToCStr;
use cast::transmute;
use libc::{c_char, c_uchar, c_void, size_t, uintptr_t};
use str;
use libc::{c_char, c_void, size_t, uintptr_t};
use sys;
use rt::task::Task;
use rt::local::Local;
Expand Down Expand Up @@ -93,12 +92,6 @@ pub unsafe fn check_not_borrowed(a: *u8,
borrowck::check_not_borrowed(a, file, line)
}

#[lang="strdup_uniq"]
#[inline]
pub unsafe fn strdup_uniq(ptr: *c_uchar, len: uint) -> ~str {
str::raw::from_buf_len(ptr, len)
}

#[lang="annihilate"]
pub unsafe fn annihilate() {
::cleanup::annihilate()
Expand Down
19 changes: 14 additions & 5 deletions src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ impl<'self, T> RandomAccessIterator<&'self [T]> for ChunkIter<'self, T> {

#[cfg(not(test))]
pub mod traits {
use super::Vector;
use super::*;

use clone::Clone;
use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Equal, Equiv};
Expand Down Expand Up @@ -686,17 +686,17 @@ pub mod traits {
impl<'self,T:Clone, V: Vector<T>> Add<V, ~[T]> for &'self [T] {
#[inline]
fn add(&self, rhs: &V) -> ~[T] {
let mut res = self.to_owned();
let mut res = with_capacity(self.len() + rhs.as_slice().len());
res.push_all(*self);
res.push_all(rhs.as_slice());
res
}
}

impl<T:Clone, V: Vector<T>> Add<V, ~[T]> for ~[T] {
#[inline]
fn add(&self, rhs: &V) -> ~[T] {
let mut res = self.to_owned();
res.push_all(rhs.as_slice());
res
self.as_slice() + rhs.as_slice()
}
}
}
Expand Down Expand Up @@ -3662,4 +3662,13 @@ mod bench {
}
}
}

#[bench]
fn add(b: &mut BenchHarness) {
let xs: &[int] = [5, ..10];
let ys: &[int] = [5, ..10];
do b.iter() {
xs + ys;
}
}
}