perf: build debug log messages lazily so disabled debug logs cost nothing (#27834)

GLOBAL_LOG_LEVEL defaults to INFO, so every log.debug(...) in the backend is discarded, but the message is built first: 187 call sites interpolate their payload into an f-string before the logging call runs, so the work happens on every request and the result is thrown away. The worst one sits in process_chat_payload and stringifies the whole request body, full conversation history included, once per chat completion.

That one line with DEBUG disabled, CPython 3.12:

| conversation | payload | before   | after   |
| ------------ | ------- | -------- | ------- |
| 4 messages   | 1.2 kB  | 3.4 us   | 0.07 us |
| 20 messages  | 17 kB   | 24.8 us  | 0.07 us |
| 60 messages  | 123 kB  | 216.6 us | 0.07 us |

The lazy form log.debug('form_data: %s', form_data) hands the payload to record.getMessage(), which the InterceptHandler only reaches once a record has passed the level check. With DEBUG enabled the emitted lines are byte-identical, f'{x=}' sites included: those map to %r. MistralLoader._debug_log callers get the same treatment, since that wrapper already forwards *args.
This commit is contained in:
Classic298 authored and GitHub committed 2026-07-31 19:09:01 -05:00
1 parent b67804f2b6
commit 52cfb02c72
40 files changed
+198 -190

No files matched your search

@@ -54,7 +54,7 @@ class S3VectorClient(VectorDBBase):
Create a new index in the S3 vector bucket for the given collection if it does not exist.
"""
if self.has_collection(index_name):
log.debug(f"Index '{index_name}' already exists, skipping creation")
log.debug("Index '%s' already exists, skipping creation", index_name)
return
try:
@@ -310,7 +310,7 @@ class S3VectorClient(VectorDBBase):
# Process each query vector
for i, query_vector in enumerate(vectors):
log.debug(f'Processing query vector {i + 1}/{len(vectors)}')
log.debug('Processing query vector %s/%s', i + 1, len(vectors))
# Prepare the query vector in S3 Vector format
query_vector_dict = {'float32': [float(x) for x in query_vector]}
@@ -521,7 +521,7 @@ class S3VectorClient(VectorDBBase):
)
# Log the actual content for debugging
log.debug(f'Document text preview (first 200 chars): {str(document_text)[:200]}')
log.debug('Document text preview (first 200 chars): %s', str(document_text)[:200])
else:
document_text = vector_id