Performance Optimizations v0.2.1#5
Merged
antarr merged 9 commits intoJan 20, 2026
Merged
Conversation
Optimized the `get_overlap_from_string` method in `src/text-splitter/splitter.cr` to scan from the end of the string to find the last N words, instead of splitting the entire text into an array. This reduces memory allocation and improves performance for large texts with small overlaps. * Implemented `Char::Reader` based reverse scan to find word boundaries. * Used `byte_slice` to extract only the relevant substring. * Preserved whitespace normalization behavior.
Deleted the unused 'found_limit' variable from the word limit logic in the text splitter module to clean up the code.
Introduces two new benchmark scripts: one for measuring full text splitting performance in real-world scenarios, and another for comparing memory and speed of old vs. optimized overlap calculation methods. These benchmarks demonstrate improved scalability and significant memory savings for large documents.
Simplified benchmarking logic by removing unnecessary variable assignments and using direct calls to split_text. Fixed minor formatting issues and improved output consistency in benchmark_full_split.cr. Updated comment formatting in benchmark_overlap.cr for consistency.
Eliminates unnecessary intermediate string variables in both character and word chunking modes by directly appending sentence and punctuation. This reduces memory allocations and improves performance, especially in character mode, while maintaining identical behavior.
Release includes performance optimizations: - Optimized overlap calculation (97-99% memory reduction) - Eliminated unnecessary string allocations in hot loops (31% memory reduction, 1.2x speedup) - Added comprehensive benchmarks and documentation Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This pull request implements performance optimizations for text chunking operations in version 0.2.1, focusing on reducing memory allocations while maintaining backward compatibility.
Changes:
- Optimized overlap calculation using backward character scanning instead of splitting entire text into word arrays (97-99% memory reduction)
- Eliminated intermediate string variables in character and word mode splitting by using direct String::Builder append operations (31% memory savings and 1.2x speedup in character mode)
- Added comprehensive benchmark documentation demonstrating performance improvements
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/text-splitter/splitter.cr | Implements core performance optimizations: eliminates sentence_with_punct intermediate variable in both character and word modes; replaces word array allocation in overlap calculation with backward character scanning |
| shard.yml | Updates version from 0.2.0 to 0.2.1 to reflect patch-level performance improvements |
| benchmarks/STRING_ALLOCATION_OPTIMIZATION.md | Documents string allocation optimization with benchmark results showing 31% memory reduction and 1.2x speedup in character mode |
| benchmarks/README.md | Provides overview of all benchmarks and instructions for running performance tests |
| benchmarks/BENCHMARK_RESULTS.md | Documents overlap calculation optimization with detailed benchmark results showing 97-99% memory reduction |
| benchmarks/.gitignore | Excludes benchmark code files from version control while preserving documentation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Introduces LAZY_ITERATOR_RESULTS.md with detailed benchmark results for the new state machine-based lazy iterator. Updates benchmarks/README.md to document lazy iterator performance and comparison. Refactors ChunkIterator in splitter.cr to use a true lazy, state machine approach for chunk generation, improving performance and memory efficiency.
Clarified the time improvement metric to indicate the new method is negligibly slower (1.02x) instead of ~same (0.98x).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Performance Optimizations v0.2.1
Summary
Major performance improvements for text chunking with no breaking changes.
Key Improvements
Changes
Testing
Benchmarks
10,000 word text (10,000 iterations):