mirror of
https://github.com/bspeice/speice.io
synced 2024-12-23 00:58:09 -05:00
26 lines
18 KiB
HTML
26 lines
18 KiB
HTML
|
<!doctype html><html lang=en dir=ltr class="blog-wrapper blog-post-page plugin-blog plugin-id-default" data-has-hydrated=false><meta charset=UTF-8><meta name=generator content="Docusaurus v3.6.0"><title data-rh=true>Allocations in Rust: Summary | The Old Speice Guy</title><meta data-rh=true name=viewport content="width=device-width,initial-scale=1.0"><meta data-rh=true name=twitter:card content=summary_large_image><meta data-rh=true property=og:url content=https://speice.io/2019/02/summary><meta data-rh=true property=og:locale content=en><meta data-rh=true name=docusaurus_locale content=en><meta data-rh=true name=docusaurus_tag content=default><meta data-rh=true name=docsearch:language content=en><meta data-rh=true name=docsearch:docusaurus_tag content=default><meta data-rh=true property=og:title content="Allocations in Rust: Summary | The Old Speice Guy"><meta data-rh=true name=description content="While there's a lot of interesting detail captured in this series, it's often helpful to have a"><meta data-rh=true property=og:description content="While there's a lot of interesting detail captured in this series, it's often helpful to have a"><meta data-rh=true property=og:type content=article><meta data-rh=true property=article:published_time content=2019-02-09T12:00:00.000Z><link data-rh=true rel=icon href=/img/favicon.ico><link data-rh=true rel=canonical href=https://speice.io/2019/02/summary><link data-rh=true rel=alternate href=https://speice.io/2019/02/summary hreflang=en><link data-rh=true rel=alternate href=https://speice.io/2019/02/summary hreflang=x-default><script data-rh=true type=application/ld+json>{"@context":"https://schema.org","@id":"https://speice.io/2019/02/summary","@type":"BlogPosting","author":{"@type":"Person","name":"Bradlee Speice"},"dateModified":"2024-11-10T02:05:00.000Z","datePublished":"2019-02-09T12:00:00.000Z","description":"While there's a lot of interesting detail captured in this series, it's often helpful to have a","headline":"Allocations in Rust: Summary","isPartOf":{"@id":"https://speice.io/","@type":"Blog","name":"Blog"},"keywords":[],"mainEntityOfPage":"https://speice.io/2019/02/summary","name":"Allocations in Rust: Summary","url":"https://speice.io/2019/02/summary"}</script><link rel=alternate type=application/rss+xml href=/rss.xml title="The Old Speice Guy RSS Feed"><link rel=alternate type=application/atom+xml href=/atom.xml title="The Old Speice Guy Atom Feed"><link rel=stylesheet href=https://cdn.jsdelivr.net/npm/katex@0.13.24/dist/katex.min.css integrity=sha384-odtC+0UGzzFL/6PNoE8rX/SPcQDXBJ+uRepguP4QkPCm2LBxH3FA3y+fKSiJ+AmM crossorigin><link rel=stylesheet href=/assets/css/styles.ae6ff4a3.css><script src=/assets/js/runtime~main.751b419d.js defer></script><script src=/assets/js/main.62ce6156.js defer></script><body class=navigation-with-keyboard><script>!function(){var t,e=function(){try{return new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}}()||function(){try{return window.localStorage.getItem("theme")}catch(t){}}();t=null!==e?e:"light",document.documentElement.setAttribute("data-theme",t)}(),function(){try{for(var[t,e]of new URLSearchParams(window.location.search).entries())if(t.startsWith("docusaurus-data-")){var a=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(a,e)}}catch(t){}}()</script><div id=__docusaurus><div role=region aria-label="Skip to main content"><a class=skipToContent_fXgn href=#__docusaurus_skipToContent_fallback>Skip to main content</a></div><nav aria-label=Main class="navbar navbar--fixed-top"><div class=navbar__inner><div class=navbar__items><button aria-label="Toggle navigation bar" aria-expanded=false class="navbar__toggle clean-btn" type=button><svg width=30 height=30 viewBox="0 0 30 30" aria-hidden=true><path stroke=currentColor stroke-linecap=round stroke-miterlimit=10 stroke-width=2 d="M4 7h22M4 15h22M4 23h22"/></svg></button><a class=navbar__brand href=/><div class=navbar__logo><img src=/img/logo.svg alt="Sierpinski Gasket" class="themedComponent_mlkZ themedComponent--light_NVdE"><img src=
|
||
|
document that answers some "yes/no" questions. You may not care about what an <code>Iterator</code> looks like
|
||
|
in assembly, you just need to know whether it allocates an object on the heap or not. And while Rust
|
||
|
will prioritize the fastest behavior it can, here are the rules for each memory type:</p>
|
||
|
<p><strong>Global Allocation</strong>:</p>
|
||
|
<ul>
|
||
|
<li><code>const</code> is a fixed value; the compiler is allowed to copy it wherever useful.</li>
|
||
|
<li><code>static</code> is a fixed reference; the compiler will guarantee it is unique.</li>
|
||
|
</ul>
|
||
|
<p><strong>Stack Allocation</strong>:</p>
|
||
|
<ul>
|
||
|
<li>Everything not using a smart pointer will be allocated on the stack.</li>
|
||
|
<li>Structs, enums, iterators, arrays, and closures are all stack allocated.</li>
|
||
|
<li>Cell types (<code>RefCell</code>) behave like smart pointers, but are stack-allocated.</li>
|
||
|
<li>Inlining (<code>#[inline]</code>) will not affect allocation behavior for better or worse.</li>
|
||
|
<li>Types that are marked <code>Copy</code> are guaranteed to have their contents stack-allocated.</li>
|
||
|
</ul>
|
||
|
<p><strong>Heap Allocation</strong>:</p>
|
||
|
<ul>
|
||
|
<li>Smart pointers (<code>Box</code>, <code>Rc</code>, <code>Mutex</code>, etc.) allocate their contents in heap memory.</li>
|
||
|
<li>Collections (<code>HashMap</code>, <code>Vec</code>, <code>String</code>, etc.) allocate their contents in heap memory.</li>
|
||
|
<li>Some smart pointers in the standard library have counterparts in other crates that don't need heap
|
||
|
memory. If possible, use those.</li>
|
||
|
</ul>
|
||
|
<p><img decoding=async loading=lazy alt="Container Sizes in Rust" src=/assets/images/container-size-7fd54cbb2391e3e7310b0424c5f92cc1.svg width=960 height=540 class=img_ev3q></p>
|
||
|
<p>-- <a href="https://docs.google.com/presentation/d/1q-c7UAyrUlM-eZyTo1pd8SZ0qwA_wYxmPZVOQkoDmH4/edit?usp=sharing" target=_blank rel="noopener noreferrer">Raph Levien</a></div></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Blog post page navigation"><a class="pagination-nav__link pagination-nav__link--prev" href=/2019/02/08/compiler-optimizations><div class=pagination-nav__sublabel>Older post</div><div class=pagination-nav__label>Allocations in Rust: Compiler optimizations</div></a><a class="pagination-nav__link pagination-nav__link--next" href=/2019/05/making-bread><div class=pagination-nav__sublabel>Newer post</div><div class=pagination-nav__label>Making bread</div></a></nav></main></div></div></div><footer class=footer><div class="container container-fluid"><div class="footer__bottom text--center"><div class=footer__copyright>Copyright © 2024 Bradlee Speice</div></div></div></footer></div>
|