kiru ⚡🗡️
Cut through text at the speed of light
The fastest text chunking library for RAG applications. Available for both Rust and Python.
What is kiru?
kiru is a high-performance text chunking library designed for modern RAG (Retrieval-Augmented Generation) systems. When you need to split millions of documents for vector databases or process streaming data in real-time, kiru delivers unmatched speed without sacrificing correctness.
Key Features
- ⚡ Blazing Fast (Python): 1000+ MB/s throughput for bytes, 300+ MB/s for characters
- 🎯 UTF-8 Safe: Never breaks multi-byte characters or emoji
- 💾 Memory Efficient: Stream gigabyte files with constant memory usage
- 🚀 Parallel Processing: Utilize all CPU cores automatically
- 🔌 Multiple Sources: Files, URLs, strings, and glob patterns
- 🛠️ Flexible Strategies: Chunk by bytes or characters
- 🦀 Rust Core: Rust performance and memory safety
- 🐍 Python Bindings: Pythonic API for ease of use
Performance
Benchmarked on 1MB text file, 1MB chunks, 1KB overlap:
| Implementation | Strategy | Source | Time (ms) | Memory (MB) | Throughput (MB/s) |
|---|---|---|---|---|---|
| kiru (Rust) | bytes | string | 0.23 | - | 4,370 |
| kiru (Python) | bytes | string | 0.71 | 2.9 | 1,408 |
| kiru (Python) | chars | string | 3.13 | 2.9 | 319 |
| LangChain | chars | string | 2,982 | 18.6 | 0.34 |
kiru is 4,000x faster than LangChain for byte chunking and 940x faster for character chunking!
Key insights:
- Rust native performance: Up to 4,370 MB/s for byte chunking
- Python bindings overhead: Still 1,400+ MB/s, beating all pure Python alternatives
- Character-aware chunking: 300+ MB/s while respecting grapheme boundaries
- Memory efficient: Uses 6x less memory than LangChain
Quick Start
Python 🐍
# Create a chunker
=
# Chunk text
=
# Chunk files in parallel
=
Rust 🦀
Add to your Cargo.toml:
[]
= "0.1"
use ;
// Create a chunker
let chunker = new?;
// Chunk text
let chunks: = chunker
.chunk_string
.collect;
// Stream large files
use ;
let stream = from_source?;
for chunk in chunker.chunk_stream
Use Cases
Building RAG Systems
# Perfect for vector database ingestion
= # Tuned for embedding models
=
=
=
Real-time Processing
# Stream processing without memory overhead
# Each chunk generated on-demand
Parallel Document Processing
// Process hundreds of documents concurrently
use ;
let chunker = by_bytes;
let sources = vec!;
let chunks = chunker.on_sources_par_stream?;
Chunking Strategies
Bytes Chunking
- Splits on byte boundaries while respecting UTF-8
- Fastest performance (1000+ MB/s in Rust, 1400+ MB/s in Python)
- Ideal for token-limited models and consistent memory usage
Characters Chunking
- Splits on character (grapheme) boundaries
- Ensures exact character counts regardless of byte representation
- Perfect for character-limited APIs (300+ MB/s in Python)
API Reference
Python API
Creating Chunkers
# Byte-based chunking
=
# Character-based chunking
=
Input Sources
# Single string
=
# Single file
=
# HTTP/HTTPS URL
=
# Multiple sources (serial)
=
=
# Multiple sources (parallel)
=
# Or iterate lazily
Source Prefixes
file://path/to/file.txt- Local fileshttp://example.comorhttps://example.com- URLstext://Inline text content- Raw text stringsglob://*.md- Glob patterns- No prefix - Treated as raw text
Rust API
Creating Chunkers
use ;
// Byte-based chunking
let chunker = new?;
// Character-based chunking
let chunker = new?;
Basic Usage
use Chunker;
// Chunk a string
let chunks: = chunker
.chunk_string
.collect;
// Stream a file
use ;
let stream = from_source?;
for chunk in chunker.chunk_stream
Advanced Usage
use ;
// Create chunker with builder pattern
let chunker = by_bytes;
// Single source
let chunks = chunker.on_source?;
// Multiple sources (serial)
let sources = vec!;
let chunks = chunker.on_sources?;
// Multiple sources (parallel) - returns Vec
let chunks: = chunker.on_sources_par?;
// Multiple sources (parallel streaming) - returns iterator
let chunks = chunker.on_sources_par_stream?;
for chunk in chunks
// Using glob patterns
let sources = vec!;
let flattened = into_flattened_sources?;
Architecture
┌─────────────────────────────────────────┐
│ Application Layer │
│ (Python or Rust Application) │
├─────────────────────────────────────────┤
│ kiru-py (PyO3 Bindings) │
│ [Python only] │
├─────────────────────────────────────────┤
│ kiru-core (Rust Library) │
│ │
│ ┌──────────┬───────────┐ │
│ │ Chunkers │ Streaming │ │
│ │ Engine │ Engine │ │
│ └──────────┴───────────┘ │
└─────────────────────────────────────────┘
Project Structure
kiru/
├── README.md # This file (shared documentation)
├── kiru-core/ # Rust implementation
│ ├── src/ # Core chunking algorithms
│ │ ├── bytes_chunker.rs
│ │ ├── characters_chunker.rs
│ │ ├── chunker.rs # Builder pattern & parallel processing
│ │ └── stream.rs # File/HTTP streaming
│ ├── benches/ # Criterion benchmarks
│ └── tests/ # Property-based tests
├── kiru-py/ # Python bindings (PyO3)
│ ├── src/lib.rs # Python wrapper
│ └── python/ # Python tests & benchmarks
└── utils/ # Version management scripts
Streaming & Memory Efficiency
kiru's killer feature: true streaming with constant memory usage.
Unlike traditional chunkers that load entire files into memory, kiru processes data as it arrives using an intelligent buffering system. This means you can chunk gigabyte-sized files with minimal RAM usage.
How Streaming Works
File/HTTP Source → Read Blocks (8KB) → UTF-8 Buffer → Chunk Iterator → Your Code
↓ ↓
As needed Constant size
Key advantages:
- Constant Memory: Process 10GB files with ~10MB RAM
- Immediate Results: First chunks available instantly, no waiting for full file load
- Works Everywhere: Local files, HTTP/HTTPS streams, any data source
- UTF-8 Safe: Buffer maintains character boundaries automatically
Python Examples
=
# ⚡ Stream a 10GB file - uses only ~10MB RAM
# Process chunk immediately as it arrives
# No waiting, no memory explosion!
# ⚡ Stream from HTTP - process as data downloads
# Chunks ready while download continues
# ⚡ Stream multiple sources in parallel
=
# All sources stream in parallel
# Memory stays constant regardless of file sizes
Rust Examples
use ;
let chunker = new?;
// ⚡ Stream a massive file with constant memory
let stream = from_source?;
for chunk in chunker.chunk_stream
// ⚡ Stream from HTTP as data arrives
let stream = from_source?;
for chunk in chunker.chunk_stream
Memory Comparison
Processing a 1GB file with 4KB chunks:
| Library | Memory Usage | Loads Full File? | Streaming? |
|---|---|---|---|
| kiru | ~10 MB | ❌ No | ✅ Yes |
| LangChain | 1000+ MB | ✅ Yes | ❌ No |
| tiktoken | 1000+ MB | ✅ Yes | ❌ No |
Result: kiru uses 100x less memory while being 4,000x faster!
Development
Setup
# Clone repository
# Run all tests
# Run Rust benchmarks
# Build Python package
# Run Python tests
# Run Python benchmarks
Running Benchmarks
# Rust benchmarks
# Python benchmarks
Performance Tips
- Use byte chunking for maximum throughput (1000+ MB/s)
- Use character chunking when exact character counts matter (300+ MB/s)
- Enable parallel processing with
on_sources_par()for multiple files - Tune chunk size based on your embedding model's context window
- Adjust overlap to balance context preservation and storage
- Stream large files to maintain constant memory usage
Why "kiru"?
"Kiru" (切る) is Japanese for "to cut" - reflecting the library's purpose of cutting text into chunks at lightning speed ⚡🗡️
Contributing
We welcome contributions! Please check out our Contributing Guide for guidelines.
License
MIT License - see LICENSE for details.
Credits
Built with:
- PyO3 - Rust bindings for Python
- Rayon - Data parallelism for Rust
- maturin - Build and publish Rust Python extensions
Ready to cut through text at the speed of light?
- 🐍 Python:
pip install kiru - 🦀 Rust: Add
kiru = "0.1"to Cargo.toml
Get started with PyPI | Crates.io | Documentation