Skip to content

v0.1.1

Choose a tag to compare

@10d9e 10d9e released this 25 Oct 01:33
· 14 commits to main since this release
0b88c7c

Release Notes - v0.1.1

Release Date: October 24, 2025
Focus: Performance Optimization & Code Quality Improvements

Performance Improvements

SIMD Vectorization (+4.8% Performance)

Implemented comprehensive SIMD optimizations using Zig's @Vector type, targeting the most critical hot paths:

TLWE Operations

  • Vectorized 6 core array operations: add, sub, neg, mul, addMul, subMul
  • Used Vec4<u32> for torus arithmetic operations
  • Impact: 2.6% overall performance improvement

TRGSW Operations

  • Vectorized complex FMA (Fused Multiply-Add) in frequency domain
  • Optimized fmaInFd1024() with Vec4<f64> for complex operations
  • Processes 4 complex numbers per iteration

FFT Operations

  • Vectorized ifft1024() twist loop (512 complex multiplications)
  • Vectorized fft1024() untwist loop (512 complex conjugate multiplications)
  • Vectorized poly_mul() complex multiplication
  • Impact: 2.3% overall performance improvement (on top of TLWE gains)

Benchmark Results

Metric Before After Improvement
Bootstrap time 38.08 ms 36.73 ms 3.5% faster
Per-gate time 39.20 ms 37.31 ms 4.8% faster
Full 80-gate test 3107 ms 2985 ms 122 ms saved

Gap vs Rust: Improved from 2.62x to 2.51x (4.2% closer to Rust performance)

Code Quality Improvements

Naming Conventions

  • Renamed: rayon_impl.zig → thread_pool.zig
  • Renamed: RayonRailgun → ThreadPool
  • Rationale: "Rayon" is Rust-specific; "ThreadPool" is language-agnostic and more descriptive

Bug Fixes

  • Fixed test memory deallocation errors in trgsw.zig
  • Removed incorrect defer free() calls on stack-allocated arrays
  • All tests now pass cleanly

Build System

  • Removed temporary profiling tool (bootstrap_profiler.zig)
  • Cleaned up build configuration
  • Ensured optimization flags propagate correctly to all modules

Documentation

New Documentation Files

Performance Analysis

  • SIMD_OPTIMIZATION_RESULTS.md - Detailed TLWE & TRGSW SIMD results
  • FFT_SIMD_OPTIMIZATION_RESULTS.md - Comprehensive FFT optimization analysis
  • SIMD_VERIFICATION.md - Verification that @Vector uses native NEON instructions
  • PROFILING_RESULTS.md - Initial profiling findings
  • PROFILING_SUMMARY.md - Executive summary

Each document includes:

  • Performance metrics and benchmarks
  • Code examples with before/after comparisons
  • Technical analysis of optimizations
  • Verification methodology
  • Next steps and recommendations

Key Documentation Highlights

SIMD Verification:

  • Confirmed Zig's @Vector compiles to native ARM64 NEON instructions
  • Verified using assembly output analysis
  • Explained optimal vector sizes for ARM64 (128-bit NEON registers)

Performance Analysis:

  • Identified FFT as dominant bottleneck (90% of execution time)
  • Documented memory profiling (zero allocations in hot path)
  • Explained why gains are modest (memory bandwidth, not compute)

Technical Details

SIMD Implementation

The vectorization uses platform-agnostic Zig code that compiles to native SIMD:

const Vec4 = @Vector(4, f64);

// Process 4 elements at once
var i: usize = 0;
while (i + 4 <= array.len) : (i += 4) {
    const a: Vec4 = array_a[i..][0..4].*;
    const b: Vec4 = array_b[i..][0..4].*;
    result[i..][0..4].* = a + b;  // Compiles to NEON instructions
}

Platform Support:

  • ARM64: NEON instructions (verified on Apple M3)
  • x86_64: SSE/AVX instructions (via LLVM backend)
  • Fallback: Automatic scalar code on unsupported platforms

Optimization Strategy

  1. Profiled first - Used memory and time profiling to identify bottlenecks
  2. Targeted hot paths - Focused on FFT and TLWE operations (>95% of time)
  3. Maintained correctness - All optimizations verified with test suite
  4. Zero allocations - Confirmed no heap allocations in critical path

Testing & Verification

Test Coverage

  • All existing tests pass
  • Fixed 3 test memory management bugs
  • Verified correctness of SIMD implementations
  • Benchmark results reproducible

CI/CD

  • Fixed formatting issues
  • Fixed test compilation errors
  • Build system cleaned up
  • All checks passing

Comparison with Rust Implementation

Implementation Per Gate Total (80 gates) Relative
Rust (release) 14.89 ms 1191 ms 1.0x
Zig v0.1.0 39.20 ms 3107 ms 2.62x
Zig v0.1.1 37.31 ms 2985 ms 2.51x

Progress: Closed the gap by 4.2% (from 2.62x to 2.51x)

Future Optimization Opportunities

Based on our analysis, remaining performance gap is primarily due to:

  1. FFT Algorithm (Highest Priority)

    • Current: Simple radix-2 FFT
    • Rust: Specialized algorithms (Split-Radix, Radix-4)
    • Potential: 20-30% improvement
  2. Butterfly Operations (High Priority)

    • Vectorize core FFT butterfly operations
    • Pre-compute twiddle factors
    • Potential: 10-20% improvement
  3. Cache Optimization (Medium Priority)

    • Memory layout optimization
    • Cache-aware blocking
    • Potential: 5-10% improvement

See NEXT_STEPS.md for detailed optimization roadmap.

Breaking Changes

Renamed APIs

  • parallel.rayon_impl → parallel.thread_pool
  • parallel.RayonRailgun → parallel.ThreadPool
  • parallel.rayonRailgun() → parallel.threadPool()

Migration: Simple find-and-replace if you're using the parallel API directly.

Build Changes

  • Removed zig build bootstrap-profile step (temporary profiling tool)
  • All other build steps remain unchanged

Compatibility

  • Zig Version: 0.14.0+ (tested on 0.15.1)
  • Platforms: macOS (ARM64), Linux (x86_64, ARM64)
  • Dependencies: No external dependencies (stdlib only)

Acknowledgments

This release focused on methodical performance optimization through:

  • Careful profiling and measurement
  • Evidence-based optimization decisions
  • Comprehensive documentation of findings
  • Maintaining code quality and correctness

The 4.8% performance improvement demonstrates that while SIMD helps, the remaining gap with Rust is primarily algorithmic - a solid foundation for future work.


Installation

# Clone the repository
git clone https://github.com/yourusername/zig-tfhe.git
cd zig-tfhe

# Build and test
zig build test
zig build add_two_numbers -Doptimize=ReleaseFast

Quick Start

const tfhe = @import("main");

// Generate keys
const secret_key = tfhe.key.SecretKey.new();
var cloud_key = try tfhe.key.CloudKey.new(allocator, &secret_key);
defer cloud_key.deinit(allocator);

// Encrypt
const ct1 = try tfhe.utils.Ciphertext.encryptBool(true, alpha, &secret_key.key_lv0);
const ct2 = try tfhe.utils.Ciphertext.encryptBool(false, alpha, &secret_key.key_lv0);

// Compute homomorphically (now 4.8% faster!)
const result = try tfhe.gates.nand(&ct1, &ct2, &cloud_key);

Full Changelog: https://github.com/yourusername/zig-tfhe/compare/v0.1.0...v0.1.1

What's Changed

  • Feat/optimized hotpath by @10d9e in #4

Full Changelog: v0.1.0...v0.1.1