Keep sketching outline for stack allocation

This commit is contained in:
Bradlee Speice 2019-01-23 22:55:00 -05:00
parent 03f72cff8e
commit a134b5278b

View File

@ -493,41 +493,41 @@ With all that in mind, let's get into the details. The unfortunate thing about s
in Rust is that there's not a good in Rust is that there's not a good
way to glance at code and figure out where allocations on the heap happen. Looking at way to glance at code and figure out where allocations on the heap happen. Looking at
other languages, Java mostly cares about `new MyObject()` (yes, I'm conveniently ignoring other languages, Java mostly cares about `new MyObject()` (yes, I'm conveniently ignoring
autoboxing). C makes things clear with calls to [malloc(3)](https://linux.die.net/man/3/malloc). autoboxing). C makes things clear with calls to [malloc(3)](https://linux.die.net/man/3/malloc),
C++ has the [new](https://stackoverflow.com/a/655086/1454178) keyword, and old C++ has the [new](https://stackoverflow.com/a/655086/1454178) keyword.
[`std::make_unique()`](https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique), and Rust's model most closely aligns with C++11 and [RAII](https://en.cppreference.com/w/cpp/language/raii);
[`std::make_shared()`](https://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared) [`Box`](https://doc.rust-lang.org/stable/alloc/boxed/struct.Box.html)
(though things are admittedly more complex with [RAII](https://en.cppreference.com/w/cpp/language/raii)). is comparable to [`std::make_unique()`](https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique),
All languages exist on a memory management spectrum, from [Zig](https://ziglang.org/) and [`Rc`](https://doc.rust-lang.org/stable/alloc/rc/struct.Rc.html) behaves like
forcing you to provide an [allocator](https://ziglang.org/documentation/master/#Memory), [`std::make_shared()`](https://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared).
to Python/Ruby/JavaScript assuming you generally never worry about those details.
So what can be done to make sure your program is using stack allocations? A couple of But what can be done to ensure your program is using stack allocations? Some guidelines
guidelines are in order: are in order:
**For code you control**: **For code you control**:
- Never using types in the [`alloc` crate](https://doc.rust-lang.org/stable/alloc/index.html) - Don't use smart pointer types, as they force heap allocation -
is sufficient. While you should always review its contents, the most notable members are
[`Box`](https://doc.rust-lang.org/stable/alloc/boxed/struct.Box.html), [`Box`](https://doc.rust-lang.org/stable/alloc/boxed/struct.Box.html),
refcount types ([`Rc`](https://doc.rust-lang.org/stable/alloc/rc/struct.Rc.html), [`Rc`](https://doc.rust-lang.org/stable/alloc/rc/struct.Rc.html), etc.
[`Arc`](https://doc.rust-lang.org/stable/alloc/sync/struct.Arc.html)) - Cloning or copying stack-allocated objects creates new objects that are
- Dynamically resizable types need to be treated with care; we'll go into detail later, stack-allocated.
but pay attention to [`String`](https://doc.rust-lang.org/stable/alloc/string/struct.String.html),
[`Vec`](https://doc.rust-lang.org/stable/alloc/vec/struct.Vec.html), and
[`HashMap`](https://doc.rust-lang.org/stable/std/collections/struct.HashMap.html)
- Enums and other wrapper types will not trigger heap allocations unless - Enums and other wrapper types will not trigger heap allocations unless
the underlying type also needs heap allocation. You can use their contents need heap allocation. You can use
[`Option`](https://doc.rust-lang.org/stable/core/option/enum.Option.html), [`Option`](https://doc.rust-lang.org/stable/core/option/enum.Option.html) and
[`Result`](https://doc.rust-lang.org/stable/core/result/enum.Result.html), and
[`RefCell`](https://doc.rust-lang.org/stable/core/cell/struct.RefCell.html) [`RefCell`](https://doc.rust-lang.org/stable/core/cell/struct.RefCell.html)
with reckless abandon. with reckless abandon.
- [Arrays](https://doc.rust-lang.org/std/primitive.array.html) are guaranteed - [Arrays](https://doc.rust-lang.org/std/primitive.array.html) are guaranteed
to be stack-allocated in all circumstances. to be stack-allocated, but dynamically resizable types (
[`String`](https://doc.rust-lang.org/stable/alloc/string/struct.String.html),
[`Vec`](https://doc.rust-lang.org/stable/alloc/vec/struct.Vec.html),
[`HashMap`](https://doc.rust-lang.org/stable/std/collections/struct.HashMap.html))
will store their contents in the heap
- Note to self: Do I need to mention generics or trait objects? I think this - Note to self: Do I need to mention generics or trait objects? I think this
may be handled by the other points, and can be addressed later. may be handled by the other points, and can be addressed later. Also, is it
obvious that cloning stack-allocated data puts things on the stack? Is there
a way to address that without it being a unique point?
**For code outside your control**: **For code outside your control**: (crates you rely on)
- Review the code to make sure it abides by the guidelines above - Review the code to make sure it abides by the guidelines above
- Use a custom allocator like [qadapt](https://crates.io/crates/qadapt) as an automated check - Use a custom allocator like [qadapt](https://crates.io/crates/qadapt) as an automated check