《王璞-Rust大会2023.pdf》由会员分享,可在线阅读,更多相关《王璞-Rust大会2023.pdf(19页珍藏版)》请在三个皮匠报告上搜索。
1、第三届中国第三届中国Rust开开发发者大会者大会Rust Atomic Deep DivePu Wang DatenLord2023/06/17Rust原子操作高性能实践What are atomic operations in Rust?WhatWhy need atomic operations?WhyHow010203Memory order in atomic operationsMemory ModelThe overhead of atomic operationsCache CoherenceSummary040506How to understand atomic operat
2、ions?Atomic operation best practiceOutlineWhat are Atomic Operations in Rust?fn compare_exchange(&self,/AtomicI8 current:i8,new:i8,success:Ordering,failure:Ordering)-ResultCompare and SwapFetch and Modifyfn fetch_add(&self,val:i8,order:Ordering)-i8fn fetch_and(&self,val:bool,order:Ordering)-bool01Lo
3、ck-free programmingWhy Atomic Operation?High PerformanceLock-context switchAtomic-no context switchShared Variable Access When Multi-threading02How to Understand Atomic Operations?Memory OrderThe order of load/store instructions accessing memory.Memory ModelCache CoherenceAtomic operation overheadAt
4、omic operations will change cache line status which might flush cache lines.03Program orderInstructions executed as the order defined in a threadMemory orderMemory access instructionsload&storeOut of orderInstruction reorder by compilersOut of order execution in CPUload v.s.loadload v.s.storestore v
5、.s.loadstore v.s.storeMemory Model04Instruction Reorder by Compilers04Write buffer/store bufferDelayed writeStore forwardingTotal store order(TSO)FIFO writer bufferOoO store v.s.loadPartial store order(PSO)Non-FIFO write bufferOoO store v.s.loadOoO store v.s.storeOut of Order Execution04Memory Order
6、 in C+/RustSequential ConsistencyAcquire04ReleaseAcqRelRelaxedConsumeEach processor issues memory operations in program orderThe switch provides the global serialization among all memory operationsSequential Consistency04A write-release gua