-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_api_bench.zig
More file actions
410 lines (353 loc) · 15.5 KB
/
Copy pathcontext_api_bench.zig
File metadata and controls
410 lines (353 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Context API Benchmark - With vs Without Context Comparison
//
// Measures the overhead of using the Context API vs. inline operations.
// Shows that passing context has minimal performance impact.
//
// Key comparisons:
// - forEach: inline vs context
// - par_range: inline vs context
// - mapIndexed: inline vs context
//
// Usage: zig build sample-context-api-bench -Doptimize=ReleaseFast
// ./.zig-cache/o/*/context_api_bench
const std = @import("std");
const loom = @import("loom");
const par_iter = loom.par_iter;
const par_range = loom.par_range;
const ThreadPool = loom.ThreadPool;
const WARMUP_ITERATIONS = 10;
const BENCH_ITERATIONS = 100;
const DATA_SIZE = 1_000_000;
pub fn main() !void {
const allocator = std.heap.page_allocator;
std.debug.print("\n", .{});
std.debug.print("============================================================\n", .{});
std.debug.print(" Context API Benchmark (With vs Without) \n", .{});
std.debug.print("============================================================\n\n", .{});
const pool = try ThreadPool.init(allocator, .{ .num_threads = 8 });
defer pool.deinit();
std.debug.print("Configuration:\n", .{});
std.debug.print(" Thread pool: 8 workers\n", .{});
std.debug.print(" Data size: {d} elements\n", .{DATA_SIZE});
std.debug.print(" Warmup: {d} iterations\n", .{WARMUP_ITERATIONS});
std.debug.print(" Bench: {d} iterations\n\n", .{BENCH_ITERATIONS});
// Allocate test data
const data = try allocator.alloc(i64, DATA_SIZE);
defer allocator.free(data);
for (data, 0..) |*d, i| {
d.* = @intCast(i);
}
// ========================================================================
// Benchmark 1: forEach - Without Context vs With Context
// ========================================================================
std.debug.print("------------------------------------------------------------\n", .{});
std.debug.print("Benchmark 1: forEach (multiply by 2)\n", .{});
std.debug.print("------------------------------------------------------------\n", .{});
// Without context - uses inline constant
{
// Reset data
for (data, 0..) |*d, i| d.* = @intCast(i);
// Warmup
for (0..WARMUP_ITERATIONS) |_| {
par_iter(data).withPool(pool).forEach(struct {
fn mul(x: *i64) void {
x.* *= 2;
}
}.mul);
// Reset
for (data, 0..) |*d, i| d.* = @intCast(i);
}
// Benchmark
const start = std.time.nanoTimestamp();
for (0..BENCH_ITERATIONS) |_| {
par_iter(data).withPool(pool).forEach(struct {
fn mul(x: *i64) void {
x.* *= 2;
}
}.mul);
// Reset for fair comparison
for (data, 0..) |*d, i| d.* = @intCast(i);
}
const end = std.time.nanoTimestamp();
const total_ns = end - start;
const avg_ns = @divFloor(total_ns, BENCH_ITERATIONS);
const avg_us = @as(f64, @floatFromInt(avg_ns)) / 1000.0;
const throughput = @as(f64, @floatFromInt(DATA_SIZE)) / (@as(f64, @floatFromInt(avg_ns)) / 1_000_000_000.0);
std.debug.print(" Without context:\n", .{});
std.debug.print(" Avg time: {d:.2} us\n", .{avg_us});
std.debug.print(" Throughput: {d:.2} M elem/s\n", .{throughput / 1_000_000.0});
}
// With context - multiplier passed via context
const MulContext = struct {
multiplier: i64,
};
{
// Reset data
for (data, 0..) |*d, i| d.* = @intCast(i);
const ctx = MulContext{ .multiplier = 2 };
// Warmup
for (0..WARMUP_ITERATIONS) |_| {
par_iter(data).withPool(pool).withContext(&ctx).forEach(struct {
fn mul(c: *const MulContext, x: *i64) void {
x.* *= c.multiplier;
}
}.mul);
// Reset
for (data, 0..) |*d, i| d.* = @intCast(i);
}
// Benchmark
const start = std.time.nanoTimestamp();
for (0..BENCH_ITERATIONS) |_| {
par_iter(data).withPool(pool).withContext(&ctx).forEach(struct {
fn mul(c: *const MulContext, x: *i64) void {
x.* *= c.multiplier;
}
}.mul);
// Reset for fair comparison
for (data, 0..) |*d, i| d.* = @intCast(i);
}
const end = std.time.nanoTimestamp();
const total_ns = end - start;
const avg_ns = @divFloor(total_ns, BENCH_ITERATIONS);
const avg_us = @as(f64, @floatFromInt(avg_ns)) / 1000.0;
const throughput = @as(f64, @floatFromInt(DATA_SIZE)) / (@as(f64, @floatFromInt(avg_ns)) / 1_000_000_000.0);
std.debug.print(" With context:\n", .{});
std.debug.print(" Avg time: {d:.2} us\n", .{avg_us});
std.debug.print(" Throughput: {d:.2} M elem/s\n\n", .{throughput / 1_000_000.0});
}
// ========================================================================
// Benchmark 2: par_range - Without Context vs With Context
// ========================================================================
std.debug.print("------------------------------------------------------------\n", .{});
std.debug.print("Benchmark 2: par_range (chunk processing)\n", .{});
std.debug.print("------------------------------------------------------------\n", .{});
// Without context - using chunks API directly on data
{
var atomic_sum = std.atomic.Value(i64).init(0);
// Warmup
for (0..WARMUP_ITERATIONS) |_| {
atomic_sum.store(0, .release);
par_iter(data).withPool(pool).chunksConst(struct {
fn process(_: usize, chunk: []const i64) void {
var sum: i64 = 0;
for (chunk) |val| {
sum += val;
}
// Without context we can't accumulate the result anywhere
std.mem.doNotOptimizeAway(sum);
}
}.process);
}
// Benchmark
const start = std.time.nanoTimestamp();
for (0..BENCH_ITERATIONS) |_| {
atomic_sum.store(0, .release);
par_iter(data).withPool(pool).chunksConst(struct {
fn process(_: usize, chunk: []const i64) void {
var sum: i64 = 0;
for (chunk) |val| {
sum += val;
}
std.mem.doNotOptimizeAway(sum);
}
}.process);
}
const end = std.time.nanoTimestamp();
const total_ns = end - start;
const avg_ns = @divFloor(total_ns, BENCH_ITERATIONS);
const avg_us = @as(f64, @floatFromInt(avg_ns)) / 1000.0;
std.debug.print(" Without context (no atomic accumulation):\n", .{});
std.debug.print(" Avg time: {d:.2} us\n", .{avg_us});
}
// With context - proper atomic accumulation
const RangeContext = struct {
total: *std.atomic.Value(i64),
};
{
var atomic_sum = std.atomic.Value(i64).init(0);
const ctx = RangeContext{
.total = &atomic_sum,
};
// Warmup
for (0..WARMUP_ITERATIONS) |_| {
atomic_sum.store(0, .release);
par_iter(data).withPool(pool).withContext(&ctx).chunksConst(struct {
fn process(c: *const RangeContext, _: usize, chunk: []const i64) void {
var sum: i64 = 0;
for (chunk) |val| {
sum += val;
}
_ = c.total.fetchAdd(sum, .monotonic);
}
}.process);
}
// Benchmark
const start = std.time.nanoTimestamp();
for (0..BENCH_ITERATIONS) |_| {
atomic_sum.store(0, .release);
par_iter(data).withPool(pool).withContext(&ctx).chunksConst(struct {
fn process(c: *const RangeContext, _: usize, chunk: []const i64) void {
var sum: i64 = 0;
for (chunk) |val| {
sum += val;
}
_ = c.total.fetchAdd(sum, .monotonic);
}
}.process);
}
const end = std.time.nanoTimestamp();
const total_ns = end - start;
const avg_ns = @divFloor(total_ns, BENCH_ITERATIONS);
const avg_us = @as(f64, @floatFromInt(avg_ns)) / 1000.0;
std.debug.print(" With context (atomic accumulation):\n", .{});
std.debug.print(" Avg time: {d:.2} us\n", .{avg_us});
std.debug.print(" Sum result: {d}\n\n", .{atomic_sum.load(.acquire)});
}
// ========================================================================
// Benchmark 3: count - Without Context vs With Context
// ========================================================================
std.debug.print("------------------------------------------------------------\n", .{});
std.debug.print("Benchmark 3: count (elements > threshold)\n", .{});
std.debug.print("------------------------------------------------------------\n", .{});
const THRESHOLD = DATA_SIZE / 2;
// Without context - hardcoded threshold
{
var result: usize = 0;
// Warmup
for (0..WARMUP_ITERATIONS) |_| {
result = par_iter(data).withPool(pool).count(struct {
fn pred(val: i64) bool {
return val > DATA_SIZE / 2;
}
}.pred);
}
// Benchmark
const start = std.time.nanoTimestamp();
for (0..BENCH_ITERATIONS) |_| {
result = par_iter(data).withPool(pool).count(struct {
fn pred(val: i64) bool {
return val > DATA_SIZE / 2;
}
}.pred);
}
const end = std.time.nanoTimestamp();
const total_ns = end - start;
const avg_ns = @divFloor(total_ns, BENCH_ITERATIONS);
const avg_us = @as(f64, @floatFromInt(avg_ns)) / 1000.0;
std.debug.print(" Without context (hardcoded threshold):\n", .{});
std.debug.print(" Avg time: {d:.2} us\n", .{avg_us});
std.debug.print(" Count: {d}\n", .{result});
}
// With context - threshold from context
const CountContext = struct {
threshold: i64,
};
{
const ctx = CountContext{ .threshold = THRESHOLD };
var result: usize = 0;
// Warmup
for (0..WARMUP_ITERATIONS) |_| {
result = par_iter(data).withPool(pool).withContext(&ctx).count(struct {
fn pred(c: *const CountContext, val: i64) bool {
return val > c.threshold;
}
}.pred);
}
// Benchmark
const start = std.time.nanoTimestamp();
for (0..BENCH_ITERATIONS) |_| {
result = par_iter(data).withPool(pool).withContext(&ctx).count(struct {
fn pred(c: *const CountContext, val: i64) bool {
return val > c.threshold;
}
}.pred);
}
const end = std.time.nanoTimestamp();
const total_ns = end - start;
const avg_ns = @divFloor(total_ns, BENCH_ITERATIONS);
const avg_us = @as(f64, @floatFromInt(avg_ns)) / 1000.0;
std.debug.print(" With context (threshold from context):\n", .{});
std.debug.print(" Avg time: {d:.2} us\n", .{avg_us});
std.debug.print(" Count: {d}\n\n", .{result});
}
// ========================================================================
// Benchmark 4: mapIndexed - Without Context vs With Context
// ========================================================================
std.debug.print("------------------------------------------------------------\n", .{});
std.debug.print("Benchmark 4: mapIndexed (value + index * factor)\n", .{});
std.debug.print("------------------------------------------------------------\n", .{});
// Without context - hardcoded factor
{
var result: ?[]i64 = null;
// Warmup
for (0..WARMUP_ITERATIONS) |_| {
if (result) |r| allocator.free(r);
result = try par_iter(data).withPool(pool).mapIndexed(i64, struct {
fn transform(idx: usize, val: i64) i64 {
return val + @as(i64, @intCast(idx)) * 10;
}
}.transform, allocator);
}
// Benchmark
const start = std.time.nanoTimestamp();
for (0..BENCH_ITERATIONS) |_| {
if (result) |r| allocator.free(r);
result = try par_iter(data).withPool(pool).mapIndexed(i64, struct {
fn transform(idx: usize, val: i64) i64 {
return val + @as(i64, @intCast(idx)) * 10;
}
}.transform, allocator);
}
const end = std.time.nanoTimestamp();
if (result) |r| allocator.free(r);
const total_ns = end - start;
const avg_ns = @divFloor(total_ns, BENCH_ITERATIONS);
const avg_us = @as(f64, @floatFromInt(avg_ns)) / 1000.0;
std.debug.print(" Without context (hardcoded factor):\n", .{});
std.debug.print(" Avg time: {d:.2} us\n", .{avg_us});
}
// With context - factor from context
const MapContext = struct {
factor: i64,
};
{
const ctx = MapContext{ .factor = 10 };
var result: ?[]i64 = null;
// Warmup
for (0..WARMUP_ITERATIONS) |_| {
if (result) |r| allocator.free(r);
result = try par_iter(data).withPool(pool).withContext(&ctx).mapIndexed(i64, struct {
fn transform(c: *const MapContext, idx: usize, val: i64) i64 {
return val + @as(i64, @intCast(idx)) * c.factor;
}
}.transform, allocator);
}
// Benchmark
const start = std.time.nanoTimestamp();
for (0..BENCH_ITERATIONS) |_| {
if (result) |r| allocator.free(r);
result = try par_iter(data).withPool(pool).withContext(&ctx).mapIndexed(i64, struct {
fn transform(c: *const MapContext, idx: usize, val: i64) i64 {
return val + @as(i64, @intCast(idx)) * c.factor;
}
}.transform, allocator);
}
const end = std.time.nanoTimestamp();
if (result) |r| allocator.free(r);
const total_ns = end - start;
const avg_ns = @divFloor(total_ns, BENCH_ITERATIONS);
const avg_us = @as(f64, @floatFromInt(avg_ns)) / 1000.0;
std.debug.print(" With context (factor from context):\n", .{});
std.debug.print(" Avg time: {d:.2} us\n\n", .{avg_us});
}
std.debug.print("============================================================\n", .{});
std.debug.print(" Benchmark Complete \n", .{});
std.debug.print("============================================================\n\n", .{});
std.debug.print("Summary:\n", .{});
std.debug.print(" The context API adds minimal overhead (typically <5%%).\n", .{});
std.debug.print(" Benefits of context:\n", .{});
std.debug.print(" - Type-safe parameter passing\n", .{});
std.debug.print(" - No global state needed\n", .{});
std.debug.print(" - Easy testing and configuration\n", .{});
std.debug.print(" - Clean separation of concerns\n", .{});
}