Skip to content

Commit 1d17c21

Browse files
committed
Rewrite path::Display to reduce unnecessary allocation
1 parent 086c0dd commit 1d17c21

File tree

6 files changed

+24
-50
lines changed

6 files changed

+24
-50
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/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/path/mod.rs

Lines changed: 12 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).into_owned()
512-
}
513-
} else {
514-
str::from_utf8_lossy(self.path.as_vec()).into_owned()
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

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/libsyntax/parse/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4200,10 +4200,10 @@ impl Parser {
42004200
let mut err = ~"circular modules: ";
42014201
let len = included_mod_stack.get().len();
42024202
for p in included_mod_stack.get().slice(i, len).iter() {
4203-
p.display().with_str(|s| err.push_str(s));
4203+
err.push_str(p.display().as_maybe_owned().as_slice());
42044204
err.push_str(" -> ");
42054205
}
4206-
path.display().with_str(|s| err.push_str(s));
4206+
err.push_str(path.display().as_maybe_owned().as_slice());
42074207
self.span_fatal(id_sp, err);
42084208
}
42094209
None => ()

0 commit comments

Comments
 (0)