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

Stop keeping metadata in memory before writing it to disk #96358

Closed
cjgillot opened this issue Apr 24, 2022 · 2 comments · Fixed by #96544
Closed

Stop keeping metadata in memory before writing it to disk #96358

cjgillot opened this issue Apr 24, 2022 · 2 comments · Fixed by #96544
Assignees
Labels
A-metadata Area: Crate metadata E-help-wanted Call for participation: Help is requested to fix this issue. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

Comments

@cjgillot
Copy link
Contributor

cjgillot commented Apr 24, 2022

rustc_metadata encodes all the crate information into an in-memory buffer in EncodedMetadata. This in-memory buffer is then passed around to be written to disk, as an rmeta file in rustc_interface::encode_and_write_metadata and/or in an rlib/dylib by codegen. Codegen wraps metadata into an object file using the object crate to be nice to linkers that will manipulate the rlib/dylib.

Keeping metadata in-memory increases the amount of memory used by rustc significantly. We want to investigate saving this memory by writing metadata to disk early, reading it back if required.

Instructions:

  • Move rustc_codegen_ssa::back::link::emit_metadata and rustc_codegen_ssa::METADATA_FILENAME to new module rustc_metadata::fs.
  • Move rustc_interface::passes::encode_and_write_metadata and rustc_metadata::util::non_durable_rename to rustc_metadata::fs.
  • Write metadata into the temporary file in encode_and_write_metadata even if !need_metadata_file.
  • Use a rustc_serialize::opaque::FileEncoder in rustc_metadata::rmeta::encoder instead of an opaque::Encoder, it should encode directly to the temporary file, and re-read this temporary file to build the EncodedMetadata.
  • Change EncodedMetadata to hold a Mmap of the on-disk metadata instead of a Vec. This Mmap can be of the temporary file, or of the proper renamed output file. In the former case, EncodedMetadata should carry the MaybeTempDir to avoid deleting the temporary directory while accessing the Mmap.

Extra: the current implementations of create_rmeta_file and create_compressed_metadata_file in rustc_codegen_ssa::back::metadata buffer everything to a vector before writing it all at once. This could be refactored to write directly to the file using a BufWriter.

Please reach out on Zulip if you have any questions. Preferably on a public stream so experts on codegen can weigh in when necessary.

@cjgillot cjgillot added E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. E-help-wanted Call for participation: Help is requested to fix this issue. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. labels Apr 24, 2022
@m-ysk
Copy link
Contributor

m-ysk commented Apr 24, 2022

@rustbot claim

@bjorn3
Copy link
Member

bjorn3 commented Apr 24, 2022

Write metadata into the temporary file in encode_and_write_metadata even if !need_metadata_file.

Preferably only do this if either need_metadata_file or should_codegen().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-metadata Area: Crate metadata E-help-wanted Call for participation: Help is requested to fix this issue. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants