Performance
508 bites tagged Performance — interview questions with model answers, and 60-second explainers.
measure: Baseline and Compare Code Performance
`measure` in XCTest establishes a performance baseline for a block of code and fails tests if it regresses. It tracks critical algorithm speed, running the code ten times for a stable average. The footgun is ignoring the baseline: you must set it first.
Xcode's Time Profiler: Hunting Down Performance Bottlenecks
Time Profiler is a stopwatch for your code, sampling your app's threads to see which functions are running most often. Use it to diagnose slow UI or high battery drain by finding CPU "hot spots."
Go Assembly: A Semi-Abstract Instruction Set
Go's assembler isn't a direct mapping to machine code; it's a semi-abstract instruction set. A `MOV` might become a `clear` or `load`. This is what you see with `go tool compile -S`. The footgun is assuming your assembly maps 1:1 to the final machine code.
Go's `unsafe` Package: Breaking the Rules for Performance
Go's `unsafe` package lets you bypass type safety, treating memory like C with raw pointers for performance gains. It's used for low-level optimizations and C interoperability. The footgun: its behavior isn't guaranteed across Go versions, making code fragile.
Go Execution Tracer: Pinpointing Concurrency Bottlenecks
Go's Execution Tracer creates a visual timeline of your program, capturing goroutine state changes, syscalls, and GC events. It's essential for diagnosing subtle concurrency issues like lock contention. The main footgun is misusing annotations for work.
Criterion: Statistical Benchmarking for Rust
Criterion isn't just a stopwatch; it's a statistical lab for your code. It provides stable performance metrics by running functions many times, letting you detect regressions and prove optimizations. The footgun is ignoring its statistical reports.
Go's pprof: Finding Your Code's Hotspots
pprof is a heat map for your code, revealing which functions consume the most CPU. It samples your program's call stacks to find performance hotspots. Use it to diagnose slow API endpoints or high-CPU background jobs. The footgun: profiling under no load.
Regex Engines: Backtracking vs. Finite Automata
A backtracking regex engine tries one path at a time, which can be fast but also exponentially slow. A finite-automata engine (like Go's) checks all paths at once, guaranteeing linear time. The footgun is using a backtracking engine on untrusted user input.
Buffered I/O: Batch System Calls for Speed
Buffered I/O batches many small reads or writes into fewer, larger system calls, trading a small amount of memory for a huge speed boost. It's essential for tasks like writing log files line-by-line, preventing a system call for every single line.
Go Benchmarking: Measure, Don't Guess
Go's benchmark runner finds stable performance numbers by repeatedly calling your code in a loop controlled by b.N. Use it to optimize hot paths or compare algorithm implementations. Forgetting b.ResetTimer() will include setup costs, skewing your results.
Rust's async/await: Cooperative Concurrency
Rust's async/await is cooperative concurrency, where tasks explicitly yield control with `.await`. This is ideal for I/O-bound work like managing thousands of network connections. The biggest footgun: calling an `async` function without `.await` does nothing.
Static Dispatch: Zero-Cost Abstraction via Monomorphization
Static dispatch resolves function calls at compile time, avoiding runtime overhead. Rust does this via monomorphization, creating specialized code for each concrete type. This is the default for generics, but the trade-off is larger binary sizes.
GC vs. Ownership: Two Paths to Memory Safety
Rust's ownership model provides memory safety at compile-time, aiming for C++-level performance without a garbage collector. This makes it ideal for systems programming where resource control is key. The footgun is assuming all "safe" languages are equal.
Stack vs. Heap: Where Go Puts Your Data
The stack is a fast, last-in-first-out region for local, fixed-size data. The heap is slower, flexible memory for dynamic data or values that escape a function's scope.
Go's sync.Map: A Specialized Concurrent Map
Go's `sync.Map` is a concurrent map optimized for keys written once and read many times. It's ideal for long-lived caches, but it's not a generic replacement for a map with a mutex. The footgun is using it for frequent writes, which can be slower.
Go Pointers: Memory Addresses, Not Math
Go pointers are street addresses for data. Instead of copying a large struct, you pass its memory address. This lets functions modify the original value and is critical for performance.
Zero-Cost Abstractions: Pay at Compile Time, Not Runtime
Zero-cost abstractions let you write high-level code that compiles to the same machine code as low-level optimizations. This is key in Rust for safe APIs without runtime overhead.
Go's Garbage Collector: The Concurrent Cleaner
Go's garbage collector is a concurrent cleaning crew, freeing memory while your program runs. It automatically reclaims unused memory, preventing leaks without manual `free()` calls. The footgun is assuming it's free; excessive allocations create GC pressure.
Performance Profiling in Tests
Performance profiling in tests means capturing frame build and raster times during a scripted, automated run instead of eyeballing smoothness, so jank regressions get caught in CI before they ever reach a real device.
Flutter's `compute`: Offload Heavy Work from the UI Thread
Flutter's `compute` function runs heavy calculations in the background to prevent your app's UI from freezing. Use it for tasks like parsing large JSON or complex math. The footgun: on the web, it runs on the same event loop, not in a true parallel thread.
Flutter Shaders: GPU Power for Custom Graphics
Shaders are small programs running on the GPU to create custom visual effects. Use them for high-performance graphics like frosted glass or animated gradients that standard widgets can't handle.
Impeller: Flutter's Jank-Free Rendering Engine
Impeller is Flutter's new rendering engine that pre-compiles shaders to deliver silky-smooth animations. It replaces the Skia engine to eliminate "jank" from on-the-fly shader compilation. The footgun: it only fixes rendering jank, not slow app logic.
Find Flutter Memory Leaks with DevTools
Think of the DevTools Memory view as an MRI for your app's RAM. It helps you find objects that aren't being garbage collected, diagnose bloat, and fix crashes.
Find Jank with Flutter's CPU Flame Charts
A flame chart visualizes CPU usage, showing which function calls are slowest. Use it to diagnose jank in Flutter. The footgun is misreading the x-axis: it's for sorting calls alphabetically, not showing execution order.
Get Performance bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.