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

+8 -8
View File
@@ -149,7 +149,7 @@ def normalize_openai_edit_image_data_url(data_url: str) -> str:
normalized_image = base64.b64encode(output.getvalue()).decode('utf-8')
return f'data:image/jpeg;base64,{normalized_image}'
except Exception as e:
log.debug(f'Image edit normalization skipped: {e}')
log.debug('Image edit normalization skipped: %s', e)
return data_url
@@ -194,7 +194,7 @@ async def set_image_model(request: Request, model: str):
) as r:
r.raise_for_status()
except Exception as e:
log.debug(f'{e}')
log.debug('%s', e)
return image_config.IMAGE_GENERATION_MODEL
@@ -488,7 +488,7 @@ async def get_image_data(data: str, headers=None, trusted_base_url: str | None =
# ENABLE_LOCAL_WEB_FETCH hammer and a blanket trust flag
# that would follow arbitrary redirects.
if trusted_base_url and _is_same_origin(data, trusted_base_url):
log.debug(f'Skipping URL validation for trusted backend: {data}')
log.debug('Skipping URL validation for trusted backend: %s', data)
else:
await asyncio.to_thread(validate_url, data)
session = await get_session()
@@ -762,7 +762,7 @@ async def image_generations(
image_config.COMFYUI_BASE_URL,
image_config.COMFYUI_API_KEY,
)
log.debug(f'res: {res}')
log.debug('res: %s', res)
images = []
@@ -817,7 +817,7 @@ async def image_generations(
ssl=AIOHTTP_CLIENT_SESSION_SSL,
) as r:
res = await r.json(content_type=None)
log.debug(f'res: {res}')
log.debug('res: %s', res)
images = []
@@ -1119,7 +1119,7 @@ async def image_edits(
)
comfyui_images.append(res.get('name', file_item[1][0]))
except Exception as e:
log.debug(f'Error uploading images to ComfyUI: {e}')
log.debug('Error uploading images to ComfyUI: %s', e)
raise Exception('Failed to upload images to ComfyUI.')
data = {
@@ -1148,7 +1148,7 @@ async def image_edits(
image_config.IMAGES_EDIT_COMFYUI_BASE_URL,
image_config.IMAGES_EDIT_COMFYUI_API_KEY,
)
log.debug(f'res: {res}')
log.debug('res: %s', res)
image_urls = set()
for image in res['data']:
@@ -1160,7 +1160,7 @@ async def image_edits(
if output_type_urls:
image_urls = output_type_urls
log.debug(f'Image URLs: {image_urls}')
log.debug('Image URLs: %s', image_urls)
images = []
for image_url in image_urls: