Rust in Action Notes Chapter 6 Memory

  1. Option<T>Types use null pointer optimization in Rust to ensure that the type occupies 0 bytes in the compiled binary. The variable is represented Noneby a null pointer ;null pointer
  2. The difference between memory address, pointer, and reference. A memory address refers to a byte in memory, an abstraction provided by assembly language; a pointer, sometimes called a raw pointer (raw pointer), is a memory address pointing to a certain data type, and a pointer is an abstraction provided by a high-level language; a reference is a pointer. In a dynamic type, a reference contains a pointer and some additional guarantees. The reference is an abstraction provided by Rust;
  3. Rust's references provide more benefits than pointers: references always point to valid data; the byte arrangement of references is compact, which helps the CPU to read quickly; references can provide length guarantees for variable-length data types. In addition to the internal pointer itself, the referenced structure also provides a data length variable to ensure that the program will never run outside the memory range;
  4. {:p}You can print the variable installation pointer and print its memory address;
  5. Code Listing 6.3 shows two forms of converting from u8 array to string. The b variable is first String::from_raw_parts(ptr, size, capa)converted [u8; 10]to *const u8and then converted to *mut u8; the c variable introduces the CStr external interface by CStr::from_ptr(c_ptr)obtaining a \0string of C language type ending with ; both b and c variables need to be [u8, n]converted *const u8to the corresponding pointer type required by the conversion first;
  6. Rust's raw pointers are divided *const Tinto *mut Ttwo types: immutable raw pointers and mutable raw pointers, which can be freely converted. &mut T and &TThe result compiled by Rust's references becomes a raw pointer, that is, the performance of a naked pointer can be obtained without unsafe during actual operation;
  7. Reasons for using naked pointers: Unavoidable use, when the system needs to call OS functions or some third-party codes need naked pointers, usually external calls to some programs written in C; when multiple places need to access data at the same time and the runtime performance requirements are extremely high;
  8. The smart pointer in C++ corresponds to Rust core::ptr::{Unique, Shared, Weak}. The fat pointer (fat pointer) usually refers to the memory layout, which is larger than the thin pointer (thin pointer, usually also refers to the naked pointer, which has only one usize width), usually with 2 usize widths or even more;
  9. core::ptr::UniqueIt is a constituent element in rust String, Box<T>; core::ptr::Sharedit is Rc<T>, Arc<T>a constituent element, if you want to implement your own smart pointer, you can refer to the implementation details of these smart pointers;
  10. std::rc::Weak, std::arc::WeakIt can be used for data structures that point to each other internally, avoiding the problem of circular pointers; alloc::raw_vec::RawVecfor implementation Vec<T>, VecDeq<T>, it can intelligently allocate and reclaim memory for any type of data; std::cell::UnsafeCellfor implementation Cell<T>, RefCell<T>, to provide internal mutability (interior mutability);
  11. "When in doubt, prefer the stack" means that when you don't know whether to put the data on the heap or the stack, put it on the stack first, because the stack is faster. The Rust version of this sentence is, that is, the type that implements the Sized feature will be stored on the stack first When in doulbe, use types that implement Sized;
  12. How to make a function accept &str, Stringtwo types of parameters at the same time, you can use the parameter template <T: AsRef<str>>to indicate that the parameter T can be called a str reference, so as to call .as_ref().len()the method, see Page189 for details;
  13. Table 6.1 gives a simple comparison between the stack and the heap. Note that the table shows that in Rust without Unsafe, it is safe to use the heap;
    Table 6.1
  14. Several general methods for optimizing heap memory allocation: allocate enough space in advance, initialize it to 0, and then change it to a non-zero value when it is used. This method is dangerous and may trigger Rust’s lifecycle check; design an allocator for the program by yourself, which can allocate the required space more effectively; research and use, allowing objects to be used while they are created (created on the fly), arena is a third-party library arena::{Arena, TypedArena}, link ;
  15. Common terms for virtual memory, Page, Word, Page fault, Swapping, Virtual memory, Real memory, Page table, Segment, Segmentation fault, MMU, TLB (translation lookaside buffer), see Page203 for details;
  16. Section 6.4.2 talks about the segmentation fault (segmentation fault), which will occur when accessing an illegal area. You can refer to the following code for implementation;
  17. Section 6.4.3 talks about the role of MMU (memory damage unit), that is, the translation of virtual addresses into physical addresses, and the role of using TLB cache to accelerate. Page 206 talks about some small tricks used by the operating system and CPU in the process of translating addresses;
  18. The figure below shows how an executable file (ELF) is loaded into virtual memory and runinsert image description here

Guess you like

Origin blog.csdn.net/Mint2yx4/article/details/131188752