File size: 8,514 Bytes
29c3655
 
32e4125
29c3655
32e4125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29c3655
 
 
 
 
 
 
 
 
 
 
 
 
32e4125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29c3655
32e4125
 
 
 
 
 
29c3655
32e4125
29c3655
32e4125
 
 
 
 
29c3655
32e4125
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Contributing

Thanks for wanting to contribute! This repository uses a strict CI and formatting policy to keep code consistent, with special emphasis on memory-efficient development for cloud deployment.

## 🧠 Memory-Constrained Development Guidelines

This project is optimized for deployment on Render's free tier (512MB RAM limit). All contributions must consider memory usage as a primary constraint.

### Memory Development Principles

1. **Memory-First Design**: Consider memory impact of every code change
2. **Lazy Loading**: Initialize services only when needed
3. **Resource Cleanup**: Always clean up resources in finally blocks or context managers
4. **Memory Testing**: Test changes in memory-constrained environments
5. **Monitoring Integration**: Add memory tracking to new services

### Memory-Aware Code Guidelines

**βœ… DO - Memory Efficient Patterns:**

```python
# Use context managers for resource cleanup
from src.utils.memory_utils import MemoryManager

with MemoryManager() as mem:
    # Memory-intensive operations
    embeddings = process_large_dataset(data)
    # Automatic cleanup on exit

# Implement lazy loading for expensive services
@lru_cache(maxsize=1)
def get_expensive_service():
    return ExpensiveService()  # Only created once

# Use generators for large data processing
def process_documents(documents):
    for doc in documents:
        yield process_single_document(doc)  # Memory efficient iteration
```

**❌ DON'T - Memory Wasteful Patterns:**

```python
# Don't load all data into memory at once
all_embeddings = [embed(doc) for doc in all_documents]  # Memory spike

# Don't create multiple instances of expensive services
service1 = ExpensiveMLModel()
service2 = ExpensiveMLModel()  # Duplicates memory usage

# Don't keep large objects in global scope
GLOBAL_LARGE_DATA = load_entire_dataset()  # Always consumes memory
```

## πŸ› οΈ Recommended Local Setup

We recommend using `pyenv` + `venv` to create a reproducible development environment. A helper script `dev-setup.sh` is included to automate the steps:

```bash
# Run the helper script (default Python version can be overridden)
./dev-setup.sh 3.11.4
source venv/bin/activate

# Install pre-commit hooks
pip install -r dev-requirements.txt
pre-commit install
```

### Memory-Constrained Testing Environment

**Test your changes in a memory-limited environment:**

```bash
# Limit Python process memory to simulate Render constraints (macOS/Linux)
ulimit -v 524288  # 512MB limit in KB

# Run your development server
flask run

# Test memory usage
curl http://localhost:5000/health | jq '.memory_usage_mb'
```

## πŸ§ͺ Development Workflow

### Before Opening a PR

**Required Checks:**

1. **Code Quality**: `make format` and `make ci-check`
2. **Test Suite**: `pytest` (all 138 tests must pass)
3. **Pre-commit**: `pre-commit run --all-files`
4. **Memory Testing**: Verify memory usage stays within limits

**Memory-Specific Testing:**

```bash
# Test memory usage during development
python -c "
from src.app_factory import create_app
from src.utils.memory_utils import MemoryManager
app = create_app()
with app.app_context():
    mem = MemoryManager()
    print(f'App startup memory: {mem.get_memory_usage():.1f}MB')
    # Should be ~50MB or less
"

# Test first request memory loading
curl -X POST http://localhost:5000/chat -H "Content-Type: application/json" \
  -d '{"message": "test"}' && \
curl http://localhost:5000/health | jq '.memory_usage_mb'
# Should be ~200MB or less
```

### Memory Optimization Development Process

1. **Profile Before Changes**: Measure baseline memory usage
2. **Implement Changes**: Follow memory-efficient patterns
3. **Profile After Changes**: Verify memory impact is acceptable
4. **Load Test**: Validate performance under memory constraints
5. **Document Changes**: Update memory-related documentation

### New Feature Development Guidelines

**When Adding New ML Services:**

```python
# Example: Adding a new ML service with memory management
class NewMLService:
    def __init__(self):
        self._model = None  # Lazy loading

    @property
    def model(self):
        if self._model is None:
            with MemoryManager() as mem:
                logger.info(f"Loading model, current memory: {mem.get_memory_usage():.1f}MB")
                self._model = load_expensive_model()
                logger.info(f"Model loaded, current memory: {mem.get_memory_usage():.1f}MB")
        return self._model

    def process(self, data):
        # Use the lazily-loaded model
        return self.model.predict(data)
```

**Memory Testing for New Features:**

```python
# Add to your test file
def test_new_feature_memory_usage():
    """Test that new feature doesn't exceed memory limits"""
    import psutil
    import os

    # Measure before
    process = psutil.Process(os.getpid())
    memory_before = process.memory_info().rss / 1024 / 1024  # MB

    # Execute new feature
    result = your_new_feature()

    # Measure after
    memory_after = process.memory_info().rss / 1024 / 1024  # MB
    memory_increase = memory_after - memory_before

    # Assert memory increase is reasonable
    assert memory_increase < 50, f"Memory increase {memory_increase:.1f}MB exceeds 50MB limit"
    assert memory_after < 300, f"Total memory {memory_after:.1f}MB exceeds 300MB limit"
```

## πŸ”§ CI Expectations

**Automated Checks:**

- **Code Quality**: Pre-commit hooks (black, isort, flake8)
- **Test Suite**: All 138 tests must pass
- **Memory Validation**: Memory usage checks during CI
- **Performance Regression**: Response time validation
- **Python Version**: Enforces Python >=3.10

**Memory-Specific CI Checks:**

```bash
# CI pipeline includes memory validation
pytest tests/test_memory_constraints.py  # Memory usage tests
pytest tests/test_performance.py         # Response time validation
pytest tests/test_resource_cleanup.py    # Resource leak detection
```

## πŸš€ Deployment Considerations

### Render Platform Constraints

**Resource Limits:**

- **RAM**: 512MB total (200MB steady state, 312MB headroom)
- **CPU**: 0.1 vCPU (I/O bound workload)
- **Storage**: 1GB (current usage ~100MB)
- **Network**: Unmetered (external API calls)

**Performance Requirements:**

- **Startup Time**: <30 seconds (lazy loading)
- **Response Time**: <3 seconds for chat requests
- **Memory Stability**: No memory leaks over 24+ hours
- **Concurrent Users**: Support 20-30 simultaneous requests

### Production Testing

**Before Production Deployment:**

```bash
# Test with production configuration
export FLASK_ENV=production
gunicorn -c gunicorn.conf.py app:app &

# Load test with memory monitoring
artillery run load-test.yml  # Simulate concurrent users
curl http://localhost:5000/health | jq '.memory_usage_mb'

# Memory leak detection (run for 1+ hours)
while true; do
  curl -s http://localhost:5000/health | jq '.memory_usage_mb'
  sleep 300  # Check every 5 minutes
done
```

## πŸ“š Additional Resources

### Memory Optimization References

- **[Memory Utils Documentation](./src/utils/memory_utils.py)**: Comprehensive memory management utilities
- **[App Factory Pattern](./src/app_factory.py)**: Lazy loading implementation
- **[Gunicorn Configuration](./gunicorn.conf.py)**: Production server optimization
- **[Design Documentation](./design-and-evaluation.md)**: Memory architecture decisions

### Development Tools

```bash
# Memory profiling during development
pip install memory-profiler
python -m memory_profiler your_script.py

# Real-time memory monitoring
pip install psutil
python -c "
import psutil
process = psutil.Process()
print(f'Memory: {process.memory_info().rss / 1024 / 1024:.1f}MB')
"
```

## 🎯 Code Review Guidelines

### Memory-Focused Code Review

**Review Checklist:**

- [ ] Does the code follow lazy loading patterns?
- [ ] Are expensive resources properly cleaned up?
- [ ] Is memory usage tested and validated?
- [ ] Are there any potential memory leaks?
- [ ] Does the change impact startup memory?
- [ ] Is caching used appropriately?

**Memory Review Questions:**

1. "What is the memory impact of this change?"
2. "Could this cause a memory leak in long-running processes?"
3. "Is this resource initialized only when needed?"
4. "Are all expensive objects properly cleaned up?"
5. "How does this scale with concurrent users?"

Thank you for contributing to memory-efficient, production-ready RAG development! Please open issues or PRs against `main` and follow these memory-conscious development practices.