tale-ndjson 0.2.1

A tail-compatible tool for pretty-printing ndjson files, especially logs.
Documentation
# FileChunk Phase 2: Adaptive Sizing & Memory Management TODO

## Overview
This document tracks the implementation of Phase 2 enhancements to the FileChunk architecture, focusing on adaptive chunk sizing and intelligent memory management.

## Current Status
✅ **Phase 1 Complete**: Basic chunked processing with fixed sizing  
🚀 **Phase 2 In Progress**: Adaptive sizing and memory management

---

## Week 1: Core Infrastructure

### 1. Design Adaptive Strategy Interfaces ⏳
**Priority**: High | **Effort**: Medium  
**Files**: `src/readers/chunked/mod.rs`, `src/readers/chunked/adaptive.rs`

- [ ] Define `ChunkStrategy` trait with methods: `initial_chunk_size()`, `adapt_size()`, `should_adapt()`
- [ ] Create `ChunkMetrics` struct: processing_speed, memory_usage, io_wait_time, lines_per_chunk
- [ ] Design `AdaptationConfig` with thresholds and limits
- [ ] Define strategy types: `Static`, `Adaptive`, `MemoryConstrained`

**Acceptance Criteria**: Clean trait design, no breaking changes to existing code

### 2. Implement ChunkMetrics Collection ⏳
**Priority**: High | **Effort**: Medium  
**Files**: `src/readers/chunked/metrics.rs`

- [ ] Create `ChunkMetrics` with timing measurements
- [ ] Add `MetricsCollector` for aggregating across chunks
- [ ] Implement processing speed calculation (bytes/sec, lines/sec)
- [ ] Add memory usage tracking per chunk
- [ ] Create moving average calculations for smooth adaptation

**Acceptance Criteria**: Accurate performance metrics, minimal overhead

### 3. Basic AdaptiveChunkStrategy ⏳
**Priority**: High | **Effort**: Large  
**Files**: `src/readers/chunked/adaptive.rs`

- [ ] Implement `StaticChunkStrategy` (current behavior)
- [ ] Create `AdaptiveChunkStrategy` with feedback loop
- [ ] Add chunk size adjustment algorithm (exponential backoff/ramp-up)
- [ ] Implement memory pressure response (size reduction)
- [ ] Add configurable adaptation parameters (sensitivity, limits)

**Acceptance Criteria**: Smooth size transitions, handles edge cases

### 4. Memory Pressure Detection ⏳
**Priority**: Medium | **Effort**: Medium  
**Files**: `src/readers/chunked/memory.rs`

- [ ] Implement system memory detection (cross-platform)
- [ ] Create `MemoryPressure` enum: None, Low, Medium, High, Critical
- [ ] Add memory usage monitoring for tale process
- [ ] Create pressure-based adaptation triggers
- [ ] Add configurable memory thresholds

**Acceptance Criteria**: Reliable memory detection, responsive to pressure

---

## Week 2: Integration & Polish

### 5. MemoryManager Implementation ⏳
**Priority**: Medium | **Effort**: Large  
**Files**: `src/readers/chunked/memory.rs`

- [ ] Create centralized memory tracking across all chunks
- [ ] Implement memory budget allocation
- [ ] Add chunk lifecycle management
- [ ] Create memory leak detection and cleanup
- [ ] Add memory usage reporting/debugging

**Acceptance Criteria**: Memory stays within bounds, no leaks

### 6. Chunk Pooling System ⏳
**Priority**: Low | **Effort**: Medium  
**Files**: `src/readers/chunked/pool.rs`

- [ ] Design `ChunkPool` for Vec<u8> recycling
- [ ] Implement size-based pooling (small/medium/large buckets)
- [ ] Add pool statistics and monitoring
- [ ] Create pool cleanup and size limits
- [ ] Integrate with ChunkedFileReader

**Acceptance Criteria**: Reduced allocations, bounded pool size

### 7. Integration with ChunkedFileReader ⏳
**Priority**: High | **Effort**: Medium  
**Files**: `src/readers/chunked.rs`

- [ ] Add strategy field to ChunkedFileReader
- [ ] Integrate metrics collection in read_chunk()
- [ ] Add adaptation calls after chunk processing
- [ ] Update configuration to use strategies
- [ ] Maintain backward compatibility

**Acceptance Criteria**: Seamless integration, no performance regression

### 8. CLI Configuration Options ⏳
**Priority**: Medium | **Effort**: Small  
**Files**: `src/main.rs`, `src/config.rs`

- [ ] Add `--adaptive-chunks` flag (enable adaptive strategy)
- [ ] Add `--max-memory` flag (explicit memory limit)
- [ ] Add `--chunk-size` flag (manual override)
- [ ] Update help text and documentation
- [ ] Add validation for conflicting options

**Acceptance Criteria**: Intuitive CLI, good help text

---

## Week 3: Testing & Documentation

### 9. Comprehensive Testing ⏳
**Priority**: High | **Effort**: Large  
**Files**: `src/readers/chunked/tests.rs`, `src/readers/mod.rs`

- [ ] Unit tests for AdaptiveChunkStrategy
- [ ] Memory pressure simulation tests
- [ ] Performance regression tests (benchmarks)
- [ ] Integration tests with various file sizes
- [ ] Edge case tests (empty files, single lines, etc.)
- [ ] Memory leak tests

**Acceptance Criteria**: High test coverage, all tests pass

### 10. Performance Benchmarking ⏳
**Priority**: Medium | **Effort**: Medium  
**Files**: `benches/chunk_performance.rs`

- [ ] Benchmark static vs adaptive strategies
- [ ] Memory usage benchmarks
- [ ] Large file processing benchmarks
- [ ] Compare with Phase 1 performance
- [ ] Create performance regression tests

**Acceptance Criteria**: Performance improvements demonstrated

### 11. Error Handling & Robustness ⏳
**Priority**: Medium | **Effort**: Small  
**Files**: Various

- [ ] Add proper error handling for adaptation failures
- [ ] Implement graceful fallback to static sizing
- [ ] Add logging for adaptation decisions (debug mode)
- [ ] Handle edge cases (OOM, disk full, etc.)
- [ ] Add configuration validation

**Acceptance Criteria**: Robust error handling, good diagnostics

### 12. Documentation Updates ⏳
**Priority**: Low | **Effort**: Medium  
**Files**: `CLAUDE.md`, `README.md`, code comments

- [ ] Update CLAUDE.md with Phase 2 architecture
- [ ] Document new CLI options and usage patterns
- [ ] Add performance tuning guide
- [ ] Update code documentation
- [ ] Create usage examples

**Acceptance Criteria**: Clear documentation, good examples

---

## Success Metrics

### Performance Goals
- [ ] 20%+ speed improvement on files >100MB
- [ ] Memory usage stays within configured bounds
- [ ] No performance regression on small files (<1MB)

### Reliability Goals  
- [ ] Graceful handling of memory pressure
- [ ] No crashes or memory leaks
- [ ] Backward compatibility maintained

### Usability Goals
- [ ] Sensible defaults (no configuration required)
- [ ] Clear error messages and diagnostics
- [ ] Good documentation and examples

---

## Implementation Notes

### Current Constants
```rust
READ_BUFFER_SIZE = 8192 bytes (8KB)
MEMORY_LIMIT_BYTES = 10MB
```

### File Organization
```
src/readers/chunked/
├── mod.rs           # Re-exports, main ChunkedFileReader
├── adaptive.rs      # AdaptiveChunkStrategy implementation  
├── metrics.rs       # Performance metrics collection
├── memory.rs        # MemoryManager and pressure detection
├── pool.rs          # Chunk pooling system
└── tests.rs         # Comprehensive test suite
```

### Dependencies to Add
- Consider `sysinfo` for system memory detection
- Consider `metrics` crate for telemetry (optional)

---

## Risk Mitigation

### Performance Risks
- Feature flags to disable adaptive behavior
- Fallback to Phase 1 behavior on failures
- Performance regression tests

### Memory Risks  
- Hard memory limits with OOM protection
- Chunk pool size limits
- Memory leak detection in tests

### Complexity Risks
- Start with simple algorithms
- Comprehensive logging for debugging
- Incremental rollout of features

---

*Last Updated: 2025-01-08*
*Next Review: After Week 1 tasks complete*