Remove a bunch of unnecessary allocations and copies

This commit is contained in:
Björn Steinbrink 2013-05-29 20:10:16 +02:00
parent ca74cbdc5c
commit 1720d9f663
27 changed files with 83 additions and 91 deletions

View File

@ -99,7 +99,7 @@ pub fn is_test_ignored(config: &config, testfile: &Path) -> bool {
return false;
fn xfail_target() -> ~str {
~"xfail-" + str::to_owned(os::SYSNAME)
~"xfail-" + os::SYSNAME
}
}
@ -173,7 +173,7 @@ fn parse_name_directive(line: &str, directive: &str) -> bool {
fn parse_name_value_directive(line: &str,
directive: ~str) -> Option<~str> {
let keycolon = directive + ~":";
let keycolon = directive + ":";
match str::find_str(line, keycolon) {
Some(colon) => {
let value = str::slice(line, colon + str::len(keycolon),

View File

@ -24,12 +24,12 @@ fn target_env(lib_path: &str, prog: &str) -> ~[(~str,~str)] {
let mut env = os::env();
// Make sure we include the aux directory in the path
assert!(prog.ends_with(~".exe"));
let aux_path = prog.slice(0u, prog.len() - 4u).to_owned() + ~".libaux";
assert!(prog.ends_with(".exe"));
let aux_path = prog.slice(0u, prog.len() - 4u).to_owned() + ".libaux";
env = do vec::map(env) |pair| {
let (k,v) = *pair;
if k == ~"PATH" { (~"PATH", v + ~";" + lib_path + ~";" + aux_path) }
if k == ~"PATH" { (~"PATH", v + ";" + lib_path + ";" + aux_path) }
else { (k,v) }
};
if str::ends_with(prog, "rustc.exe") {

View File

@ -378,7 +378,7 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
was_expected = true;
}
if !was_expected && is_compiler_error_or_warning(str::to_owned(line)) {
if !was_expected && is_compiler_error_or_warning(line) {
fatal_ProcRes(fmt!("unexpected compiler error or warning: '%s'",
line),
ProcRes);
@ -596,8 +596,7 @@ fn make_lib_name(config: &config, auxfile: &Path, testfile: &Path) -> Path {
}
fn make_exe_name(config: &config, testfile: &Path) -> Path {
Path(output_base_name(config, testfile).to_str() +
str::to_owned(os::EXE_SUFFIX))
Path(output_base_name(config, testfile).to_str() + os::EXE_SUFFIX)
}
fn make_run_args(config: &config, _props: &TestProps, testfile: &Path) ->
@ -606,7 +605,7 @@ fn make_run_args(config: &config, _props: &TestProps, testfile: &Path) ->
// then split apart its command
let toolargs = split_maybe_args(&config.runtool);
let mut args = toolargs + ~[make_exe_name(config, testfile).to_str()];
let mut args = toolargs + [make_exe_name(config, testfile).to_str()];
let prog = args.shift();
return ProcArgs {prog: prog, args: args};
}
@ -655,7 +654,7 @@ fn make_cmdline(_libpath: &str, prog: &str, args: &[~str]) -> ~str {
#[cfg(target_os = "win32")]
fn make_cmdline(libpath: &str, prog: &str, args: &[~str]) -> ~str {
fmt!("%s %s %s", lib_path_cmd_prefix(libpath), prog,
str::connect(args, ~" "))
str::connect(args, " "))
}
// Build the LD_LIBRARY_PATH variable as it would be seen on the command line
@ -776,8 +775,8 @@ fn _arm_exec_compiled_test(config: &config, props: &TestProps,
for args.args.each |tv| {
newcmd_out.push_str(" ");
newcmd_err.push_str(" ");
newcmd_out.push_str(tv.to_owned());
newcmd_err.push_str(tv.to_owned());
newcmd_out.push_str(*tv);
newcmd_err.push_str(*tv);
}
newcmd_out.push_str(" 2>/dev/null");

View File

@ -110,10 +110,11 @@ pub struct Opt {
}
fn mkname(nm: &str) -> Name {
let unm = str::to_owned(nm);
return if nm.len() == 1u {
Short(str::char_at(unm, 0u))
} else { Long(unm) };
if nm.len() == 1u {
Short(str::char_at(nm, 0u))
} else {
Long(nm.to_owned())
}
}
/// Create an option that is required and takes an argument
@ -195,19 +196,19 @@ pub enum Fail_ {
pub fn fail_str(f: Fail_) -> ~str {
return match f {
ArgumentMissing(ref nm) => {
~"Argument to option '" + *nm + "' missing."
fmt!("Argument to option '%s' missing.", *nm)
}
UnrecognizedOption(ref nm) => {
~"Unrecognized option: '" + *nm + "'."
fmt!("Unrecognized option: '%s'.", *nm)
}
OptionMissing(ref nm) => {
~"Required option '" + *nm + "' missing."
fmt!("Required option '%s' missing.", *nm)
}
OptionDuplicated(ref nm) => {
~"Option '" + *nm + "' given more than once."
fmt!("Option '%s' given more than once.", *nm)
}
UnexpectedArgument(ref nm) => {
~"Option " + *nm + " does not take an argument."
fmt!("Option '%s' does not take an argument.", *nm)
}
};
}
@ -245,11 +246,11 @@ pub fn getopts(args: &[~str], opts: &[Opt]) -> Result {
let mut names;
let mut i_arg = None;
if cur[1] == '-' as u8 {
let tail = str::slice(cur, 2, curlen).to_owned();
let tail = str::slice(cur, 2, curlen);
let mut tail_eq = ~[];
for str::each_splitn_char(tail, '=', 1) |s| { tail_eq.push(s.to_owned()) }
if tail_eq.len() <= 1 {
names = ~[Long(tail)];
names = ~[Long(tail.to_owned())];
} else {
names =
~[Long(copy tail_eq[0])];

View File

@ -230,7 +230,7 @@ pub mod v4 {
let input_is_inaddr_none =
result::get(&ip_rep_result).as_u32() == INADDR_NONE;
let new_addr = uv_ip4_addr(str::to_owned(ip), 22);
let new_addr = uv_ip4_addr(ip, 22);
let reformatted_name = uv_ip4_name(&new_addr);
debug!("try_parse_addr: input ip: %s reparsed ip: %s",
ip, reformatted_name);
@ -259,7 +259,6 @@ pub mod v6 {
use uv_ip6_name = uv::ll::ip6_name;
use core::result;
use core::str;
/**
* Convert a str to `ip_addr`
@ -285,7 +284,7 @@ pub mod v6 {
pub fn try_parse_addr(ip: &str) -> result::Result<IpAddr,ParseAddrErr> {
unsafe {
// need to figure out how to establish a parse failure..
let new_addr = uv_ip6_addr(str::to_owned(ip), 22);
let new_addr = uv_ip6_addr(ip, 22);
let reparsed_name = uv_ip6_name(&new_addr);
debug!("v6::try_parse_addr ip: '%s' reparsed '%s'",
ip, reparsed_name);

View File

@ -585,7 +585,7 @@ fn get_path(rawurl: &str, authority: bool) ->
}
}
return Ok((decode_component(str::slice(rawurl, 0, end).to_owned()),
return Ok((decode_component(str::slice(rawurl, 0, end)),
str::slice(rawurl, end, len).to_owned()));
}
@ -596,14 +596,13 @@ fn get_query_fragment(rawurl: &str) ->
if str::starts_with(rawurl, "#") {
let f = decode_component(str::slice(rawurl,
1,
str::len(rawurl)).to_owned());
str::len(rawurl)));
return Ok((~[], Some(f)));
} else {
return Ok((~[], None));
}
}
let (q, r) = split_char_first(str::slice(rawurl, 1,
str::len(rawurl)).to_owned(), '#');
let (q, r) = split_char_first(str::slice(rawurl, 1, rawurl.len()), '#');
let f = if str::len(r) != 0 {
Some(decode_component(r)) } else { None };
return Ok((query_from_str(q), f));

View File

@ -399,8 +399,7 @@ mod tests {
let mut left = len;
while left > 0u {
let take = (left + 1u) / 2u;
sh.input_str(str::slice(t.input, len - left,
take + len - left).to_owned());
sh.input_str(t.input.slice(len - left, take + len - left));
left = left - take;
}
let out = sh.result();

View File

@ -293,7 +293,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
let mut i = 0u;
while i < digits {
let range = str::char_range_at(str::to_owned(ss), pos);
let range = str::char_range_at(ss, pos);
pos = range.next;
match range.ch {
@ -632,7 +632,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
}
}
do io::with_str_reader(str::to_owned(format)) |rdr| {
do io::with_str_reader(format) |rdr| {
let mut tm = Tm {
tm_sec: 0_i32,
tm_min: 0_i32,
@ -844,7 +844,7 @@ priv fn do_strftime(format: &str, tm: &Tm) -> ~str {
let mut buf = ~"";
do io::with_str_reader(str::to_owned(format)) |rdr| {
do io::with_str_reader(format) |rdr| {
while !rdr.eof() {
match rdr.read_char() {
'%' => buf += parse_type(rdr.read_char(), tm),

View File

@ -201,7 +201,7 @@ struct Logger {
pub impl Logger {
fn info(&self, i: &str) {
io::println(~"workcache: " + i.to_owned());
io::println(~"workcache: " + i);
}
}

View File

@ -741,7 +741,6 @@ pub fn mangle_internal_name_by_seq(ccx: @CrateContext, flav: &str) -> ~str {
pub fn output_dll_filename(os: session::os, lm: LinkMeta) -> ~str {
let libname = fmt!("%s-%s-%s", lm.name, lm.extras_hash, lm.vers);
let (dll_prefix, dll_suffix) = match os {
session::os_win32 => (win32::DLL_PREFIX, win32::DLL_SUFFIX),
session::os_macos => (macos::DLL_PREFIX, macos::DLL_SUFFIX),
@ -749,8 +748,7 @@ pub fn output_dll_filename(os: session::os, lm: LinkMeta) -> ~str {
session::os_android => (android::DLL_PREFIX, android::DLL_SUFFIX),
session::os_freebsd => (freebsd::DLL_PREFIX, freebsd::DLL_SUFFIX),
};
return str::to_owned(dll_prefix) + libname +
str::to_owned(dll_suffix);
fmt!("%s%s-%s-%s%s", dll_prefix, lm.name, lm.extras_hash, lm.vers, dll_suffix)
}
// If the user wants an exe generated we need to invoke

View File

@ -2014,7 +2014,7 @@ pub fn type_to_str_inner(names: @TypeNames, outer0: &[TypeRef], ty: TypeRef)
let mut first: bool = true;
for tys.each |t| {
if first { first = false; } else { s += ", "; }
s += type_to_str_inner(names, outer, *t).to_owned();
s += type_to_str_inner(names, outer, *t);
}
// [Note at-str] FIXME #2543: Could rewrite this without the copy,
// but need better @str support.

View File

@ -268,8 +268,7 @@ pub fn list_file_metadata(intr: @ident_interner,
match get_metadata_section(os, path) {
option::Some(bytes) => decoder::list_crate_metadata(intr, bytes, out),
option::None => {
out.write_str(~"could not find metadata in "
+ path.to_str() + ".\n");
out.write_str(fmt!("could not find metadata in %s.\n", path.to_str()))
}
}
}

View File

@ -89,8 +89,7 @@ pub fn enc_ty(w: @io::Writer, cx: @ctxt, t: ty::t) {
let abbrev_len = 3u + estimate_sz(pos) + estimate_sz(len);
if abbrev_len < len {
// I.e. it's actually an abbreviation.
let s = ~"#" + uint::to_str_radix(pos, 16u) + ":" +
uint::to_str_radix(len, 16u) + "#";
let s = fmt!("#%x:%x#", pos, len);
let a = ty_abbrev { pos: pos, len: len, s: @s };
abbrevs.insert(t, a);
}

View File

@ -98,7 +98,8 @@ pub fn trans_inline_asm(bcx: block, ia: &ast::inline_asm) -> block {
// Add the clobbers to our constraints list
if clobbers != ~"" && constraints != ~"" {
constraints += ~"," + clobbers;
constraints += ",";
constraints += clobbers;
} else {
constraints += clobbers;
}

View File

@ -2906,8 +2906,7 @@ pub fn decl_crate_map(sess: session::Session, mapmeta: LinkMeta,
let cstore = sess.cstore;
while cstore::have_crate_data(cstore, n_subcrates) { n_subcrates += 1; }
let mapname = if *sess.building_library {
mapmeta.name.to_owned() + "_" + mapmeta.vers.to_owned() + "_"
+ mapmeta.extras_hash.to_owned()
fmt!("%s_%s_%s", mapmeta.name, mapmeta.vers, mapmeta.extras_hash)
} else {
~"toplevel"
};

View File

@ -60,8 +60,8 @@ pub impl FnType {
fn build_shim_args(&self, bcx: block,
arg_tys: &[TypeRef],
llargbundle: ValueRef) -> ~[ValueRef] {
let mut atys = /*bad*/copy self.arg_tys;
let mut attrs = /*bad*/copy self.attrs;
let mut atys: &[LLVMType] = self.arg_tys;
let mut attrs: &[option::Option<Attribute>] = self.attrs;
let mut llargvals = ~[];
let mut i = 0u;
@ -71,8 +71,8 @@ pub impl FnType {
let llretptr = GEPi(bcx, llargbundle, [0u, n]);
let llretloc = Load(bcx, llretptr);
llargvals = ~[llretloc];
atys = vec::to_owned(atys.tail());
attrs = vec::to_owned(attrs.tail());
atys = atys.tail();
attrs = attrs.tail();
}
while i < n {
@ -133,12 +133,12 @@ pub impl FnType {
ret_ty: TypeRef,
llwrapfn: ValueRef,
llargbundle: ValueRef) {
let mut atys = /*bad*/copy self.arg_tys;
let mut attrs = /*bad*/copy self.attrs;
let mut atys: &[LLVMType] = self.arg_tys;
let mut attrs: &[option::Option<Attribute>] = self.attrs;
let mut j = 0u;
let llretptr = if self.sret {
atys = vec::to_owned(atys.tail());
attrs = vec::to_owned(attrs.tail());
atys = atys.tail();
attrs = attrs.tail();
j = 1u;
get_param(llwrapfn, 0u)
} else if self.ret_ty.cast {

View File

@ -3661,8 +3661,8 @@ pub fn check_intrinsic_type(ccx: @mut CrateCtxt, it: @ast::foreign_item) {
~"bswap64" => (0, ~[ ty::mk_i64() ], ty::mk_i64()),
ref other => {
tcx.sess.span_err(it.span,
~"unrecognized intrinsic function: `" +
(*other) + "`");
fmt!("unrecognized intrinsic function: `%s`",
*other));
return;
}
};

View File

@ -323,8 +323,8 @@ fn check_main_fn_ty(ccx: @mut CrateCtxt,
}
_ => {
tcx.sess.span_bug(main_span,
~"main has a non-function type: found `" +
ppaux::ty_to_str(tcx, main_t) + "`");
fmt!("main has a non-function type: found `%s`",
ppaux::ty_to_str(tcx, main_t)));
}
}
}
@ -372,8 +372,8 @@ fn check_start_fn_ty(ccx: @mut CrateCtxt,
}
_ => {
tcx.sess.span_bug(start_span,
~"start has a non-function type: found `" +
ppaux::ty_to_str(tcx, start_t) + "`");
fmt!("start has a non-function type: found `%s`",
ppaux::ty_to_str(tcx, start_t)));
}
}
}

View File

@ -416,7 +416,7 @@ pub fn ty_to_str(cx: ctxt, typ: t) -> ~str {
ty_rptr(r, ref tm) => {
region_to_str_space(cx, "&", r) + mt_to_str(cx, tm)
}
ty_unboxed_vec(ref tm) => { ~"unboxed_vec<" + mt_to_str(cx, tm) + ">" }
ty_unboxed_vec(ref tm) => { fmt!("unboxed_vec<%s>", mt_to_str(cx, tm)) }
ty_type => ~"type",
ty_tup(ref elems) => {
let strs = elems.map(|elem| ty_to_str(cx, *elem));

View File

@ -272,12 +272,11 @@ fn run_cmd(repl: &mut Repl, _in: @io::Reader, _out: @io::Writer,
}
~"help" => {
println(
~":{\\n ..lines.. \\n:}\\n - execute multiline command\n" +
":load <crate> ... - \
loads given crates as dynamic libraries\n" +
":clear - clear the bindings\n" +
":exit - exit from the repl\n" +
":help - show this message");
":{\\n ..lines.. \\n:}\\n - execute multiline command\n\
:load <crate> ... - loads given crates as dynamic libraries\n\
:clear - clear the bindings\n\
:exit - exit from the repl\n\
:help - show this message");
}
~"load" => {
let mut loaded_crates: ~[~str] = ~[];

View File

@ -145,7 +145,7 @@ pub mod win32 {
pub fn as_utf16_p<T>(s: &str, f: &fn(*u16) -> T) -> T {
let mut t = str::to_utf16(s);
// Null terminate before passing on.
t += ~[0u16];
t += [0u16];
vec::as_imm_buf(t, |buf, _len| f(buf))
}
}
@ -437,8 +437,7 @@ fn dup2(src: c_int, dst: c_int) -> c_int {
/// Returns the proper dll filename for the given basename of a file.
pub fn dll_filename(base: &str) -> ~str {
return str::to_owned(DLL_PREFIX) + str::to_owned(base) +
str::to_owned(DLL_SUFFIX)
fmt!("%s%s%s", DLL_PREFIX, base, DLL_SUFFIX)
}
/// Optionally returns the filesystem path to the current executable which is

View File

@ -448,7 +448,8 @@ fn scan_number(c: char, rdr: @mut StringReader) -> token::Token {
is_float = true;
bump(rdr);
let dec_part = scan_digits(rdr, 10u);
num_str += ~"." + dec_part;
num_str += ".";
num_str += dec_part;
}
if is_float {
match base {

View File

@ -26,8 +26,8 @@ fn print_complements() {
let all = ~[Blue, Red, Yellow];
for vec::each(all) |aa| {
for vec::each(all) |bb| {
io::println(show_color(*aa) + ~" + " + show_color(*bb) +
~" -> " + show_color(transform(*aa, *bb)));
io::println(show_color(*aa) + " + " + show_color(*bb) +
" -> " + show_color(transform(*aa, *bb)));
}
}
}
@ -50,7 +50,7 @@ fn show_color(cc: color) -> ~str {
fn show_color_list(set: ~[color]) -> ~str {
let mut out = ~"";
for vec::eachi(set) |_ii, col| {
out += ~" ";
out += " ";
out += show_color(*col);
}
return out;
@ -82,7 +82,7 @@ fn show_number(nn: uint) -> ~str {
while num != 0 {
dig = num % 10;
num = num / 10;
out = show_digit(dig) + ~" " + out;
out = show_digit(dig) + " " + out;
}
return out;
@ -131,8 +131,8 @@ fn creature(
}
option::None => {
// log creatures met and evil clones of self
let report = fmt!("%u", creatures_met) + ~" " +
show_number(evil_clones_met);
let report = fmt!("%u %s",
creatures_met, show_number(evil_clones_met));
to_rendezvous_log.send(report);
break;
}

View File

@ -49,7 +49,7 @@ fn make_cumulative(aa: ~[AminoAcids]) -> ~[AminoAcids] {
let mut ans: ~[AminoAcids] = ~[];
for aa.each |a| {
cp += a.prob;
ans += ~[AminoAcids {ch: a.ch, prob: cp}];
ans += [AminoAcids {ch: a.ch, prob: cp}];
}
return ans;
}
@ -72,7 +72,7 @@ fn make_random_fasta(wr: @io::Writer,
desc: ~str,
genelist: ~[AminoAcids],
n: int) {
wr.write_line(~">" + id + ~" " + desc);
wr.write_line(~">" + id + " " + desc);
let mut rng = rand::rng();
let rng = @mut MyRandom {
last: rng.next()
@ -91,7 +91,7 @@ fn make_random_fasta(wr: @io::Writer,
fn make_repeat_fasta(wr: @io::Writer, id: ~str, desc: ~str, s: ~str, n: int) {
unsafe {
wr.write_line(~">" + id + ~" " + desc);
wr.write_line(~">" + id + " " + desc);
let mut op: ~str = ~"";
let sl: uint = str::len(s);
for uint::range(0u, n as uint) |i| {
@ -139,13 +139,13 @@ fn main() {
make_cumulative(~[acid('a', 30u32), acid('c', 20u32), acid('g', 20u32),
acid('t', 30u32)]);
let alu: ~str =
~"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG" +
~"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA" +
~"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT" +
~"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA" +
~"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG" +
~"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC" +
~"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
~"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG\
GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA\
CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT\
ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA\
GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG\
AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC\
AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
make_repeat_fasta(writer, ~"ONE", ~"Homo sapiens alu", alu, n * 2);
make_random_fasta(writer, ~"TWO", ~"IUB ambiguity codes", iub, n * 3);
make_random_fasta(writer, ~"THREE",

View File

@ -103,7 +103,7 @@ pub impl Sudoku {
for u8::range(0u8, 9u8) |row| {
for u8::range(0u8, 9u8) |col| {
let color = self.grid[row][col];
if color == 0u8 { work += ~[(row, col)]; }
if color == 0u8 { work += [(row, col)]; }
}
}

View File

@ -96,7 +96,7 @@ fn recurse_or_fail(depth: int, st: Option<State>) {
fn_box: || @Cons((), fn_box()),
tuple: (@Cons((), st.tuple.first()),
~Cons((), @*st.tuple.second())),
vec: st.vec + ~[@Cons((), *st.vec.last())],
vec: st.vec + [@Cons((), *st.vec.last())],
res: r(@Cons((), st.res._l))
}
}

View File

@ -26,7 +26,7 @@ impl to_str for int {
impl<T:to_str> to_str for ~[T] {
fn to_str(&self) -> ~str {
~"[" + str::connect(vec::map(*self, |e| e.to_str() ), ~", ") + ~"]"
~"[" + str::connect(vec::map(*self, |e| e.to_str() ), ", ") + "]"
}
}