dinhanhx / dinhanhx/gpu-friendly-rag
⚡ Performance: np.append in loop creates O(n²) memory allocation
- Dominant language
- Python
- Stars
- 0
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
## Description
In `setup_db.py` line 18:
```python
embedding_db = np.empty((0, embedding_size), dtype=np.float32)
for page in tqdm(pdf):
content = page.get_text()
embedding = embedding_model.encode([content])
embedding_db = np.append(embedding_db, embedding, axis=0) # O(n²)
```
`np.append` copies the entire array each iteration, resulting in O(n²) time and memory. For large PDFs this becomes extremely slow.
## Suggested Fix
Collect embeddings in a list, then stack once:
```python
embeddings = []
for page in tqdm(pdf):
content = page.get_text()
embedding = embedding_model.encode([content])
embeddings.append(embedding)
embedding_db = np.vstack(embeddings)
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.