StringZilla
repository·main·Indexed 25 days ago
https://github.com/ashvardanian/stringzillaA high-performance string processing library (version 5.0.5) that leverages SIMD, SWAR, and GPGPU to accelerate searching, hashing, sorting, fingerprinting, and fuzzy-matching. It provides native C/C++ implementations and bindings for .NET 8+ and Go 1.24+, featuring zero-copy operations on UTF-8 byte spans, Unicode case-folding, normalization, and specialized memory kernels for copy, move, fill, and lookup.
What's inside stringzilla
- StringZilla is a high-performance string processing library designed to accelerate exact and fuzzy matching, hashing, edit distances, sorting, segmentation, and random-string generation. It utilizes SIMD and SWAR instructions to provide significantly higher throughput than standard libraries like LibC, ICU, or NVIDIA's own libraries. It is designed to be portable, with backends for WebAssembly, RISC-V (RVV), PowerPC, and LoongArch.
Understand StringZilla architecture and functionality
mainStringZilla is a high-performance string operations library split into two layers:
- StringZilla: A single-header C library and C++ wrapper for high-performance string operations (search, hashing, sorting, etc.).
- StringZillas: Parallel CPU/GPU backends used for large-batch operations and accelerators (similarity scoring, fingerprinting).
Both layers are designed for portability across architectures (little-endian/big-endian, 32/64-bit), operating systems, and encodings (ASCII/UTF-8).
Iterate UTF-8 words using UAX-29 word boundary rules
mainThe
sz_utf8_wordbreaksoperation allows you to walk a UTF-8 string and yield individual words according to Unicode UAX-29 word-boundary rules. This ensures that complex tokens like "don't" or CJK (Chinese, Japanese, Korean) character runs are split according to human-readable expectations rather than simply splitting on raw whitespace.Performance is optimized via a dispatcher that automatically selects the fastest available SIMD backend for your CPU, including
haswellandicelakeon x86 architectures.Levenshtein Edit Distance
mainStringZilla implements Levenshtein distance using an anti-diagonal evaluation approach rather than the traditional row-by-row method. This allows for better vectorization and parallel computation of cells within a diagonal, as they are independent.
This approach is suitable for:
- Approximate string matching
- Spell-checking
- Bioinformatics
- Weighted edit-distances (where substitution costs vary)
Understand StringZilla Fingerprinting and MinHash
mainStringZilla's fingerprinting engine generates fixed-width MinHash signatures for strings. It works by rolling multiple hash windows of varying widths over the text and retaining the minimum hash value per dimension.
Key Use Cases
- Near-duplicate detection: Identifying documents that are similar even after small edits.
- Multi-pattern search: Searching across web-scale corpora.
Performance Characteristics
- Efficiency: Performance is measured in both MB/s (text consumed) and hashes/s (rolling hashes emitted). One operation is defined as one emitted hash dimension.
- Scaling:
- CPU: Throughput (MB/s) remains relatively flat across different document lengths, but the emitted hash-rate (hashes/s) decreases as document length increases (fewer, longer documents result in fewer total operations for the same byte count).
- GPU: Throughput and hash-rate scale up with document length as longer documents better utilize CUDA warps.
Understand StringZilla comparison kernels (`sz_equal` and `sz_order`)
mainStringZilla provides high-performance comparison operations viasz_equal(equality check) andsz_order(lexicographic ordering). These operations use a dispatcher that automatically selects the fastest available SIMD backend for your specific CPU architecture (e.g.,haswell,skylake, oricelakeon x86).Use `sz_utf8_linebreaks` for Unicode-compliant line wrapping
mainThe
sz_utf8_linebreaksoperation identifies line-break opportunities in a UTF-8 string according to the Unicode UAX-14 rules. Instead of splitting solely on raw spaces or hard newlines, this kernel yields positions where a text layout engine is permitted to wrap text.Performance is optimized via a dispatcher that selects the fastest available SIMD backend for the current CPU (e.g.,
haswelloricelakeon x86, or ARM-based backends likeNEONorSVE).Understand StringZilla C Library structure
mainStringZilla's C implementation is split into two main components to handle different execution models:
stringzilla/: Contains the single-threaded core ABI.stringzillas/: Contains the batch and parallel engines, including the C lowering of C++ and CUDA templates.
SIMD capabilities are determined during the build process via
probes/programs. These programs perform try-compilations and executeprobes/run_capabilities.cto detect whichSZ_USE_*SIMD tiers the toolchain can emit and the hardware can execute.Use StringZilla memory kernels for Copy, Move, Fill, and Lookup
mainStringZilla provides high-performance memory kernels for common operations. The library uses a dispatcher that automatically selects the fastest available SIMD backend for your CPU (e.g.,
haswell,skylake, oricelakeon x86).Available operations:
sz_copy: High-speed memory copying.sz_move: High-speed memory moving.sz_fill: High-speed memory filling.sz_lookup: High-speed memory lookup.
Iterate UTF-8 sentences using `sz_utf8_sentences`
mainThe
sz_utf8_sentencesoperation walks a UTF-8 string and yields each sentence according to the Unicode UAX-29 sentence-boundary rules. It is designed to distinguish real sentence terminators from abbreviation dots or decimal points, preventing incorrect breaks on every period.Performance is optimized via SIMD backends (including
haswellandicelakeon x86), with a dispatcher that automatically selects the fastest available backend for the running CPU.Understand StringZilla similarity engines and metrics
mainStringZilla provides similarity engines designed for scoring large collections of strings against each other (cross-product matrices), which is useful for fuzzy matching and bioinformatics alignment.
Supported Algorithms
- Levenshtein: Computes minimum-cost edit distance byte by byte.
- Levenshtein UTF-8: Computes edit distance codepoint by codepoint for correct multibyte text handling.
- Needleman-Wunsch: Maximizes a signed global alignment score end-to-end.
- Smith-Waterman: Finds the best-scoring local subsequence.
Performance Metric: GCUPS
Throughput is measured in GCUPS (billions of cell updates per second). One GCUP represents one billion dynamic-programming cell updates. This is the standard metric for alignment efficiency.
Install StringZilla for C and C++
mainStringZilla can be integrated as a header-only library or as a precompiled shared library.
Header-Only (Simplest)
Add the
include/directory to your search path and include the umbrella headers. This method selects the best SIMD backend at compile time based on your compiler flags.CMake Integration
Header-Only Mode
Use
FetchContentto pull the project and link againststringzilla::stringzilla_header.Precompiled Shared Library
To resolve the best backend at runtime (allowing a single binary to run optimally on different CPUs), enable
STRINGZILLA_BUILD_SHAREDand link againststringzilla::stringzilla_shared.# Header-Only include(FetchContent) FetchContent_Declare( stringzilla GIT_REPOSITORY https://github.com/ashvardanian/stringzilla.git GIT_TAG main) FetchContent_MakeAvailable(stringzilla) target_link_libraries(your_app PRIVATE stringzilla::stringzilla_header)# Precompiled Shared Library set(STRINGZILLA_BUILD_SHARED ON) FetchContent_MakeAvailable(stringzilla) target_link_libraries(your_app PRIVATE stringzilla::stringzilla_shared)