Skip to content

Commit 8d0fad5

Browse files
committed
Shrink LiveNode.
`Liveness::users` is a vector that is occasionally enormous. For example, doing a "clean incremental" check build of `inflate`, there is one instance that represents 5,499 live nodes and 1087 vars, which requires 5,977,413 entries. At 24 bytes per entry, that is 143MB. This patch changes LiveNode from a usize to a u32. On 64-bit machines that halves the size of these entries, significantly reducing peak memory usage and memory traffic, and speeding up "clean incremental" builds of `inflate` by about 10%.
1 parent 4c26e2e commit 8d0fad5

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

src/librustc/middle/liveness.rs

+14-20
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
//! enclosing function. On the way down the tree, it identifies those AST
5252
//! nodes and variable IDs that will be needed for the liveness analysis
5353
//! and assigns them contiguous IDs. The liveness id for an AST node is
54-
//! called a `live_node` (it's a newtype'd usize) and the id for a variable
55-
//! is called a `variable` (another newtype'd usize).
54+
//! called a `live_node` (it's a newtype'd u32) and the id for a variable
55+
//! is called a `variable` (another newtype'd u32).
5656
//!
5757
//! On the way back up the tree, as we are about to exit from a function
5858
//! declaration we allocate a `liveness` instance. Now that we know
@@ -112,7 +112,7 @@ use lint;
112112
use util::nodemap::{NodeMap, NodeSet};
113113

114114
use std::collections::VecDeque;
115-
use std::{fmt, usize};
115+
use std::{fmt, u32};
116116
use std::io::prelude::*;
117117
use std::io;
118118
use std::rc::Rc;
@@ -134,23 +134,17 @@ enum LoopKind<'a> {
134134
}
135135

136136
#[derive(Copy, Clone, PartialEq)]
137-
struct Variable(usize);
137+
struct Variable(u32);
138138

139-
#[derive(Copy, PartialEq)]
140-
struct LiveNode(usize);
139+
#[derive(Copy, Clone, PartialEq)]
140+
struct LiveNode(u32);
141141

142142
impl Variable {
143-
fn get(&self) -> usize { let Variable(v) = *self; v }
143+
fn get(&self) -> usize { self.0 as usize }
144144
}
145145

146146
impl LiveNode {
147-
fn get(&self) -> usize { let LiveNode(v) = *self; v }
148-
}
149-
150-
impl Clone for LiveNode {
151-
fn clone(&self) -> LiveNode {
152-
LiveNode(self.get())
153-
}
147+
fn get(&self) -> usize { self.0 as usize }
154148
}
155149

156150
#[derive(Copy, Clone, PartialEq, Debug)]
@@ -233,11 +227,11 @@ impl fmt::Debug for Variable {
233227

234228
impl LiveNode {
235229
fn is_valid(&self) -> bool {
236-
self.get() != usize::MAX
230+
self.0 != u32::MAX
237231
}
238232
}
239233

240-
fn invalid_node() -> LiveNode { LiveNode(usize::MAX) }
234+
fn invalid_node() -> LiveNode { LiveNode(u32::MAX) }
241235

242236
struct CaptureInfo {
243237
ln: LiveNode,
@@ -285,7 +279,7 @@ impl<'a, 'tcx> IrMaps<'a, 'tcx> {
285279
}
286280

287281
fn add_live_node(&mut self, lnk: LiveNodeKind) -> LiveNode {
288-
let ln = LiveNode(self.num_live_nodes);
282+
let ln = LiveNode(self.num_live_nodes as u32);
289283
self.lnks.push(lnk);
290284
self.num_live_nodes += 1;
291285

@@ -303,7 +297,7 @@ impl<'a, 'tcx> IrMaps<'a, 'tcx> {
303297
}
304298

305299
fn add_variable(&mut self, vk: VarKind) -> Variable {
306-
let v = Variable(self.num_vars);
300+
let v = Variable(self.num_vars as u32);
307301
self.var_kinds.push(vk);
308302
self.num_vars += 1;
309303

@@ -708,7 +702,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
708702
for var_idx in 0..self.ir.num_vars {
709703
let idx = node_base_idx + var_idx;
710704
if test(idx).is_valid() {
711-
write!(wr, " {:?}", Variable(var_idx))?;
705+
write!(wr, " {:?}", Variable(var_idx as u32))?;
712706
}
713707
}
714708
Ok(())
@@ -848,7 +842,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
848842
debug!("^^ liveness computation results for body {} (entry={:?})",
849843
{
850844
for ln_idx in 0..self.ir.num_live_nodes {
851-
debug!("{:?}", self.ln_str(LiveNode(ln_idx)));
845+
debug!("{:?}", self.ln_str(LiveNode(ln_idx as u32)));
852846
}
853847
body.id
854848
},

0 commit comments

Comments
 (0)