Cookbook Overview
Practical recipes showing how to solve real-world problems with Blitz. Each recipe follows the same structure: a problem statement, a complete solution with working Zig code, an explanation of which APIs are used and why, and performance guidance.
Recipes
Section titled “Recipes”| Recipe | Description | Key APIs |
|---|---|---|
| Image Processing | Pixel transforms, grayscale conversion, and gaussian blur on image buffers | parallelFor, iterMut().mapInPlace() |
| CSV Data Parsing | Parse CSV lines in parallel and aggregate numeric results | parallelReduce, join() |
| Monte Carlo Simulation | Estimate pi using random sampling across threads | parallelReduce, range() |
| Log File Analysis | Search logs for patterns, count errors, and find specific entries | iter().any(), iter().findAny(), join() |
| Matrix Multiplication | Dense matrix multiply with parallel row computation | parallelFor with context |
Choosing the Right API
Section titled “Choosing the Right API”These recipes demonstrate different Blitz APIs. Use this guide to pick the right one for your task:
| Task Pattern | Recommended API | Why |
|---|---|---|
| Transform every element | iterMut().mapInPlace() | In-place, zero allocation |
| Aggregate to single value | iter().sum() / .min() / .max() | Optimized parallel reduction |
| Custom aggregation | iter().reduce() or parallelReduce() | Full control over combine logic |
| Search for element | iter().findAny() / .any() / .all() | Early exit stops all workers |
| Index-based loop with context | parallelFor() | Access external data via context struct |
| Multiple independent tasks | join() | Named results, heterogeneous types |
| Range-based computation | range().forEach() / .sum() | No backing slice needed |
Common Patterns Across Recipes
Section titled “Common Patterns Across Recipes”Context Structs
Section titled “Context Structs”Most low-level APIs use a context struct to pass data into the parallel body:
const Context = struct { input: []const f64, output: []f64, scale: f64,};
blitz.parallelFor(n, Context, .{ .input = input, .output = output, .scale = 2.0,}, struct { fn body(ctx: Context, start: usize, end: usize) void { for (ctx.input[start..end], ctx.output[start..end]) |in, *out| { out.* = in * ctx.scale; } }}.body);Sequential Thresholds
Section titled “Sequential Thresholds”For small data sizes, the overhead of parallelism exceeds the benefit. Blitz handles this internally for iterator operations, but for custom algorithms you should add your own thresholds:
if (data.len < 10_000) { // Sequential fallback for (data) |*v| v.* = transform(v.*);} else { blitz.iterMut(f64, data).mapInPlace(transform);}Associative Combine Functions
Section titled “Associative Combine Functions”When using parallelReduce or iter().reduce(), the combine function must be associative. Addition, multiplication, min, and max all qualify. Subtraction and division do not.