Overview
bun-scikit achieves superior performance through native Zig kernels that accelerate compute-intensive training loops. Prebuilt binaries are included in the npm package for seamless installation.No additional build steps or permissions (
bun pm trust) are required for standard usage. Native binaries work out-of-the-box on supported platforms.Prebuilt Binary Support
The npm package includes prebuilt native binaries for:| Platform | Architecture | Binary Format | Status |
|---|---|---|---|
| Linux | x64 | .so | Available |
| Windows | x64 | .dll | Available |
| macOS | x64/ARM | .dylib | Not yet published |
Accelerated Models
The following models use native Zig kernels by default:LinearRegression
fit()- Native matrix operations (normal equation solver)predict()- Optimized linear algebra
LogisticRegression
fit()- Native gradient descent implementationpredict()- Fast sigmoid and classificationpredictProba()- Optimized probability computation
DecisionTreeClassifier
fit()- Native tree building (Gini/entropy calculations)predict()- Fast tree traversal
Set
BUN_SCIKIT_TREE_BACKEND=js to use the optimized JavaScript fallback instead of native Zig.RandomForestClassifier
fit()- Parallel tree building with native codepredict()- Fast ensemble prediction aggregation
Native ABI Contract
bun-scikit defines a stable ABI (Application Binary Interface) between JavaScript and native Zig code.ABI Version
Status Codes
Native functions return standardized status codes:Available Status Codes
Available Status Codes
bun_scikit_status_ok()- Operation succeededbun_scikit_status_invalid_handle()- Invalid model handlebun_scikit_status_invalid_shape()- Tensor shape mismatchbun_scikit_status_allocation_failed()- Memory allocation errorbun_scikit_status_fit_failed()- Training failedbun_scikit_status_symbol_unavailable()- Missing native symbol
Handle Lifecycle
Native models use opaque handles represented asBigInt in JavaScript:
Memory Ownership
Clear ownership rules prevent memory safety issues:- Input tensors (
x,y): Caller-owned, never freed by native code - Output tensors (
out): Caller-owned, preallocated to required size - Model state: Zig-owned, released via
*_model_destroy()
Tensor Layout
All tensors use standardized layouts:Tensor Format Requirements
Tensor Format Requirements
Feature Matrix (
X):- Type:
Float64Array - Layout: Row-major contiguous
- Shape:
[n_samples, n_features]
y):- Type:
Float64Array - Layout: Contiguous
- Shape:
[n_samples]
y):- Tree/Forest:
Uint16Array(encoded class indices, max 256 classes) - Logistic:
Uint8Array(binary labels) - Layout: Contiguous
Runtime Bridges
bun-scikit supports two bridge mechanisms for native code:Node-API Bridge (Preferred)
Stable C API for Node.js/Bun addon modules:- Implementation:
src/native/node-addon/bun_scikit_addon.cpp - Benefits: Stable ABI, cross-runtime compatibility
- Auto-detected when available
Bun FFI Bridge
Direct FFI usingbun:ffi:
- Implementation:
src/native/zigKernels.ts - Benefits: Lower overhead, Bun-specific optimizations
- Fallback when Node-API unavailable
The runtime automatically selects the best available bridge. You typically don’t need to choose manually.
Environment Variables
Customize native runtime behavior with environment variables:Bridge Selection
Backend Control
When to Use Environment Variables
When to Use Environment Variables
Force JS backend:
- Debugging numerical differences
- Platforms without prebuilt binaries
- Development without building native code
- Testing local builds
- Non-standard installation directories
- CI/CD with custom artifact locations
- Performance testing different bridges
- Debugging native code issues
Building Native Kernels
Build Zig Libraries
- Linux:
dist/native/bun_scikit_kernels.so - macOS:
dist/native/bun_scikit_kernels.dylib - Windows:
dist/native/bun_scikit_kernels.dll
Build Node-API Addon
Verifying Native Acceleration
Check if native kernels are active:If native kernels are unavailable, models will throw an error during
fit() asking you to run bun run native:build.Troubleshooting
Native library not found
Native library not found
Problem:
fit() throws error about missing native library.Solutions:- Install package:
bun add bun-scikit(includes prebuilt binaries) - Build locally:
bun run native:build - Check platform support (macOS requires local build)
- Verify environment variables aren’t overriding paths
ABI version mismatch
ABI version mismatch
Problem: Error about incompatible ABI version.Solutions:
- Update package:
bun update bun-scikit - Rebuild native kernels:
bun run native:build - Clear old builds:
rm -rf dist/native && bun run native:build
Performance not as expected
Performance not as expected
Problem: Not seeing advertised speedups.Solutions:
- Verify native backend: Check
fitBackend_property - Try different backends:
BUN_SCIKIT_TREE_BACKEND=zigvsjs - Check dataset size (small data may have JS overhead)
- Run benchmarks:
bun run benchto compare with baseline
Next Steps
Benchmarks
See detailed performance comparisons
Optimization Tips
Learn how to maximize performance