Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce memory use of sty #19549

Merged
merged 14 commits into from
Dec 29, 2014
Merged

Reduce memory use of sty #19549

merged 14 commits into from
Dec 29, 2014

Conversation

huonw
Copy link
Member

@huonw huonw commented Dec 5, 2014

This takes building librustc/lib.rs from using 696 MB to 588 (rustc --no-trans), and 1.99 GB to 1.87 (rustc -O). It also reduces sty down to 32 bytes on platforms with 64-bit pointers, at the expense of some more side-tables in ctxt. I'm sure there's more gains to be had from reducing the size of the side tables (e.g. by making the actual things they're storing smaller).

r? @nikomatsakis

@huonw
Copy link
Member Author

huonw commented Dec 5, 2014

My memory measurements are very imprecise (using only GNU time).

Here's the state of sty after this PR:

src/librustc/middle/ty.rs:1145:1: 1182:2 note: total size: 32 bytes
src/librustc/middle/ty.rs:1145 pub enum sty<'tcx> {
src/librustc/middle/ty.rs:1146     ty_bool,
src/librustc/middle/ty.rs:1147     ty_char,
src/librustc/middle/ty.rs:1148     ty_int(ast::IntTy),
src/librustc/middle/ty.rs:1149     ty_uint(ast::UintTy),
src/librustc/middle/ty.rs:1150     ty_float(ast::FloatTy),
                               ...
src/librustc/middle/ty.rs:1146:5: 1146:12 note: variant data: 0 bytes
src/librustc/middle/ty.rs:1146     ty_bool,
                                   ^~~~~~~
src/librustc/middle/ty.rs:1147:5: 1147:12 note: variant data: 0 bytes
src/librustc/middle/ty.rs:1147     ty_char,
                                   ^~~~~~~
src/librustc/middle/ty.rs:1148:5: 1148:23 note: variant data: 1 bytes
src/librustc/middle/ty.rs:1148     ty_int(ast::IntTy),
                                   ^~~~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1149:5: 1149:25 note: variant data: 1 bytes
src/librustc/middle/ty.rs:1149     ty_uint(ast::UintTy),
                                   ^~~~~~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1150:5: 1150:27 note: variant data: 1 bytes
src/librustc/middle/ty.rs:1150     ty_float(ast::FloatTy),
                                   ^~~~~~~~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1158:5: 1158:39 note: variant data: 16 bytes
src/librustc/middle/ty.rs:1158     ty_enum(DefId, &'tcx Substs<'tcx>),
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1159:5: 1159:22 note: variant data: 8 bytes
src/librustc/middle/ty.rs:1159     ty_uniq(Ty<'tcx>),
                                   ^~~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1160:5: 1160:11 note: variant data: 0 bytes
src/librustc/middle/ty.rs:1160     ty_str,
                                   ^~~~~~
src/librustc/middle/ty.rs:1161:5: 1161:35 note: variant data: 24 bytes
src/librustc/middle/ty.rs:1161     ty_vec(Ty<'tcx>, Option<uint>), // Second field is length.
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1162:5: 1162:21 note: variant data: 16 bytes
src/librustc/middle/ty.rs:1162     ty_ptr(mt<'tcx>),
                                   ^~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1163:5: 1163:36 note: variant data: 24 bytes
src/librustc/middle/ty.rs:1163     ty_rptr(&'tcx Region, mt<'tcx>),
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1164:5: 1164:37 note: variant data: 8 bytes
src/librustc/middle/ty.rs:1164     ty_bare_fn(&'tcx BareFnTy<'tcx>),
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1165:5: 1165:37 note: variant data: 8 bytes
src/librustc/middle/ty.rs:1165     ty_closure(Box<ClosureTy<'tcx>>),
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1166:5: 1166:33 note: variant data: 8 bytes
src/librustc/middle/ty.rs:1166     ty_trait(Box<TyTrait<'tcx>>),
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1167:5: 1167:41 note: variant data: 16 bytes
src/librustc/middle/ty.rs:1167     ty_struct(DefId, &'tcx Substs<'tcx>),
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1168:5: 1168:64 note: variant data: 24 bytes
src/librustc/middle/ty.rs:1168     ty_unboxed_closure(DefId, &'tcx Region, &'tcx Substs<'tcx>),
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1169:5: 1169:26 note: variant data: 24 bytes
src/librustc/middle/ty.rs:1169     ty_tup(Vec<Ty<'tcx>>),
                                   ^~~~~~~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1171:5: 1171:22 note: variant data: 16 bytes
src/librustc/middle/ty.rs:1171     ty_param(ParamTy), // type parameter
                                   ^~~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1172:5: 1172:22 note: variant data: 8 bytes
src/librustc/middle/ty.rs:1172     ty_open(Ty<'tcx>), // A deref'ed fat pointer, i.e., a dynamically sized value
                                   ^~~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1178:5: 1178:22 note: variant data: 8 bytes
src/librustc/middle/ty.rs:1178     ty_infer(InferTy), // something used only during inference/typeck
                                   ^~~~~~~~~~~~~~~~~
src/librustc/middle/ty.rs:1179:5: 1179:11 note: variant data: 0 bytes
src/librustc/middle/ty.rs:1179     ty_err, // Also only used during inference/typeck, to represent
                                   ^~~~~~

@nikomatsakis
Copy link
Contributor

Nice!

@huonw huonw force-pushed the middle-ty-2 branch 3 times, most recently from 70a99e2 to fe3a98f Compare December 5, 2014 22:06
@arielb1
Copy link
Contributor

arielb1 commented Dec 8, 2014

`ty_contents

@huonw
Copy link
Member Author

huonw commented Dec 8, 2014

@arielb1, did your comment get truncated?

@arielb1
Copy link
Contributor

arielb1 commented Dec 9, 2014

@huonw

Sure. But I also mixed up TyContents and TypeFlags so it was irrelevant (only the latter is in TyS, although the former is still a u64 for no reason).

By the way, now that TraitRef is Copy and only 16 bytes long, it may be worth it to stop using Rc<TraitRef> everywhere.

@nikomatsakis
Copy link
Contributor

@huonw needs rebase

huonw added 13 commits December 29, 2014 23:55
This current inflates memory use more than 3 times.
This cuts memory use dramatically from the previous commit, and reduces
use overall. E.g. the memory usage of `rustc -O librustc/lib.rs` seems
to drop 100MB from 1.98GB to 1.88GB (on one run anyway).
This reduces memory use for building librustc with -O from 1.88 to 1.76
GB.
This cuts the ty_bare_fn variant to 48 bytes rather than 56. There
doesn't seem to be a noticable memory usage decrease from this.
This allows expanding how many arenas exist without users having to
care, since they are all created with CtxtArena::new().
This makes sty only 32 bytes on machines with 64-bit pointers.
(on platforms with 64-bit pointers.)

The StmtMac variant is rather large and also fairly rare, so let's
optimise the common case.
This replaces required the RUST_LOG=... invocation to make it much more
user friendly.
I've totally mangled the history with these rebases; sorry, future programmer!
bors added a commit that referenced this pull request Dec 29, 2014
This takes building `librustc/lib.rs` from using 696 MB to 588 (`rustc --no-trans`), and 1.99 GB to 1.87 (`rustc -O`). It also reduces `sty` down to 32 bytes on platforms with 64-bit pointers, at the expense of some more side-tables in `ctxt`. I'm sure there's more gains to be had from reducing the size of the side tables (e.g. by making the actual things they're storing smaller).

r? @nikomatsakis
@bors bors merged commit 91db254 into rust-lang:master Dec 29, 2014
@tomprogrammer
Copy link
Contributor

@huonw Which option did you enable to get the size of the enums/variants told by the compiler?

@huonw
Copy link
Member Author

huonw commented Dec 31, 2014

@tomprogrammer, this PR added the -Z print-enum-sizes flag which generates that output.

@huonw huonw deleted the middle-ty-2 branch December 31, 2014 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants