- Hands-On Data Structures and Algorithms with Rust
- Claus Matzinger
- 505字
- 2021-07-02 14:11:50
Concurrency and performance
Having code that is easier to reason about and where the state cannot be changed is even more important in multithreaded scenarios. This prevents so-called anomalies (or side effects) where the state of an object is changed outside a dependent thread.
Locks are generally made to change the state of a shared object—they secure critical sections, which only a single thread can modify at any given time. Other threads have to "line up" and wait for the lock to be released to access the part as well. In Rust, this is called a mutex.
Locks and mutex zones are bad for the following reasons:
- They have to be in the right order (acquired and released).
- What happens when a thread panics in a mutex zone?
- They are hard to integrate seamlessly into the part of the program that they protect.
- They are a bottleneck for performance.
Immutability is a simple way to avoid all of these, and there are many immutable data structure crates available, including one with persistent data structures called Rust Persistent Data Structures (RPDS) (https://crates.io/crates/rpds), that utilize a copy-on-write approach with versioning to capture state changes. Since these changes build on top of each other, threads can fully read one consistent object state at a time without having to wait or acquire a lock.
Persistent data structures are a take on creating data structures that are as efficient and mutable as their traditional counterparts, but better suited for concurrency. This is achieved by keeping the original data immutable and storing versioned change sets.
The idea of immutable data is best thought of in the context of functional programming. Functional programming is built on the principle of mathematical functions. A function is a relation of two sets of data (typically X and Y), where each element of X has exactly one element in Y that it maps to using the f function ( in short: where
).
As a consequence, the input data, X, will not be changed to produce output data, Y, making it easy to run the f function in parallel. The downside is the increased cost at runtime: regardless of the operation, whether it's only to flip a bit on the input data or to overhaul everything, the result is always a full copy.
To reduce this inefficiency, the Gang of Four's decorator pattern on X's iterator can be used to stack up only the changes and execute them on every call, reducing runtime complexity and avoiding multiple copies of the output data. A problem that remains is that if the input and the output are large, a lot of memory is required. This is a tricky situation and can only be avoided by the programmer thinking thoroughly about decomposing the function better.
- 數據庫基礎與應用:Access 2010
- 輕松學大數據挖掘:算法、場景與數據產品
- SQL Server 2008數據庫應用技術(第二版)
- 數據要素五論:信息、權屬、價值、安全、交易
- MySQL 8.x從入門到精通(視頻教學版)
- 網站數據庫技術
- “互聯網+”時代立體化計算機組
- Mastering LOB Development for Silverlight 5:A Case Study in Action
- MySQL DBA修煉之道
- 數據分析師養成寶典
- 區塊鏈+:落地場景與應用實戰
- Arquillian Testing Guide
- Trino權威指南(原書第2版)
- Unity 4.x Game AI Programming
- Access 2013 數據庫管理與應用從新手到高手