Skip to content

Performance Optimizations v0.2.1#5

Merged
antarr merged 9 commits into
mainfrom
perf-optimize-overlap-calculation-574547704945957741
Jan 20, 2026
Merged

Performance Optimizations v0.2.1#5
antarr merged 9 commits into
mainfrom
perf-optimize-overlap-calculation-574547704945957741

Conversation

@antarr

@antarr antarr commented Jan 20, 2026

Copy link
Copy Markdown
Contributor

Performance Optimizations v0.2.1

Summary

Major performance improvements for text chunking with no breaking changes.

Key Improvements

  • 97-99% memory reduction in overlap calculation
  • 31% memory savings + 1.2x speedup in character-based splitting
  • Eliminated unnecessary string allocations in hot loops

Changes

  1. Optimized overlap calculation - backward char scanning
  2. Eliminated temp strings - direct String::Builder append
  3. Added benchmarks - comprehensive performance analysis

Testing

  • ✅ All 25 tests pass
  • ✅ Ameba linter: 0 failures
  • ✅ Backward compatible

Benchmarks

10,000 word text (10,000 iterations):

  • Old overlap: 6,748 MB → New: 12 MB (99.8% reduction)
  • Old char mode: 244 MB → New: 168 MB (31% reduction)

google-labs-jules Bot and others added 6 commits January 20, 2026 02:09
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>
@antarr antarr requested a review from Copilot January 20, 2026 03:19
@antarr antarr self-assigned this Jan 20, 2026
@antarr antarr added the enhancement New feature or request label Jan 20, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread benchmarks/.gitignore
Comment thread src/text-splitter/splitter.cr
Comment thread benchmarks/STRING_ALLOCATION_OPTIMIZATION.md Outdated
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).
@antarr antarr merged commit ec38135 into main Jan 20, 2026
4 checks passed
@antarr antarr deleted the perf-optimize-overlap-calculation-574547704945957741 branch January 20, 2026 03:46
@antarr antarr requested a review from alchemist-guy January 20, 2026 03:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants