Skip to content

perf(vectorstore): eliminate O(n²) memory copy on every add() #570

Closed

Description

@AmitoVrito

Problem

vectorstore.py:34 copies the entire existing vector matrix on every add() call:

self._vectors = np.concatenate([self._vectors, vecs], axis=0)

Adding n documents triggers 1 + 2 + … + n = O(n²) total bytes copied. For a 10 000-document corpus the last insert copies ~9 999 rows unnecessarily.

Impact

HIGH — memory usage and insert latency grow quadratically with corpus size.

Fix

Accumulate chunks in a list[np.ndarray], then call np.vstack() exactly once at the first query (lazy consolidation). Keep a dirty flag so re-consolidation only happens after new adds.

def add(self, vecs):
    self._pending.append(vecs)
    self._dirty = True

def _consolidate(self):
    if self._dirty:
        self._vectors = np.vstack(self._pending_or_existing)
        self._dirty = False

Acceptance Criteria

  • add() is O(chunk_size) not O(total_size)
  • query() consolidates lazily and caches the result
  • All existing vectorstore tests pass

Activity

  1. self-assigned this
    on Apr 20, 2026
  2. added
    performance
    on Apr 20, 2026
  3. AmitoVrito commented on Apr 20, 2026

    @AmitoVrito
    CollaboratorAuthor

    Fixed in commit 248fea9 — all 12 performance issues resolved and regression tests added in c10f5eb. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

performance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions