StringZilla

repository·main·Indexed 25 days ago

https://github.com/ashvardanian/stringzilla

A 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.

Tokens
46.9K
Snippets
134
Records
211
Agent score
85%

What's inside stringzilla

  1. Overview of StringZilla

    main
    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.
  2. Understand StringZilla architecture and functionality

    main

    StringZilla is a high-performance string operations library split into two layers:

    1. StringZilla: A single-header C library and C++ wrapper for high-performance string operations (search, hashing, sorting, etc.).
    2. 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).

  3. Iterate UTF-8 words using UAX-29 word boundary rules

    main

    The sz_utf8_wordbreaks operation 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 haswell and icelake on x86 architectures.

  4. Levenshtein Edit Distance

    main

    StringZilla 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)
  5. Understand StringZilla Fingerprinting and MinHash

    main

    StringZilla'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.
  6. Understand StringZilla comparison kernels (`sz_equal` and `sz_order`)

    main
    StringZilla provides high-performance comparison operations via sz_equal (equality check) and sz_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, or icelake on x86).
  7. Use `sz_utf8_linebreaks` for Unicode-compliant line wrapping

    main

    The sz_utf8_linebreaks operation 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., haswell or icelake on x86, or ARM-based backends like NEON or SVE).

  8. Understand StringZilla C Library structure

    main

    StringZilla'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 execute probes/run_capabilities.c to detect which SZ_USE_* SIMD tiers the toolchain can emit and the hardware can execute.

  9. Use StringZilla memory kernels for Copy, Move, Fill, and Lookup

    main

    StringZilla 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, or icelake on 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.
  10. Iterate UTF-8 sentences using `sz_utf8_sentences`

    main

    The sz_utf8_sentences operation 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 haswell and icelake on x86), with a dispatcher that automatically selects the fastest available backend for the running CPU.

  11. Understand StringZilla similarity engines and metrics

    main

    StringZilla 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.

  12. Install StringZilla for C and C++

    main

    StringZilla 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 FetchContent to pull the project and link against stringzilla::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_SHARED and link against stringzilla::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)