quick-bool 0.1.0

A lock-free boolean implementation using atomic operations
Documentation
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/env node
/**
 * Generate Mermaid charts from existing Cargo benchmark timing data
 * This script can be used when benchmarks have already been run
 */

const fs = require('fs');
const path = require('path');

function loadTimingDataFromFile() {
    try {
        // Look for Criterion benchmark data in target/criterion
        const criterionPath = 'target/criterion';
        
        if (!fs.existsSync(criterionPath)) {
            console.error("No Criterion benchmark data found. Please run benchmarks first.");
            throw new Error("No Criterion benchmark data found. Please run benchmarks first.");
        }
        
        console.log(`Loading timing data from ${criterionPath}...`);
        
        // Read all benchmark directories
        const implementations = fs.readdirSync(criterionPath)
            .filter(name => !name.startsWith('.') && name !== 'report');
        
        const timingData = [];
        
        for (const impl of implementations) {
            const implPath = path.join(criterionPath, impl);
            const implStats = fs.statSync(implPath);
            
            if (implStats.isDirectory()) {
                const operations = fs.readdirSync(implPath)
                    .filter(name => !name.startsWith('.'));
                
                for (const op of operations) {
                    const opPath = path.join(implPath, op, 'new', 'estimates.json');
                    
                    if (fs.existsSync(opPath)) {
                        try {
                            const content = fs.readFileSync(opPath, 'utf8');
                            const data = JSON.parse(content);
                            
                            // Extract timing data (values are already in nanoseconds)
                            const meanNs = data.mean.point_estimate;
                            const medianNs = data.median.point_estimate;
                            
                            timingData.push({
                                event: 'bench',
                                name: `benchmarks::${impl}::${op}`,
                                implementation: impl,
                                operation: op,
                                mean: meanNs,
                                median: medianNs
                            });
                        } catch (parseError) {
                            console.warn(`Failed to parse ${opPath}: ${parseError.message}`);
                        }
                    }
                }
            }
        }
        
        if (timingData.length > 0) {
            console.log(`Parsed ${timingData.length} benchmark results from Criterion data`);
            return timingData;
        } else {
            console.error("No valid benchmark data found in Criterion output");
            throw new Error("No valid benchmark data found in Criterion output");
        }
    } catch (error) {
        console.error("Error loading timing data:", error.message);
        throw error;
    }
}

function parseCriterionOutput(content) {
    const timingData = [];
    const lines = content.split('\n');
    
    let currentBenchmark = null;
    
    for (let i = 0; i < lines.length; i++) {
        const line = lines[i];
        const trimmedLine = line.trim();
        
        // Look for benchmark name lines (e.g., "QuickBool/first_access")
        if (trimmedLine.includes('/') && !trimmedLine.includes('time:') && !trimmedLine.includes('change:') && !trimmedLine.includes('slope') && !trimmedLine.includes('mean') && !trimmedLine.includes('median')) {
            const match = trimmedLine.match(/^([^/]+)\/(.+)$/);
            if (match) {
                currentBenchmark = {
                    name: `benchmarks::${match[1]}::${match[2]}`,
                    implementation: match[1],
                    operation: match[2]
                };
            }
        }
        
        // Look for timing data lines
        if (currentBenchmark && trimmedLine.startsWith('time:')) {
            const timeMatch = trimmedLine.match(/time:\s*\[([^\]]+)\]/);
            if (timeMatch) {
                const timeStr = timeMatch[1];
                
                // Split the time string into individual time values, keeping numbers and units together
                // The format is like "255.43 ps 256.54 ps 256.82 ps"
                const timeValues = [];
                const parts = timeStr.split(/\s+/).filter(s => s.trim());
                
                for (let j = 0; j < parts.length; j += 2) {
                    if (j + 1 < parts.length) {
                        timeValues.push(parts[j] + ' ' + parts[j + 1]);
                    }
                }
                
                if (timeValues.length >= 3) {
                    // Use the middle value (second value) as the median estimate
                    const medianTime = timeValues[1];
                    const ns = parseTimeToNanoseconds(medianTime);
                    if (ns > 0) {
                        timingData.push({
                            event: 'bench',
                            name: currentBenchmark.name,
                            implementation: currentBenchmark.implementation,
                            operation: currentBenchmark.operation,
                            mean: ns,
                            median: ns
                        });
                    }
                }
            }
        }
        
        // Handle case where benchmark name and time are on the same line
        // Format: "QuickBool/first_access  time:   [1.2073 ns 1.2274 ns 1.2325 ns]"
        if (trimmedLine.includes('/') && trimmedLine.includes('time:')) {
            const nameMatch = trimmedLine.match(/^([^/]+\/[^\s]+)\s+time:\s*\[([^\]]+)\]/);
            if (nameMatch) {
                const nameParts = nameMatch[1].split('/');
                const timeStr = nameMatch[2];
                
                currentBenchmark = {
                    name: `benchmarks::${nameParts[0]}::${nameParts[1]}`,
                    implementation: nameParts[0],
                    operation: nameParts[1]
                };
                
                // Parse the timing data
                const timeValues = [];
                const parts = timeStr.split(/\s+/).filter(s => s.trim());
                
                for (let j = 0; j < parts.length; j += 2) {
                    if (j + 1 < parts.length) {
                        timeValues.push(parts[j] + ' ' + parts[j + 1]);
                    }
                }
                
                if (timeValues.length >= 3) {
                    const medianTime = timeValues[1];
                    const ns = parseTimeToNanoseconds(medianTime);
                    if (ns > 0) {
                        timingData.push({
                            event: 'bench',
                            name: currentBenchmark.name,
                            implementation: currentBenchmark.implementation,
                            operation: currentBenchmark.operation,
                            mean: ns,
                            median: ns
                        });
                    }
                }
            }
        }
    }
    
    return timingData;
}

function parseTimeToNanoseconds(timeStr) {
    const trimmed = timeStr.trim();
    
    // Extract the numeric part and unit
    const match = trimmed.match(/^([0-9.]+)\s*([a-zA-Zµ]+)$/);
    if (!match) {
        return 0;
    }
    
    const numericValue = parseFloat(match[1]);
    const unit = match[2];
    
    if (isNaN(numericValue)) {
        return 0;
    }
    
    // Convert to nanoseconds based on unit
    if (unit === 'ns') {
        return numericValue;
    } else if (unit === 'ps') {
        return numericValue / 1000;
    } else if (unit === 'µs' || unit === 'us') {
        return numericValue * 1000;
    } else if (unit === 'ms') {
        return numericValue * 1000000;
    } else if (unit === 's') {
        return numericValue * 1000000000;
    }
    
    return 0;
}

function parseBenchmarkResults(timingData) {
    if (!timingData || timingData.length === 0) {
        return [];
    }
    
    const results = [];
    
    for (const benchmark of timingData) {
        // Extract implementation and operation from benchmark name
        // Expected formats:
        // 1. "benchmarks::QuickBool::first_access" (individual implementation)
        // 2. "benchmarks::Comparison::QuickBool_first" (comparison - skip these)
        const nameParts = benchmark.name.split('::');
        
        if (nameParts.length >= 3) {
            const groupName = nameParts[1]; // e.g., "QuickBool", "LazyLock", "OnceLock", "Comparison"
            const operation = nameParts[2];  // e.g., "first_access", "cached_access", "QuickBool_first"
            
            // Skip comparison benchmarks as they're redundant
            if (groupName === 'Comparison') {
                continue;
            }
            
            // Extract timing information
            const meanNs = benchmark.mean || 0;
            const medianNs = benchmark.median || 0;
            
            if (meanNs > 0 && medianNs > 0) {
                results.push({
                    implementation: groupName,
                    operation: operation,
                    mean: meanNs,
                    median: medianNs
                });
            }
        }
    }
    
    // Remove duplicates and keep the best (lowest mean) result for each implementation/operation pair
    const uniqueResults = [];
    const seen = new Set();
    
    results.sort((a, b) => a.mean - b.mean);
    
    for (const result of results) {
        const key = `${result.implementation}-${result.operation}`;
        if (!seen.has(key)) {
            seen.add(key);
            uniqueResults.push(result);
        }
    }
    
    return uniqueResults.sort((a, b) => {
        if (a.implementation === b.implementation) {
            return a.operation.localeCompare(b.operation);
        }
        return a.implementation.localeCompare(b.implementation);
    });
}

function generatePerformanceComparisonChart(results) {
    // Filter out operations that don't have meaningful data
    const meaningfulResults = results.filter(r => r.mean > 0.001); // Only include results > 1ps
    
    if (meaningfulResults.length === 0) {
        return '```mermaid\nxychart-beta\n    title "No benchmark data available"\n    x-axis "No data" ["No data"]\n    y-axis "Time" 0 --> 1\n    BAR [0]\n```';
    }
    
    // Only include operations that have data for all implementations
    const implementations = [...new Set(meaningfulResults.map(r => r.implementation))];
    const operationGroups = new Map();
    
    // Group results by operation
    for (const result of meaningfulResults) {
        if (!operationGroups.has(result.operation)) {
            operationGroups.set(result.operation, []);
        }
        operationGroups.get(result.operation).push(result);
    }
    
    // Include operations that have data for at least 2 implementations
    const validOperations = [];
    for (const [operation, results] of operationGroups) {
        if (results.length >= 2) {
            validOperations.push(operation);
        }
    }
    
    if (validOperations.length === 0) {
        return '```mermaid\nxychart-beta\n    title "No benchmark data available"\n    x-axis "No data" ["No data"]\n    y-axis "Time" 0 --> 1\n    BAR [0]\n```';
    }
    
    let chart = '```mermaid\nxychart-beta\n';
    chart += '    title "Performance Comparison: QuickBool vs LazyLock vs OnceLock"\n';
    chart += '    x-axis "Operation" [';
    chart += validOperations.map(op => `"${op}"`).join(', ');
    chart += ']\n';
    
    // Calculate appropriate y-axis range
    const maxValue = Math.max(...meaningfulResults.map(r => r.mean));
    const yMax = Math.ceil(maxValue * 1.2); // Add 20% padding
    
    chart += `    y-axis "Time (nanoseconds)" 0 --> ${yMax}\n`;
    
    // Add data for each implementation
    for (const impl of implementations) {
        const data = validOperations.map(op => {
            const result = meaningfulResults.find(r => 
                r.implementation === impl && r.operation === op);
            if (result && result.mean > 0.001) {
                return result.mean.toFixed(3);
            }
            // If no data for this implementation/operation, skip this implementation
            return null;
        });
        
        // Only include implementations that have data for most operations
        const validData = data.filter(d => d !== null);
        if (validData.length >= validOperations.length * 0.5) { // At least 50% of operations have data
            const cleanData = data.map(d => d || '0.001'); // Use small non-zero value instead of 0
            chart += `    LINE [${cleanData.join(', ')}] "${impl}"\n`;
        }
    }
    
    chart += '```';
    return chart;
}

function generateFirstAccessChart(results) {
    const firstAccess = results.filter(r => r.operation.includes('first') && r.mean > 0.001);
    
    if (firstAccess.length === 0) {
        return '```mermaid\nxychart-beta\n    title "No first access data available"\n    x-axis "No data" ["No data"]\n    y-axis "Time" 0 --> 1\n    BAR [0]\n```';
    }
    
    let chart = '```mermaid\nxychart-beta\n';
    chart += '    title "First Access Performance Comparison"\n';
    chart += '    x-axis "Implementation" [';
    chart += firstAccess.map(r => `"${r.implementation}"`).join(', ');
    chart += ']\n';
    
    // Calculate appropriate y-axis range
    const maxValue = Math.max(...firstAccess.map(r => r.mean));
    const yMax = Math.ceil(maxValue * 1.2); // Add 20% padding
    
    chart += `    y-axis "Time (nanoseconds)" 0 --> ${yMax}\n`;
    
    const data = firstAccess.map(r => r.mean.toFixed(3));
    chart += `    BAR [${data.join(', ')}]\n`;
    
    chart += '```';
    return chart;
}

function generateCachedAccessChart(results) {
    const cachedAccess = results.filter(r => r.operation.includes('cached') && r.mean > 0.001);
    
    if (cachedAccess.length === 0) {
        return '```mermaid\nxychart-beta\n    title "No cached access data available"\n    x-axis "No data" ["No data"]\n    y-axis "Time" 0 --> 1\n    BAR [0]\n```';
    }
    
    // Group by implementation and get the best cached performance
    const bestCached = [];
    const seen = new Set();
    
    for (const result of cachedAccess) {
        if (!seen.has(result.implementation)) {
            seen.add(result.implementation);
            bestCached.push(result);
        }
    }
    
    let chart = '```mermaid\nxychart-beta\n';
            chart += '    title "Cached Access Performance"\n';
    chart += '    x-axis "Implementation" [';
    chart += bestCached.map(r => `"${r.implementation}"`).join(', ');
    chart += ']\n';
    
    // Calculate appropriate y-axis range
    const maxValue = Math.max(...bestCached.map(r => r.mean));
    const yMax = Math.ceil(maxValue * 1.2); // Add 20% padding
    
    chart += `    y-axis "Time (nanoseconds)" 0 --> ${yMax}\n`;
    
    const data = bestCached.map(r => r.mean.toFixed(3));
    chart += `    BAR [${data.join(', ')}]\n`;
    
    chart += '```';
    return chart;
}

// Main execution
function main() {
    console.log("Loading existing benchmark timing data...");
    
    // Load timing data from file
    const timingData = loadTimingDataFromFile();
    
    if (!timingData || timingData.length === 0) {
        console.error("No benchmark timing data found");
        return;
    }
    
    console.log(`Found ${timingData.length} benchmark timing records`);
    
    // Parse the results
    const results = parseBenchmarkResults(timingData);
    
    if (!results || results.length === 0) {
        console.error("No benchmark results found");
        return;
    }
    
    console.log(`Parsed ${results.length} benchmark results`);
    
    // Ensure output directory exists
    const outputDir = "tmp/md";
    fs.mkdirSync(outputDir, { recursive: true });
    
    console.log("Generating Mermaid charts...");
    const charts = {
        firstAccess: generateFirstAccessChart(results),
        cachedAccess: generateCachedAccessChart(results)
    };
    
    // Write charts to files
    fs.writeFileSync('tmp/md/first_access_chart.md', charts.firstAccess);
    fs.writeFileSync('tmp/md/cached_access_chart.md', charts.cachedAccess);
    
    console.log("Mermaid charts generated successfully!");
}

main();