speice.io/_drafts/understanding-allocations-in-rust.md

135 lines
6.2 KiB
Markdown
Raw Normal View History

2018-12-19 22:50:57 -05:00
---
layout: post
2018-12-26 10:19:34 -05:00
title: "Understanding Heap Allocations in Rust"
2018-12-19 22:50:57 -05:00
description: "An introduction to the Rust memory model"
category:
tags: [rust]
---
There's an alchemy of distilling complex technical topics into articles and videos
that change the way programmers see the tools they interact with on a regular basis.
2019-01-04 00:08:36 -05:00
I knew what a linker was, but there's a staggering amount of complexity in between
[`main()` and your executable](https://www.youtube.com/watch?v=dOfucXtyEsU).
2018-12-26 10:19:34 -05:00
Rust programmers use the [`Box`](https://doc.rust-lang.org/stable/std/boxed/struct.Box.html)
type all the time, but there's a rich history of the Rust language itself wrapped up in
2018-12-19 22:50:57 -05:00
[how special it is](https://manishearth.github.io/blog/2017/01/10/rust-tidbits-box-is-special/).
2019-01-04 00:08:36 -05:00
In a similar vein, I want you to look at code and understand how memory is used;
the complex choreography of operating system, compiler, and program that frees you
2019-01-01 14:31:15 -05:00
to focus on functionality far-flung from frivolous book-keeping. The Rust compiler relieves
2019-01-04 00:08:36 -05:00
a great deal of the cognitive burden associated with memory management, but we're going
to step into its world for a while.
2018-12-19 22:50:57 -05:00
2019-01-01 14:31:15 -05:00
Let's learn a bit about memory in Rust.
2018-12-19 22:50:57 -05:00
# Table of Contents
This post is intended as both guide and reference material; we'll work to establish
2018-12-26 10:19:34 -05:00
an understanding of the different memory types Rust makes use of, then summarize each
2018-12-19 22:50:57 -05:00
section for easy citation in the future. To that end, a table of contents is provided
to assist in easy navigation:
2018-12-26 10:19:34 -05:00
- [Foreword](#foreword)
2019-01-04 00:08:36 -05:00
- [Stacking Up: Non-Heap Memory Types](#non-heap-memory-types)
- [Piling On: Rust and the Heap](#piling-on-rust-and-the-heap)
2019-01-01 14:31:15 -05:00
- [Compiler Optimizations Make Everything Complicated](#compiler-optimizations-make-everything-complicated)
2018-12-26 10:19:34 -05:00
- Summary: When Does Rust Allocate?
2019-01-01 14:31:15 -05:00
- [Appendix and Further Reading](#appendix-and-further-reading)
2018-12-26 10:19:34 -05:00
# Foreword
2019-01-04 00:08:36 -05:00
There's a simple checklist to see if you can skip over reading this article. You must:
2018-12-26 10:19:34 -05:00
1. Only write `#![no_std]` crates
2. Never use `unsafe`
3. Never use `#![feature(alloc)]`
For some uses of Rust, typically embedded devices, these constraints make sense.
They're working with very limited memory, and the program binary size itself may
2019-01-04 00:08:36 -05:00
significantly affect what's available! There's no operating system able to manage
this "virtual memory" junk, but that's not an issue because there's only one
running application. The [embedonomicon] is ever in mind, and interacting with the
"real world" through extra peripherals is accomplished by reading and writing to
exact memory addresses.
2018-12-26 10:19:34 -05:00
Most Rust programs find these requirements overly burdensome though. C++ developers
2019-01-04 00:08:36 -05:00
would struggle without access to [`std::vector`](https://en.cppreference.com/w/cpp/container/vector)
(except those hardcore no-STL guys), and Rust developers would struggle without
[`std::vec`](https://doc.rust-lang.org/std/vec/struct.Vec.html). But in this scenario,
`std::vec` is actually part of the [`alloc` crate](https://doc.rust-lang.org/alloc/vec/struct.Vec.html),
and thus off-limits (because the `alloc` crate requires `#![feature(alloc)]`).
Or how would you use trait objects? There's no
2018-12-26 10:19:34 -05:00
[`Box<dyn Trait>`](https://doc.rust-lang.org/alloc/boxed/struct.Box.html)
available to use for dynamic dispatch.
2019-01-04 00:08:36 -05:00
Whether writing code for embedded devices or not, the important thing in both situations
is how much you know *before your application starts* about what your memory usage looks like.
In the embedded device example, there's a small, fixed amount of memory you can possibly use.
In a browser, however, you have no idea how large [google.com's home page] is until you start
trying to download it. The compiler uses this information (or lack thereof) to optimize
how memory is used; put simply, your code runs faster when the compiler can guarantee exactly
how much memory your program needs while it's running. This post is all about understanding
the optimization tricks the compiler uses, and how you can help the compiler and make
your programs more efficient.
Now let's address some conditions and caveats before going much further.
This article will focus on "safe" Rust only; `unsafe` mode allows you
to make use of platform-specific allocation API's (think the [libc] and [winapi]
implementations of [malloc]) that we'll ignore. We'll also assume a "debug"
build of libraries and applications (what you get with `cargo run` and `cargo test`)
and address (hehe) "release" mode at the end (`cargo run --release` and `cargo test --release`).
Finally, while the details are unlikely to change, the Rust docs
2018-12-26 10:19:34 -05:00
include a warning worth repeating here:
> Rust does not currently have a rigorously and formally defined memory model.
> - the [Rust docs](https://doc.rust-lang.org/std/ptr/fn.read_volatile.html)
2018-12-19 22:50:57 -05:00
2019-01-04 00:08:36 -05:00
# Stacking Up: Non-Heap Memory Types
Languages like Java and Python do an amazing job of simplifying the memory model
needed for programmers. You can essentially treat
Most of the reason this post was written is because I
Everyone's agreed that [compilers](https://www.youtube.com/watch?v=bSkpMdDe4g4) are
[smart](https://www.youtube.com/watch?v=nAbCKa0FzjQ), and Rust is no exception.
2018-12-19 23:21:54 -05:00
Example: Why doesn't `Vec::new()` go to the allocator?
Questions:
1. What is the "Push" instruction? Why do we like the stack?
2. How does Rust allocate arguments to the function?
3. How does Rust allocate variables created in the function but never returned?
4. How does Rust allocate variables created in the function and returned?
5. How do Option<> or Result<> affect structs?
6. How are arrays allocated?
7. Legal to pass an array as an argument?
2019-01-01 14:31:15 -05:00
# Piling On - Rust and the Heap
2018-12-19 23:21:54 -05:00
Example: How to trigger a heap allocation
Questions:
1. Where do collection types allocate memory?
2. Does a Box<> always allocate heap?
2019-01-01 14:31:15 -05:00
- Yes, with exception of compiler optimizations
2018-12-19 23:21:54 -05:00
3. Passing Box<Trait> vs. genericizing/monomorphization
2019-01-01 14:31:15 -05:00
- If it uses `dyn Trait`, it's on the heap.
2018-12-26 10:19:34 -05:00
4. Other pointer types? Do Rc<>/Arc<> force heap allocation?
2019-01-01 14:31:15 -05:00
- Maybe? Part of the alloc crate, but should use qadapt to check
2018-12-19 23:21:54 -05:00
2019-01-01 14:31:15 -05:00
# Compiler Optimizations Make Everything Complicated
2018-12-19 23:21:54 -05:00
2018-12-26 10:19:34 -05:00
Example: Compiler stripping out allocations of Box<>, Vec::push()
2019-01-01 14:31:15 -05:00
# Appendix and Further Reading
[Embedonomicon]:
2018-12-26 10:19:34 -05:00
[embedonomicon]: https://docs.rust-embedded.org/embedonomicon/
[libc]: CRATES.IO LINK
[winapi]: CRATES.IO LINK
[malloc]: MANPAGE LINK