solana-pubkey-compare
High-performance Solana public key comparison using optimized BPF assembly.
Overview
This crate provides ultra-fast public key comparison for Solana blockchain programs, achieving significant performance improvements through hand-optimized BPF assembly. Perfect for performance-critical Solana programs where compute unit efficiency matters.
Performance
- Assembly implementation: 19 compute units on Solana BPF
- Standard comparison: 28 compute units on Solana BPF
- Improvement: ~32% reduction in compute units
Features
- ✅ Zero dependencies and
#[no_std]compatible - ✅ Hand-optimized BPF assembly for Solana runtime
- ✅ Automatic fallback to standard comparison for native testing
- ✅ Generic interface supporting any 32-byte key types
- ✅ Compile-time safety with Rust's type system
- ✅ Early exit optimization for maximum efficiency
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic Example
use fast_eq;
use Pubkey;
// Compare Solana Pubkeys
let key1 = new_unique;
let key2 = new_unique;
if fast_eq
// Works with any 32-byte types
let bytes1: = ;
let bytes2: = ;
assert!;
In Solana Programs
use ;
use fast_eq;
Performance-Critical Loops
use fast_eq;
How It Works
Assembly Implementation
The core of this crate is a hand-optimized BPF assembly function that:
- Loads 64-bit chunks: Uses
ldxdwto load 8 bytes at a time - Early exit: Uses conditional jumps (
jne) to exit immediately on first mismatch - Minimal instructions: Only 11-19 instructions depending on where difference is found
- Register-optimized: Uses minimal register pressure for maximum efficiency
Instruction Sequence
// Compare bytes 0-7
ldxdw r3, [r1+0] // Load first 8 bytes of key1
ldxdw r4, [r2+0] // Load first 8 bytes of key2
jne r3, r4, not_equal // Exit early if different
// Repeat for bytes 8-15, 16-23, 24-31...
// Return 1 if all equal, 0 if any different
Fallback Implementation
On non-Solana platforms, the function falls back to the standard PartialEq implementation for compatibility with testing and development workflows.
Benchmarks
Performance measurements on Solana BPF runtime:
| Operation | Standard PartialEq |
fast_eq |
Improvement |
|---|---|---|---|
| Equal keys | 28 CU | 19 CU | 32% faster |
| Different keys (first byte) | 28 CU | 11 CU | 61% faster |
| Different keys (last byte) | 28 CU | 19 CU | 32% faster |
CU = Compute Units
Type Requirements
The generic type T must implement:
AsRef<[u8]>- For accessing the underlying byte dataPartialEq- For the native fallback implementation
This includes:
solana_program::pubkey::Pubkey[u8; 32]Vec<u8>(if exactly 32 bytes)- Any custom types that dereference to 32-byte arrays
Safety
This function is completely safe to call. While it uses unsafe internally to interface with the assembly function, all safety invariants are maintained:
- References are valid for the duration of the call
- Data alignment is handled by the BPF runtime
- No memory is mutated - this is a pure comparison
- All bounds are compile-time verified
Development
Building
# Build for native (testing)
# Build for Solana BPF
Testing
# Run tests (uses fallback implementation)
# Test on Solana BPF runtime
Contributing
Contributions are welcome! Please ensure:
- All tests pass on both native and BPF targets
- Assembly changes are documented and benchmarked
- New features maintain the
#[no_std]compatibility
License
MIT License - see LICENSE file for details.
Credits
Created by Switchboard for high-performance Solana applications.