%title: Zero-cost %author: Ivan Tham %date: 2021-07-14 %usage: mdp slide.md -> Zero-Cost Abstraction <- =========================== Quite search on youtube and I found that there are quite a few talks on this for the past month. --- -> What is zero-cost abstractions? <- ===================================== https://doc.rust-lang.org/book/ch00-00-introduction.html https://prev.rust-lang.org/en-US/ (old website) How it works? Any guess? --- -> Abstraction? <- ================== - high-level concepts - classes, interfaces, traits - iterator rather than imperative loop - Example ```rust let v1: Vec = vec![1, 2, 3]; let v2: Vec<_> = v1.iter().map(|x| x + 1).collect(); ``` Let's talk about the second `Vec<_>` later. --- -> Common misconception of zero-cost <- ======================================= At least for me, at first I thought any code you write will magically become faster due to "zero-cost", but that is not the case. Let's go through it later. -> Iterator basics <- ===================== Syntactic sugar for `while i.next() { ... }`. https://cheats.rs/#iterators --- -> Abstractions, but zero-cost? <- ================================== Let's see what it can do. An example is iterator. https://doc.rust-lang.org/book/ch13-04-performance.html From what I see online, abstractions usually have a runtime cost. In rust, usually iterator perform as good as hand-rolled loops or faster. Zero-cost abstractions convert runtime cost into compile-time cost. --- -> Types and few other tricks <- ================================ Show few types in `std::iter`. Iterator, different types for different iterator. Adapters `Map`, `Take`... Traits help in this, different types have different traits. - `DoubleEndedIterator` - `ExactSizeIterator` - `FusedIterator` Speeding up an iterator is usually done by. - `Iterator::size_hint` - `TrustedLen` (nightly) - `InPlaceIterable` (internal) New: since rust 1.50 can use const-generic array indexing. Can do `[1, 2, 3]` instead of `[1, 2, 3].iter()`. E.g. `[1].chain([2])`. --- -> Other benefits of iterator <- ================================ - Expressiveness, able to collect into multiple types (`HashSet`, `HashMap`) - Auto-vectorization, use SIMD for free (most cases due to types in place) - Can switch to rayon easily to parallelize stuff (`iter` to `par_iter`) --- -> Footguns <- ============== `peekable` + `as_ref` when used with other combinators can easily be confused. --- -> Go through few iterators in std <- ===================================== Just for `Iterator` - `first`, `last`, `nth`, `find`, `position` - `step_by`, `max`, `min` - `zip`, `enumerate` - `map`, `filter`, `filter_map`, `fold`, `skip_while`, `take_while`, `skip` - `try_*` - `fold`, `reduce` - `peekable`, `flatten` - `inspect`, `by_ref` - `partition`, `windows` - `collect` - `all`, `any` - `sum`, `product`, `cycle` - ... --- ``` _ J█▄ ▄██ _██ __ __ ███▄███████████████ _ ╚███████████_ _███████████ì ⌠███████▀▀ "▀██" "▀████████ / (_) _____ ▄▄▄████▀ ▀█████▄▄µ / / / | / / _ \ ▀█████████████████████████▄_ ▀█████ / / /| |/ / __/ ███████████████████████████████¡ ██████ì /_/_/ |___/\___/ _██▀"▀█▀▀████████▀▀▀▀▀▀▀████████ ██▀▀██▄_ *████▄▄▄█▀ ⌠███████ _███████▀ ⌠██▄▄████▀ _████" ⌠███████████████████▀` "████▄ __ ▀█████ ⌠████████████████████, ,_█████▀ _________ ____/ /__ √████¡ ⌠███████ ▀███████ ██████▄ / ___/ __ \/ __ / _ \ '▀█████████████████▄▄▄▄ █████████████████▀▀ / /__/ /_/ / /_/ / __/ ▄████████████████████ ▀████████████████ \___/\____/\__,_/\___/ ▀▀██████▀▀▀▀▀▀▀▀▀▀▀▀▀ "▀▀▀▀▀▀▀▀█████▀▀` ██████▄▄███ ▄██▄▄██████¡ ╓████⌐ █ì ╔█' █████ ⌠▀▀▀███████▄▄√______▄▄████████▀▀▀ ╚██▀███████████████████▀▀█ì ⌠█▀ ███▀███▀███ ▀▀█ " ▀ " ```