- Hands-On Data Structures and Algorithms with Rust
- Claus Matzinger
- 389字
- 2021-07-02 14:11:49
Accessing the box
An extra step doesn't sound like much, but it has considerable consequences. This trade-off for easily sharing ownership across various functions or threads removes the ability to put as much data as possible into the CPU's cache, since the pointer makes any data locality difficult. Heap allocations themselves are expensive operations and reducing those will already provide a major speedup.
Furthermore, the compiler cannot deallocate a boxed value if it's still referenced in some places—a problem that occurs especially if the program is large and complex. Similar to orphaned objects in C# or Java, a saved Rc reference can easily be forgotten, creating a memory leak. Therefore, it's recommended to use heap memory only when required.
One principle piece of advice that requires a boxed value in Rust is to "favor object composition over class inheritance" (Gang of Four 1995:20). In the absence of class inheritance, the choice is obviously to use object composition. Considering that you should also "program to an interface not to an implementation" (ibid), there is often a strong wish to put a reference to a trait inside of a struct instead of directly working with the implementation.
To apply this architecture in Rust, the language requires us to put a trait's implementation into a Box<dyn TheTrait>, making it more difficult to handle, test, and reason about. This trait object requires the compiler to rely on dynamic dispatch, which is considerably slower than the default static dispatch.
Other than generics, there is no default solution to this issue. The impl Trait addition of Rust 2018 alleviates this issue for function parameters and return values, but cannot be used for field types.
So far, it looks like the best choice is to use concrete types instead of traits to avoid multiple dereference operations—as long as refactoring on change seems to be doable. If you create a library, generics are a better way to go for performance and flexibility.
- ETL數(shù)據(jù)整合與處理(Kettle)
- 有趣的二進(jìn)制:軟件安全與逆向分析
- Google Visualization API Essentials
- 信息系統(tǒng)與數(shù)據(jù)科學(xué)
- 數(shù)據(jù)庫系統(tǒng)原理及應(yīng)用教程(第4版)
- UDK iOS Game Development Beginner's Guide
- WS-BPEL 2.0 Beginner's Guide
- 達(dá)夢數(shù)據(jù)庫性能優(yōu)化
- 數(shù)據(jù)挖掘原理與SPSS Clementine應(yīng)用寶典
- 數(shù)據(jù)科學(xué)實(shí)戰(zhàn)指南
- Doris實(shí)時數(shù)倉實(shí)戰(zhàn)
- 數(shù)據(jù)挖掘競賽實(shí)戰(zhàn):方法與案例
- Unity for Architectural Visualization
- 高效使用Redis:一書學(xué)透數(shù)據(jù)存儲與高可用集群
- Hadoop大數(shù)據(jù)技術(shù)開發(fā)實(shí)戰(zhàn)