Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PERF] Use orjson in client #2434

Merged
merged 3 commits into from
Jul 2, 2024
Merged
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
11 changes: 9 additions & 2 deletions chromadb/api/async_fastapi.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
from uuid import UUID
import urllib.parse
import orjson as json
import orjson
from typing import Any, Optional, cast, Tuple, Sequence, Dict
import logging
import httpx
Expand Down Expand Up @@ -115,13 +115,20 @@ def _get_client(self) -> httpx.AsyncClient:
async def _make_request(
self, method: str, path: str, **kwargs: Dict[str, Any]
) -> Any:
# If the request has json in kwargs, use orjson to serialize it,
# remove it from kwargs, and add it to the data parameter
# This is because httpx uses a slower json serializer
if "json" in kwargs:
data = orjson.dumps(kwargs.pop("json"))
kwargs["data"] = data

# Unlike requests, httpx does not automatically escape the path
escaped_path = urllib.parse.quote(path, safe="/", encoding=None, errors=None)
url = self._api_url + escaped_path

response = await self._get_client().request(method, url, **cast(Any, kwargs))
BaseHTTPClient._raise_chroma_error(response)
return json.loads(response.text)
return orjson.loads(response.text)

@trace_method("AsyncFastAPI.heartbeat", OpenTelemetryGranularity.OPERATION)
@override
Expand Down
11 changes: 9 additions & 2 deletions chromadb/api/fastapi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import orjson as json
import orjson
import logging
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sort of obfuscation should be considered a crime.

from typing import Any, Dict, Optional, cast, Tuple
from typing import Sequence
Expand Down Expand Up @@ -76,13 +76,20 @@ def __init__(self, system: System):
self._session.headers[header] = value.get_secret_value()

def _make_request(self, method: str, path: str, **kwargs: Dict[str, Any]) -> Any:
# If the request has json in kwargs, use orjson to serialize it,
# remove it from kwargs, and add it to the data parameter
# This is because httpx uses a slower json serializer
if "json" in kwargs:
data = orjson.dumps(kwargs.pop("json"))
kwargs["data"] = data

# Unlike requests, httpx does not automatically escape the path
escaped_path = urllib.parse.quote(path, safe="/", encoding=None, errors=None)
url = self._api_url + escaped_path

response = self._session.request(method, url, **cast(Any, kwargs))
BaseHTTPClient._raise_chroma_error(response)
return json.loads(response.text)
return orjson.loads(response.text)

@trace_method("FastAPI.heartbeat", OpenTelemetryGranularity.OPERATION)
@override
Expand Down
Loading