Skip to content

Commit 5acc998

Browse files
committed
auto merge of #12098 : kballard/rust/from_utf8_lossy_tweak, r=huonw
MaybeOwned allows from_utf8_lossy to avoid allocation if there are no invalid bytes in the input. Before: ``` test str::bench::from_utf8_lossy_100_ascii ... bench: 183 ns/iter (+/- 5) test str::bench::from_utf8_lossy_100_invalid ... bench: 341 ns/iter (+/- 15) test str::bench::from_utf8_lossy_100_multibyte ... bench: 227 ns/iter (+/- 13) test str::bench::from_utf8_lossy_invalid ... bench: 102 ns/iter (+/- 4) test str::bench::is_utf8_100_ascii ... bench: 2 ns/iter (+/- 0) test str::bench::is_utf8_100_multibyte ... bench: 2 ns/iter (+/- 0) ``` Now: ``` test str::bench::from_utf8_lossy_100_ascii ... bench: 96 ns/iter (+/- 4) test str::bench::from_utf8_lossy_100_invalid ... bench: 318 ns/iter (+/- 10) test str::bench::from_utf8_lossy_100_multibyte ... bench: 105 ns/iter (+/- 2) test str::bench::from_utf8_lossy_invalid ... bench: 105 ns/iter (+/- 2) test str::bench::is_utf8_100_ascii ... bench: 2 ns/iter (+/- 0) test str::bench::is_utf8_100_multibyte ... bench: 2 ns/iter (+/- 0) ```
2 parents 548b8ce + 1d17c21 commit 5acc998

File tree

20 files changed

+398
-421
lines changed

20 files changed

+398
-421
lines changed

src/compiletest/runtest.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,7 @@ fn check_error_patterns(props: &TestProps,
442442
testfile: &Path,
443443
ProcRes: &ProcRes) {
444444
if props.error_patterns.is_empty() {
445-
testfile.display().with_str(|s| {
446-
fatal(~"no error pattern specified in " + s);
447-
})
445+
fatal(~"no error pattern specified in " + testfile.display().as_maybe_owned().as_slice());
448446
}
449447
450448
if ProcRes.status.success() {

src/etc/vim/syntax/rust.vim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ syn keyword rustTrait Primitive Int Float ToStrRadix ToPrimitive FromPrimitive
8787
syn keyword rustTrait GenericPath Path PosixPath WindowsPath
8888
syn keyword rustTrait RawPtr
8989
syn keyword rustTrait Buffer Writer Reader Seek
90-
syn keyword rustTrait SendStr SendStrOwned SendStrStatic IntoSendStr
91-
syn keyword rustTrait Str StrVector StrSlice OwnedStr
90+
syn keyword rustTrait Str StrVector StrSlice OwnedStr IntoMaybeOwned
9291
syn keyword rustTrait IterBytes
9392
syn keyword rustTrait ToStr IntoStr
9493
syn keyword rustTrait CloneableTuple ImmutableTuple

src/libextra/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -893,8 +893,8 @@ pub fn run_test(force_ignore: bool,
893893
spawn(proc() {
894894
let mut task = task::task();
895895
task.name(match desc.name {
896-
DynTestName(ref name) => SendStrOwned(name.clone()),
897-
StaticTestName(name) => SendStrStatic(name),
896+
DynTestName(ref name) => name.to_owned().into_maybe_owned(),
897+
StaticTestName(name) => name.into_maybe_owned()
898898
});
899899
let result_future = task.future_result();
900900
task.spawn(testfn);

src/libgreen/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ pub fn run(main: proc()) -> int {
257257
let (port, chan) = Chan::new();
258258
let mut opts = TaskOpts::new();
259259
opts.notify_chan = Some(chan);
260-
opts.name = Some(SendStrStatic("<main>"));
260+
opts.name = Some("<main>".into_maybe_owned());
261261
pool.spawn(opts, main);
262262

263263
// Wait for the main task to return, and set the process error code

src/libgreen/task.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ mod tests {
510510
#[test]
511511
fn smoke_opts() {
512512
let mut opts = TaskOpts::new();
513-
opts.name = Some(SendStrStatic("test"));
513+
opts.name = Some("test".into_maybe_owned());
514514
opts.stack_size = Some(20 * 4096);
515515
let (p, c) = Chan::new();
516516
opts.notify_chan = Some(c);

src/libnative/task.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ mod tests {
294294
#[test]
295295
fn smoke_opts() {
296296
let mut opts = TaskOpts::new();
297-
opts.name = Some(SendStrStatic("test"));
297+
opts.name = Some("test".into_maybe_owned());
298298
opts.stack_size = Some(20 * 4096);
299299
let (p, c) = Chan::new();
300300
opts.notify_chan = Some(c);

src/librustdoc/html/render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ impl<'a> SourceCollector<'a> {
436436
cur.push(p.filename().expect("source has no filename") + bytes!(".html"));
437437
let mut w = BufferedWriter::new(if_ok!(File::create(&cur)));
438438

439-
let title = cur.filename_display().with_str(|s| format!("{} -- source", s));
439+
let title = format!("{} -- source", cur.filename_display());
440440
let page = layout::Page {
441441
title: title,
442442
ty: "source",

src/libstd/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ pub mod vec_ng;
117117
pub mod str;
118118

119119
pub mod ascii;
120-
pub mod send_str;
121120

122121
pub mod ptr;
123122
pub mod owned;

src/libstd/path/mod.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use fmt;
7070
use iter::Iterator;
7171
use option::{Option, None, Some};
7272
use str;
73-
use str::{OwnedStr, Str, StrSlice};
73+
use str::{MaybeOwned, OwnedStr, Str, StrSlice, from_utf8_lossy};
7474
use to_str::ToStr;
7575
use vec;
7676
use vec::{CloneableVector, OwnedCloneableVector, OwnedVector, Vector};
@@ -495,7 +495,7 @@ pub struct Display<'a, P> {
495495

496496
impl<'a, P: GenericPath> fmt::Show for Display<'a, P> {
497497
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
498-
self.with_str(|s| f.pad(s))
498+
self.as_maybe_owned().as_slice().fmt(f)
499499
}
500500
}
501501

@@ -505,33 +505,25 @@ impl<'a, P: GenericPath> ToStr for Display<'a, P> {
505505
/// If the path is not UTF-8, invalid sequences with be replaced with the
506506
/// unicode replacement char. This involves allocation.
507507
fn to_str(&self) -> ~str {
508-
if self.filename {
509-
match self.path.filename() {
510-
None => ~"",
511-
Some(v) => str::from_utf8_lossy(v)
512-
}
513-
} else {
514-
str::from_utf8_lossy(self.path.as_vec())
515-
}
508+
self.as_maybe_owned().into_owned()
516509
}
517510
}
518511

519512
impl<'a, P: GenericPath> Display<'a, P> {
520-
/// Provides the path as a string to a closure
513+
/// Returns the path as a possibly-owned string.
521514
///
522515
/// If the path is not UTF-8, invalid sequences will be replaced with the
523516
/// unicode replacement char. This involves allocation.
524517
#[inline]
525-
pub fn with_str<T>(&self, f: |&str| -> T) -> T {
526-
let opt = if self.filename { self.path.filename_str() }
527-
else { self.path.as_str() };
528-
match opt {
529-
Some(s) => f(s),
530-
None => {
531-
let s = self.to_str();
532-
f(s.as_slice())
518+
pub fn as_maybe_owned(&self) -> MaybeOwned<'a> {
519+
from_utf8_lossy(if self.filename {
520+
match self.path.filename() {
521+
None => &[],
522+
Some(v) => v
533523
}
534-
}
524+
} else {
525+
self.path.as_vec()
526+
})
535527
}
536528
}
537529

@@ -591,6 +583,23 @@ impl BytesContainer for CString {
591583
}
592584
}
593585

586+
impl<'a> BytesContainer for str::MaybeOwned<'a> {
587+
#[inline]
588+
fn container_as_bytes<'b>(&'b self) -> &'b [u8] {
589+
self.as_slice().as_bytes()
590+
}
591+
#[inline]
592+
fn container_into_owned_bytes(self) -> ~[u8] {
593+
self.into_owned().into_bytes()
594+
}
595+
#[inline]
596+
fn container_as_str<'b>(&'b self) -> Option<&'b str> {
597+
Some(self.as_slice())
598+
}
599+
#[inline]
600+
fn is_str(_: Option<str::MaybeOwned>) -> bool { true }
601+
}
602+
594603
#[inline(always)]
595604
fn contains_nul(v: &[u8]) -> bool {
596605
v.iter().any(|&x| x == 0)

src/libstd/path/posix.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -564,24 +564,16 @@ mod tests {
564564
macro_rules! t(
565565
($path:expr, $exp:expr) => (
566566
{
567-
let mut called = false;
568567
let path = Path::new($path);
569-
path.display().with_str(|s| {
570-
assert_eq!(s, $exp);
571-
called = true;
572-
});
573-
assert!(called);
568+
let mo = path.display().as_maybe_owned();
569+
assert_eq!(mo.as_slice(), $exp);
574570
}
575571
);
576572
($path:expr, $exp:expr, filename) => (
577573
{
578-
let mut called = false;
579574
let path = Path::new($path);
580-
path.filename_display().with_str(|s| {
581-
assert_eq!(s, $exp);
582-
called = true;
583-
});
584-
assert!(called);
575+
let mo = path.filename_display().as_maybe_owned();
576+
assert_eq!(mo.as_slice(), $exp);
585577
}
586578
)
587579
)

src/libstd/path/windows.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,20 +1278,12 @@ mod tests {
12781278
let path = Path::new(b!("\\"));
12791279
assert_eq!(path.filename_display().to_str(), ~"");
12801280

1281-
let mut called = false;
12821281
let path = Path::new("foo");
1283-
path.display().with_str(|s| {
1284-
assert_eq!(s, "foo");
1285-
called = true;
1286-
});
1287-
assert!(called);
1288-
called = false;
1282+
let mo = path.display().as_maybe_owned();
1283+
assert_eq!(mo.as_slice(), "foo");
12891284
let path = Path::new(b!("\\"));
1290-
path.filename_display().with_str(|s| {
1291-
assert_eq!(s, "");
1292-
called = true;
1293-
});
1294-
assert!(called);
1285+
let mo = path.filename_display().as_maybe_owned();
1286+
assert_eq!(mo.as_slice(), "");
12951287
}
12961288

12971289
#[test]

src/libstd/prelude.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ pub use num::{Primitive, Int, Float, ToStrRadix, ToPrimitive, FromPrimitive};
6363
pub use path::{GenericPath, Path, PosixPath, WindowsPath};
6464
pub use ptr::RawPtr;
6565
pub use io::{Buffer, Writer, Reader, Seek};
66-
pub use send_str::{SendStr, SendStrOwned, SendStrStatic, IntoSendStr};
67-
pub use str::{Str, StrVector, StrSlice, OwnedStr};
66+
pub use str::{Str, StrVector, StrSlice, OwnedStr, IntoMaybeOwned};
6867
pub use to_bytes::IterBytes;
6968
pub use to_str::{ToStr, IntoStr};
7069
pub use tuple::{CloneableTuple, ImmutableTuple};

src/libstd/rt/task.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rt::local::Local;
3030
use rt::local_heap::LocalHeap;
3131
use rt::rtio::LocalIo;
3232
use rt::unwind::Unwinder;
33-
use send_str::SendStr;
33+
use str::SendStr;
3434
use sync::arc::UnsafeArc;
3535
use sync::atomics::{AtomicUint, SeqCst};
3636
use task::{TaskResult, TaskOpts};

0 commit comments

Comments
 (0)