283 lines
16 KiB
HTML
283 lines
16 KiB
HTML
<!DOCTYPE HTML>
|
||
<html lang="en" class="light sidebar-visible" dir="ltr">
|
||
<head>
|
||
<!-- Book generated using mdBook -->
|
||
<meta charset="UTF-8">
|
||
<title>Memory management in rustc - Rust Compiler Development Guide</title>
|
||
|
||
|
||
<!-- Custom HTML head -->
|
||
|
||
<meta name="description" content="A guide to developing the Rust compiler (rustc)">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<meta name="theme-color" content="#ffffff">
|
||
|
||
<link rel="icon" href="favicon.svg">
|
||
<link rel="shortcut icon" href="favicon.png">
|
||
<link rel="stylesheet" href="css/variables.css">
|
||
<link rel="stylesheet" href="css/general.css">
|
||
<link rel="stylesheet" href="css/chrome.css">
|
||
<link rel="stylesheet" href="css/print.css" media="print">
|
||
|
||
<!-- Fonts -->
|
||
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
|
||
<link rel="stylesheet" href="fonts/fonts.css">
|
||
|
||
<!-- Highlight.js Stylesheets -->
|
||
<link rel="stylesheet" id="highlight-css" href="highlight.css">
|
||
<link rel="stylesheet" id="tomorrow-night-css" href="tomorrow-night.css">
|
||
<link rel="stylesheet" id="ayu-highlight-css" href="ayu-highlight.css">
|
||
|
||
<!-- Custom theme stylesheets -->
|
||
|
||
|
||
<!-- Provide site root and default themes to javascript -->
|
||
<script>
|
||
const path_to_root = "";
|
||
const default_light_theme = "light";
|
||
const default_dark_theme = "navy";
|
||
</script>
|
||
<!-- Start loading toc.js asap -->
|
||
<script src="toc.js"></script>
|
||
</head>
|
||
<body>
|
||
<div id="body-container">
|
||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
||
<script>
|
||
try {
|
||
let theme = localStorage.getItem('mdbook-theme');
|
||
let sidebar = localStorage.getItem('mdbook-sidebar');
|
||
|
||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
||
}
|
||
|
||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
||
}
|
||
} catch (e) { }
|
||
</script>
|
||
|
||
<!-- Set the theme before any content is loaded, prevents flash -->
|
||
<script>
|
||
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
|
||
let theme;
|
||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
||
if (theme === null || theme === undefined) { theme = default_theme; }
|
||
const html = document.documentElement;
|
||
html.classList.remove('light')
|
||
html.classList.add(theme);
|
||
html.classList.add("js");
|
||
</script>
|
||
|
||
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
|
||
|
||
<!-- Hide / unhide sidebar before it is displayed -->
|
||
<script>
|
||
let sidebar = null;
|
||
const sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
|
||
if (document.body.clientWidth >= 1080) {
|
||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
||
sidebar = sidebar || 'visible';
|
||
} else {
|
||
sidebar = 'hidden';
|
||
}
|
||
sidebar_toggle.checked = sidebar === 'visible';
|
||
html.classList.remove('sidebar-visible');
|
||
html.classList.add("sidebar-" + sidebar);
|
||
</script>
|
||
|
||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
||
<!-- populated by js -->
|
||
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
|
||
<noscript>
|
||
<iframe class="sidebar-iframe-outer" src="toc.html"></iframe>
|
||
</noscript>
|
||
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
|
||
<div class="sidebar-resize-indicator"></div>
|
||
</div>
|
||
</nav>
|
||
|
||
<div id="page-wrapper" class="page-wrapper">
|
||
|
||
<div class="page">
|
||
<div id="menu-bar-hover-placeholder"></div>
|
||
<div id="menu-bar" class="menu-bar sticky">
|
||
<div class="left-buttons">
|
||
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
||
<i class="fa fa-bars"></i>
|
||
</label>
|
||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
||
<i class="fa fa-paint-brush"></i>
|
||
</button>
|
||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
||
<li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
||
</ul>
|
||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
||
<i class="fa fa-search"></i>
|
||
</button>
|
||
</div>
|
||
|
||
<h1 class="menu-title">Rust Compiler Development Guide</h1>
|
||
|
||
<div class="right-buttons">
|
||
<a href="print.html" title="Print this book" aria-label="Print this book">
|
||
<i id="print-button" class="fa fa-print"></i>
|
||
</a>
|
||
<a href="https://github.com/rust-lang/rustc-dev-guide" title="Git repository" aria-label="Git repository">
|
||
<i id="git-repository-button" class="fa fa-github"></i>
|
||
</a>
|
||
<a href="https://github.com/rust-lang/rustc-dev-guide/edit/master/src/memory.md" title="Suggest an edit" aria-label="Suggest an edit">
|
||
<i id="git-edit-button" class="fa fa-edit"></i>
|
||
</a>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
<div id="search-wrapper" class="hidden">
|
||
<form id="searchbar-outer" class="searchbar-outer">
|
||
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
||
</form>
|
||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
||
<div id="searchresults-header" class="searchresults-header"></div>
|
||
<ul id="searchresults">
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
||
<script>
|
||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
||
});
|
||
</script>
|
||
|
||
<div id="content" class="content">
|
||
<main>
|
||
<h1 id="memory-management-in-rustc"><a class="header" href="#memory-management-in-rustc">Memory management in rustc</a></h1>
|
||
<p>Generally rustc tries to be pretty careful how it manages memory.
|
||
The compiler allocates <em>a lot</em> of data structures throughout compilation,
|
||
and if we are not careful, it will take a lot of time and space to do so.</p>
|
||
<p>One of the main way the compiler manages this is using <a href="https://en.wikipedia.org/wiki/Region-based_memory_management">arena</a>s and <a href="https://en.wikipedia.org/wiki/String_interning">interning</a>.</p>
|
||
<h2 id="arenas-and--interning"><a class="header" href="#arenas-and--interning">Arenas and Interning</a></h2>
|
||
<p>Since A LOT of data structures are created during compilation, for performance
|
||
reasons, we allocate them from a global memory pool.
|
||
Each are allocated once from a long-lived <em>arena</em>.
|
||
This is called <em>arena allocation</em>.
|
||
This system reduces allocations/deallocations of memory.
|
||
It also allows for easy comparison of types (more on types <a href="./ty.html">here</a>) for equality:
|
||
for each interned type <code>X</code>, we implemented <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Ty.html#implementations"><code>PartialEq</code> for X</a>,
|
||
so we can just compare pointers.
|
||
The <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.CtxtInterners.html#structfield.arena"><code>CtxtInterners</code></a> type contains a bunch of maps of interned types and the arena itself.</p>
|
||
<h3 id="example-tytykind"><a class="header" href="#example-tytykind">Example: <code>ty::TyKind</code></a></h3>
|
||
<p>Taking the example of <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/sty/type.TyKind.html"><code>ty::TyKind</code></a> which represents a type in the compiler (you
|
||
can read more <a href="./ty.html">here</a>). Each time we want to construct a type, the
|
||
compiler doesn’t naively allocate from the buffer. Instead, we check if that
|
||
type was already constructed. If it was, we just get the same pointer we had
|
||
before, otherwise we make a fresh pointer. With this schema if we want to know
|
||
if two types are the same, all we need to do is compare the pointers which is
|
||
efficient. <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/sty/type.TyKind.html"><code>ty::TyKind</code></a> should never be constructed on the stack, and it would be unusable
|
||
if done so.
|
||
You always allocate them from this arena and you always intern them so they are
|
||
unique.</p>
|
||
<p>At the beginning of the compilation we make a buffer and each time we need to allocate a type we use
|
||
some of this memory buffer. If we run out of space we get another one. The lifetime of that buffer
|
||
is <code>'tcx</code>. Our types are tied to that lifetime, so when compilation finishes all the memory related
|
||
to that buffer is freed and our <code>'tcx</code> references would be invalid.</p>
|
||
<p>In addition to types, there are a number of other arena-allocated data structures that you can
|
||
allocate, and which are found in this module. Here are a few examples:</p>
|
||
<ul>
|
||
<li><a href="./ty_module/generic_arguments.html#the-genericargs-type"><code>GenericArgs</code></a>, allocated with <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.mk_args"><code>mk_args</code></a> – this will intern a slice of types, often used
|
||
to specify the values to be substituted for generics args (e.g. <code>HashMap<i32, u32></code> would be
|
||
represented as a slice <code>&'tcx [tcx.types.i32, tcx.types.u32]</code>).</li>
|
||
<li><a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/type.TraitRef.html"><code>TraitRef</code></a>, typically passed by value – a <strong>trait reference</strong> consists of a reference to a trait
|
||
along with its various type parameters (including <code>Self</code>), like <code>i32: Display</code> (here, the def-id
|
||
would reference the <code>Display</code> trait, and the args would contain <code>i32</code>). Note that <code>def-id</code> is
|
||
defined and discussed in depth in the <a href="./ty_module/generic_arguments.html#adtdef-and-defid"><code>AdtDef and DefId</code></a> section.</li>
|
||
<li><a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Predicate.html"><code>Predicate</code></a> defines something the trait system has to prove (see <a href="./traits/resolution.html">traits</a> module).</li>
|
||
</ul>
|
||
<h2 id="the-tcx-and-how-it-uses-lifetimes"><a class="header" href="#the-tcx-and-how-it-uses-lifetimes">The <code>tcx</code> and how it uses lifetimes</a></h2>
|
||
<p>The typing context (<code>tcx</code>) is the central data structure in the compiler. It is the context that
|
||
you use to perform all manner of queries. The <code>struct</code> <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html"><code>TyCtxt</code></a> defines a reference to this shared
|
||
context:</p>
|
||
<pre><code class="language-rust ignore">tcx: TyCtxt<'tcx>
|
||
// ----
|
||
// |
|
||
// arena lifetime</code></pre>
|
||
<p>As you can see, the <code>TyCtxt</code> type takes a lifetime parameter. When you see a reference with a
|
||
lifetime like <code>'tcx</code>, you know that it refers to arena-allocated data (or data that lives as long as
|
||
the arenas, anyhow).</p>
|
||
<h3 id="a-note-on-lifetimes"><a class="header" href="#a-note-on-lifetimes">A Note On Lifetimes</a></h3>
|
||
<p>The Rust compiler is a fairly large program containing lots of big data
|
||
structures (e.g. the <a href="./ast-validation.html">Abstract Syntax Tree (AST)</a>, <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/index.html">High-Level Intermediate
|
||
Representation (<code>HIR</code>)</a>, and the type system) and as such, arenas and
|
||
references are heavily relied upon to minimize unnecessary memory use. This
|
||
manifests itself in the way people can plug into the compiler (i.e. the
|
||
<a href="./rustc-driver/intro.html">driver</a>), preferring a "push"-style API (callbacks) instead
|
||
of the more Rust-ic "pull" style (think the <code>Iterator</code> trait).</p>
|
||
<p>Thread-local storage and interning are used a lot through the compiler to reduce
|
||
duplication while also preventing a lot of the ergonomic issues due to many
|
||
pervasive lifetimes. The <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/tls/index.html"><code>rustc_middle::ty::tls</code></a> module is used to access these
|
||
thread-locals, although you should rarely need to touch it.</p>
|
||
|
||
</main>
|
||
|
||
<nav class="nav-wrapper" aria-label="Page navigation">
|
||
<!-- Mobile navigation buttons -->
|
||
<a rel="prev" href="queries/salsa.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||
<i class="fa fa-angle-left"></i>
|
||
</a>
|
||
|
||
<a rel="next prefetch" href="serialization.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||
<i class="fa fa-angle-right"></i>
|
||
</a>
|
||
|
||
<div style="clear: both"></div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
||
<a rel="prev" href="queries/salsa.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||
<i class="fa fa-angle-left"></i>
|
||
</a>
|
||
|
||
<a rel="next prefetch" href="serialization.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||
<i class="fa fa-angle-right"></i>
|
||
</a>
|
||
</nav>
|
||
|
||
</div>
|
||
|
||
|
||
|
||
|
||
<script>
|
||
window.playground_copyable = true;
|
||
</script>
|
||
|
||
|
||
<script src="elasticlunr.min.js"></script>
|
||
<script src="mark.min.js"></script>
|
||
<script src="searcher.js"></script>
|
||
|
||
<script src="clipboard.min.js"></script>
|
||
<script src="highlight.js"></script>
|
||
<script src="book.js"></script>
|
||
|
||
<!-- Custom JS scripts -->
|
||
<script src="mermaid.min.js"></script>
|
||
<script src="mermaid-init.js"></script>
|
||
|
||
|
||
</div>
|
||
</body>
|
||
</html>
|