mirror of
https://github.com/bspeice/speice.io
synced 2024-11-14 22:18:10 -05:00
1 line
31 KiB
JavaScript
1 line
31 KiB
JavaScript
|
"use strict";(self.webpackChunkspeice_io=self.webpackChunkspeice_io||[]).push([["1874"],{2763:function(e,n,t){t.r(n),t.d(n,{assets:function(){return l},contentTitle:function(){return i},default:function(){return d},frontMatter:function(){return o},metadata:function(){return s},toc:function(){return c}});var s=t(2340),r=t(5893),a=t(65);let o={slug:"2019/02/stacking-up",title:"Allocations in Rust: Fixed memory",date:new Date("2019-02-06T12:00:00.000Z"),authors:["bspeice"],tags:[]},i=void 0,l={authorsImageUrls:[void 0]},c=[{value:"Structs",id:"structs",level:2},{value:"Function arguments",id:"function-arguments",level:2},{value:"Enums",id:"enums",level:2},{value:"Arrays",id:"arrays",level:2},{value:"Closures",id:"closures",level:2},{value:"Generics",id:"generics",level:2},{value:"Copy types",id:"copy-types",level:2},{value:"Iterators",id:"iterators",level:2}];function h(e){let n={a:"a",code:"code",em:"em",h2:"h2",li:"li",ol:"ol",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,a.a)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"const"})," and ",(0,r.jsx)(n.code,{children:"static"})," are perfectly fine, but it's relatively rare that we know at compile-time about\neither values or references that will be the same for the duration of our program. Put another way,\nit's not often the case that either you or your compiler knows how much memory your entire program\nwill ever need."]}),"\n",(0,r.jsx)(n.p,{children:'However, there are still some optimizations the compiler can do if it knows how much memory\nindividual functions will need. Specifically, the compiler can make use of "stack" memory (as\nopposed to "heap" memory) which can be managed far faster in both the short- and long-term.'}),"\n",(0,r.jsxs)(n.p,{children:["When requesting memory, the ",(0,r.jsxs)(n.a,{href:"http://www.cs.virginia.edu/~evans/cs216/guides/x86.html",children:[(0,r.jsx)(n.code,{children:"push"})," instruction"]}),"\ncan typically complete in ",(0,r.jsx)(n.a,{href:"https://agner.org/optimize/instruction_tables.ods",children:"1 or 2 cycles"})," (<1ns\non modern CPUs). Contrast that to heap memory which requires an allocator (specialized\nsoftware to track what memory is in use) to reserve space. When you're finished with stack memory,\nthe ",(0,r.jsx)(n.code,{children:"pop"})," instruction runs in 1-3 cycles, as opposed to an allocator needing to worry about memory\nfragmentation and other issues with the heap. All sorts of incredibly sophisticated techniques have\nbeen used to design allocators:"]}),"\n",(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsxs)(n.li,{children:[(0,r.jsx)(n.a,{href:"https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)",children:"Garbage Collection"}),"\nstrategies like ",(0,r.jsx)(n.a,{href:"https://en.wikipedia.org/wiki/Tracing_garbage_collection",children:"Tracing"})," (used in\n",(0,r.jsx)(n.a,{href:"https://www.oracle.com/technetwork/java/javase/tech/g1-intro-jsp-135488.html",children:"Java"}),") and\n",(0,r.jsx)(n.a,{href:"https://en.wikipedia.org/wiki/Reference_counting",children:"Reference counting"})," (used in\n",(0,r.jsx)(n.a,{href:"https://docs.python.org/3/extending/extending.html#reference-counts",children:"Python"}),")"]}),"\n",(0,r.jsxs)(n.li,{children:["Thread-local structures to prevent locking the allocator in\n",(0,r.jsx)(n.a,{href:"https://jamesgolick.com/2013/5/19/how-tcmalloc-works.html",children:"tcmalloc"})]}),"\n",(0,r.jsxs)(n.li,{children:["Arena structures used in ",(0,r.jsx)(n.a,{href:"http://jemalloc.net/",children:"jemalloc"}),", which\n",(0,r.jsx)(n.a,{href:"https://blog.rust-lang.org/2019/01/17/Rust-1.32.0.html#jemalloc-is-removed-by-default",children:"until recently"}),"\nwas the primary allocator for Rust programs!"]}),"\n"]}),"\n",(0,r.jsxs)(n.p,{children:["But no matter how fast your allocator is, the principle remains: the fastest allocator is the one\nyou never use. As such, we're not going to discuss how exactly the\n",(0,r.jsxs)(n.a,{href:"http://www.cs.virginia.edu/~evans/cs216/guides/x86.html",children:[(0,r.jsx)(n.
|