Skip to content

Commit 5b4a141

Browse files
committed
auto merge of #12616 : alexcrichton/rust/size, r=huonw
I've been playing around with code size when linking to libstd recently, and these were some findings I found that really helped code size. I started out by eliminating all I/O implementations from libnative and instead just return an unimplemented error. In doing so, a `fn main() {}` executable was ~378K before this patch, and about 170K after the patch. These size wins are all pretty minor, but they all seemed pretty reasonable to me. With native I/O not stubbed out, this takes the size of an LTO executable from 675K to 400K.
2 parents 84ebf74 + ddc1c21 commit 5b4a141

File tree

6 files changed

+119
-114
lines changed

6 files changed

+119
-114
lines changed

src/libnative/io/process.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ fn spawn_process_os(config: p::ProcessConfig,
470470
Err(e) => {
471471
assert!(e.kind == io::BrokenPipe ||
472472
e.kind == io::EndOfFile,
473-
"unexpected error: {:?}", e);
473+
"unexpected error: {}", e);
474474
Ok(SpawnProcessResult {
475475
pid: pid,
476476
handle: ptr::null()
@@ -744,7 +744,7 @@ fn waitpid(pid: pid_t) -> p::ProcessExit {
744744

745745
let mut status = 0 as c_int;
746746
match retry(|| unsafe { wait::waitpid(pid, &mut status, 0) }) {
747-
-1 => fail!("unknown waitpid error: {:?}", super::last_error()),
747+
-1 => fail!("unknown waitpid error: {}", super::last_error()),
748748
_ => {
749749
if imp::WIFEXITED(status) {
750750
p::ExitStatus(imp::WEXITSTATUS(status) as int)

src/libstd/macros.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,14 @@ macro_rules! fail(
149149
// function to pass to format_args!, *and* we need the
150150
// file and line numbers right here; so an inner bare fn
151151
// is our only choice.
152-
#[inline]
152+
//
153+
// LLVM doesn't tend to inline this, presumably because begin_unwind_fmt
154+
// is #[cold] and #[inline(never)] and because this is flagged as cold
155+
// as returning !. We really do want this to be inlined, however,
156+
// because it's just a tiny wrapper. Small wins (156K to 149K in size)
157+
// were seen when forcing this to be inlined, and that number just goes
158+
// up with the number of calls to fail!()
159+
#[inline(always)]
153160
fn run_fmt(fmt: &::std::fmt::Arguments) -> ! {
154161
::std::rt::begin_unwind_fmt(fmt, file!(), line!())
155162
}

src/libstd/num/strconv.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -532,19 +532,19 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Div<T,T>+
532532
) -> Option<T> {
533533
match exponent {
534534
ExpDec if radix >= DIGIT_E_RADIX // decimal exponent 'e'
535-
=> fail!("from_str_bytes_common: radix {:?} incompatible with \
535+
=> fail!("from_str_bytes_common: radix {} incompatible with \
536536
use of 'e' as decimal exponent", radix),
537537
ExpBin if radix >= DIGIT_P_RADIX // binary exponent 'p'
538-
=> fail!("from_str_bytes_common: radix {:?} incompatible with \
538+
=> fail!("from_str_bytes_common: radix {} incompatible with \
539539
use of 'p' as binary exponent", radix),
540540
_ if special && radix >= DIGIT_I_RADIX // first digit of 'inf'
541-
=> fail!("from_str_bytes_common: radix {:?} incompatible with \
541+
=> fail!("from_str_bytes_common: radix {} incompatible with \
542542
special values 'inf' and 'NaN'", radix),
543543
_ if (radix as int) < 2
544-
=> fail!("from_str_bytes_common: radix {:?} to low, \
544+
=> fail!("from_str_bytes_common: radix {} to low, \
545545
must lie in the range [2, 36]", radix),
546546
_ if (radix as int) > 36
547-
=> fail!("from_str_bytes_common: radix {:?} to high, \
547+
=> fail!("from_str_bytes_common: radix {} to high, \
548548
must lie in the range [2, 36]", radix),
549549
_ => ()
550550
}

src/libstd/rt/crate_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn version(crate_map: &CrateMap) -> i32 {
123123

124124
fn do_iter_crate_map<'a>(
125125
crate_map: &'a CrateMap<'a>,
126-
f: |&ModEntry|,
126+
f: |&'a ModEntry<'a>|,
127127
visited: &mut ~[*CrateMap<'a>]) {
128128
let raw = crate_map as *CrateMap<'a>;
129129
if visited.bsearch(|a| (*a as uint).cmp(&(raw as uint))).is_some() {
@@ -149,7 +149,7 @@ fn do_iter_crate_map<'a>(
149149
}
150150

151151
/// Iterates recursively over `crate_map` and all child crate maps
152-
pub fn iter_crate_map<'a>(crate_map: &'a CrateMap<'a>, f: |&ModEntry|) {
152+
pub fn iter_crate_map<'a>(crate_map: &'a CrateMap<'a>, f: |&'a ModEntry<'a>|) {
153153
let mut v = ~[];
154154
do_iter_crate_map(crate_map, f, &mut v);
155155
}

0 commit comments

Comments
 (0)