Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-03-16 - [Optimize non-batched vector operations in Weaver]
**Learning:** Network/I/O bound operations in an asynchronous environment should not be sequentially looped. The `Weaver` pipeline was executing non-batched operations sequentially, causing performance to degrade linearly with the number of operations (`O(N)`).
**Action:** Used `asyncio.gather` to concurrently execute non-batched operations, turning an `O(N)` network wait time into approximately `O(1)` (limited by connection pool and concurrency caps). Applied to `src/pipelines/weaver.py`'s execution method. Always look for synchronous or sequentially awaited operations when iterating over a batch of network requests.
3 changes: 0 additions & 3 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,17 @@
import asyncio
import logging
import time
import uuid
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Any, Dict, List

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel

# ── Project root setup ────────────────────────────────────────────
import sys
import os

PROJECT_ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(PROJECT_ROOT))
Expand Down
1 change: 0 additions & 1 deletion src/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from src.api.routes.memory import router as memory_router
from src.api.routes.scanner import router as scanner_router
from src.api.schemas import APIResponse, StatusEnum
from src.config import settings

logger = logging.getLogger("xmem.api")

Expand Down
2 changes: 1 addition & 1 deletion src/api/routes/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def _parse_github_url(url: str) -> tuple:
if m:
return m.group(1), m.group(2)
raise ValueError(
f"Invalid GitHub URL. Expected format: https://github.com/org/repo"
"Invalid GitHub URL. Expected format: https://github.com/org/repo"
)


Expand Down
1 change: 0 additions & 1 deletion src/api/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from __future__ import annotations

from datetime import datetime
from enum import Enum
from typing import Any, Dict, List, Optional

Expand Down
5 changes: 2 additions & 3 deletions src/config/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@

import logging

from logging.handlers import RotatingFileHandler, TimedRotatingFileHandler
from logging.handlers import RotatingFileHandler
import sys
import os
from pathlib import Path
from typing import Optional
from enum import Enum
from dataclasses import dataclass, field
from dataclasses import dataclass


class LogLevel(str, Enum):
Expand Down
1 change: 0 additions & 1 deletion src/pipelines/code_retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
from src.scanner.code_store import CodeStore
from src.schemas.code import (
annotations_namespace,
directories_namespace,
files_namespace,
snippets_namespace,
symbols_namespace,
Expand Down
2 changes: 1 addition & 1 deletion src/pipelines/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
)
from src.schemas.events import EventResult
from src.schemas.image import ImageResult
from src.schemas.judge import JudgeDomain, JudgeResult, OperationType
from src.schemas.judge import JudgeDomain, JudgeResult
from src.schemas.profile import ProfileResult
from src.schemas.summary import SummaryResult
from src.schemas.weaver import WeaverResult
Expand Down
1 change: 0 additions & 1 deletion src/pipelines/retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from __future__ import annotations

import logging
import os
from typing import Any, Callable, Dict, List, Optional

from dotenv import load_dotenv
Expand Down
8 changes: 5 additions & 3 deletions src/pipelines/weaver.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@ async def execute(
batched_executed = await self._execute_batched_vector(judge_result.operations, domain, user_id)
result.executed.extend(batched_executed)
else:
for op in judge_result.operations:
executed = await self._execute_one(op, domain, user_id)
result.executed.append(executed)
import asyncio
# Execute non-batched operations concurrently to significantly reduce latency
tasks = [self._execute_one(op, domain, user_id) for op in judge_result.operations]
executed_ops = await asyncio.gather(*tasks)
result.executed.extend(executed_ops)

self._log_summary(domain, result)
return result
Expand Down
1 change: 0 additions & 1 deletion src/prompts/profiler_topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from dataclasses import dataclass, field
from typing import Dict, List, Union

from src.config.constants import LLM_TAB_SEPARATOR


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion src/prompts/summarizer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from functools import lru_cache
from typing import List, Tuple
from typing import List
import inspect

from src.prompts.examples.summary import SUMMARY_EXAMPLES
Expand Down
4 changes: 1 addition & 3 deletions src/scanner/ast_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
import hashlib
import logging
import re
import textwrap
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Dict, List, Optional, Set, Tuple
from typing import Any, Dict, List, Optional, Tuple

# Tree-sitter imports (optional β€” graceful degradation if not installed)
try:
Expand Down
1 change: 0 additions & 1 deletion src/scanner/git_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from __future__ import annotations

import logging
import os
import subprocess
from dataclasses import dataclass, field
from enum import Enum
Expand Down
2 changes: 0 additions & 2 deletions src/scanner/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@
from src.scanner.ast_parser import ParsedFile, ParsedSymbol, parse_file, compute_content_hash
from src.scanner.code_store import CodeStore
from src.scanner.git_ops import (
DiffResult,
clone_or_pull,
get_diff,
get_head_sha,
get_language,
list_all_files,
should_skip_file,
Expand Down
1 change: 0 additions & 1 deletion src/scanner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import os
import sys
import time
from pathlib import Path
from typing import Any, Dict, List

from dotenv import load_dotenv
Expand Down
2 changes: 1 addition & 1 deletion src/schemas/retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List


@dataclass
Expand Down
1 change: 0 additions & 1 deletion src/schemas/summary.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from __future__ import annotations
from typing import List
from pydantic import BaseModel, Field


Expand Down
3 changes: 0 additions & 3 deletions src/storage/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ def process_memories(store: BaseVectorStore): # <- Takes ANY vector store
from enum import Enum
from ..config import get_logger
from ..utils.exceptions import (
VectorStoreError,
VectorStoreConnectionError,
VectorStoreValidationError,
VectorNotFoundError,
)

logger = get_logger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def another_api_call():
import time
import logging
from dataclasses import dataclass, field
from .exceptions import XMemError, ValidationError
from .exceptions import ValidationError

logger = logging.getLogger(__name__)
T = TypeVar("T")
Expand Down
38 changes: 38 additions & 0 deletions test_weaver_perf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import asyncio
import time

class MockJudgeResult:
def __init__(self, n_ops=10):
self.is_empty = False
self.has_writes = True
self.operations = [{"type": "ADD", "content": f"test {i}"} for i in range(n_ops)]

class MockWeaver:
async def _execute_one(self, op, domain, user_id):
await asyncio.sleep(0.1) # Simulate DB op
return f"Executed {op['content']}"

async def main_sequential():
weaver = MockWeaver()
judge_result = MockJudgeResult()
start = time.time()
result = []
for op in judge_result.operations:
executed = await weaver._execute_one(op, "domain", "user_id")
result.append(executed)
print(f"Sequential took: {time.time() - start:.2f}s")

async def main_concurrent():
weaver = MockWeaver()
judge_result = MockJudgeResult()
start = time.time()

# Run concurrently using asyncio.gather
tasks = [weaver._execute_one(op, "domain", "user_id") for op in judge_result.operations]
result = await asyncio.gather(*tasks)

print(f"Concurrent took: {time.time() - start:.2f}s")

if __name__ == "__main__":
asyncio.run(main_sequential())
asyncio.run(main_concurrent())