speice.io/assets/js/88eed8c4.0354277e.js

1 line
20 KiB
JavaScript
Raw Normal View History

"use strict";(self.webpackChunkspeice_io=self.webpackChunkspeice_io||[]).push([["1652"],{44257:function(e,n,t){t.r(n),t.d(n,{assets:function(){return c},contentTitle:function(){return a},default:function(){return h},frontMatter:function(){return r},metadata:function(){return s},toc:function(){return o}});var s=t(1495),i=t(85893),l=t(50065);let r={slug:"2019/02/the-whole-world",title:"Allocations in Rust: Global memory",date:new Date("2019-02-05T12:00:00.000Z"),authors:["bspeice"],tags:[]},a=void 0,c={authorsImageUrls:[void 0]},o=[{value:"<code>const</code> values",id:"const-values",level:2},{value:"Read-Only",id:"read-only",level:3},{value:"Initialization",id:"initialization",level:3},{value:"Copying",id:"copying",level:3},{value:"<code>static</code> values",id:"static-values",level:2},{value:"Memory Uniqueness",id:"memory-uniqueness",level:3},{value:"Initialization",id:"initialization-1",level:3},{value:"The <code>Sync</code> marker",id:"the-sync-marker",level:3},{value:"Interior mutability",id:"interior-mutability",level:3}];function d(e){let n={a:"a",code:"code",em:"em",h2:"h2",h3:"h3",li:"li",p:"p",pre:"pre",ul:"ul",...(0,l.a)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsxs)(n.p,{children:["The first memory type we'll look at is pretty special: when Rust can prove that a ",(0,i.jsx)(n.em,{children:"value"})," is fixed\nfor the life of a program (",(0,i.jsx)(n.code,{children:"const"}),"), and when a ",(0,i.jsx)(n.em,{children:"reference"})," is unique for the life of a program\n(",(0,i.jsx)(n.code,{children:"static"})," as a declaration, not\n",(0,i.jsx)(n.a,{href:"https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#the-static-lifetime",children:(0,i.jsx)(n.code,{children:"'static"})})," as a\nlifetime), we can make use of global memory. This special section of data is embedded directly in\nthe program binary so that variables are ready to go once the program loads; no additional\ncomputation is necessary."]}),"\n",(0,i.jsxs)(n.p,{children:["Understanding the value/reference distinction is important for reasons we'll go into below, and\nwhile the\n",(0,i.jsx)(n.a,{href:"https://github.com/rust-lang/rfcs/blob/master/text/0246-const-vs-static.md",children:"full specification"})," for\nthese two keywords is available, we'll take a hands-on approach to the topic."]}),"\n",(0,i.jsxs)(n.h2,{id:"const-values",children:[(0,i.jsx)(n.code,{children:"const"})," values"]}),"\n",(0,i.jsxs)(n.p,{children:["When a ",(0,i.jsx)(n.em,{children:"value"}),' is guaranteed to be unchanging in your program (where "value" may be scalars,\n',(0,i.jsx)(n.code,{children:"struct"}),"s, etc.), you can declare it ",(0,i.jsx)(n.code,{children:"const"}),". This tells the compiler that it's safe to treat the\nvalue as never changing, and enables some interesting optimizations; not only is there no\ninitialization cost to creating the value (it is loaded at the same time as the executable parts of\nyour program), but the compiler can also copy the value around if it speeds up the code."]}),"\n",(0,i.jsxs)(n.p,{children:["The points we need to address when talking about ",(0,i.jsx)(n.code,{children:"const"})," are:"]}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.code,{children:"Const"})," values are stored in read-only memory - it's impossible to modify."]}),"\n",(0,i.jsxs)(n.li,{children:["Values resulting from calling a ",(0,i.jsx)(n.code,{children:"const fn"})," are materialized at compile-time."]}),"\n",(0,i.jsxs)(n.li,{children:["The compiler may (or may not) copy ",(0,i.jsx)(n.code,{children:"const"})," values wherever it chooses."]}),"\n"]}),"\n",(0,i.jsx)(n.h3,{id:"read-only",children:"Read-Only"}),"\n",(0,i.jsxs)(n.p,{children:['The first point is a bit strange - "read-only memory."\n',(0,i.jsx)(n.a,{href:"https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#differences-between-variables-and-constants",children:"The Rust book"}),"\nmentions in a couple places that using ",(0,i.jsx)(n.code,{children:"mut"})," with constants is illegal, but it's also important to\ndemon