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
2026-07-31 19:09:01 -05:00
committed by GitHub
parent b67804f2b6
commit 52cfb02c72
40 changed files with 198 additions and 190 deletions
+7 -7
View File
@@ -210,7 +210,7 @@ async def periodic_usage_pool_cleanup():
break
else:
if attempt < max_retries:
log.debug(f'Cleanup lock already exists. Retry {attempt + 1} after {retry_delay}s...')
log.debug('Cleanup lock already exists. Retry %s after %ss...', attempt + 1, retry_delay)
await asyncio.sleep(retry_delay)
else:
log.warning('Failed to acquire cleanup lock after retries. Skipping cleanup.')
@@ -237,7 +237,7 @@ async def periodic_usage_pool_cleanup():
del connections[sid]
if not connections:
log.debug(f'Cleaning up model {model_id} from usage pool')
log.debug('Cleaning up model %s from usage pool', model_id)
del USAGE_POOL[model_id]
else:
USAGE_POOL[model_id] = connections
@@ -299,7 +299,7 @@ async def emit_to_users(event: str, data: dict, user_ids: list[str]):
for user_id in user_ids:
await sio.emit(event, data, room=f'user:{user_id}')
except Exception as e:
log.debug(f'Failed to emit event {event} to users {user_ids}: {e}')
log.debug('Failed to emit event %s to users %s: %s', event, user_ids, e)
async def enter_room_for_users(room: str, user_ids: list[str]):
@@ -315,7 +315,7 @@ async def enter_room_for_users(room: str, user_ids: list[str]):
for sid in session_ids:
await sio.enter_room(sid, room)
except Exception as e:
log.debug(f'Failed to make users {user_ids} join room {room}: {e}')
log.debug('Failed to make users %s join room %s: %s', user_ids, room, e)
async def disconnect_user_sessions(user_id: str):
@@ -411,7 +411,7 @@ async def user_join(sid, data):
# Join all the channels only if user has channels permission
if user.role == 'admin' or await has_permission(user.id, 'features.channels'):
channels = await Channels.get_channels_by_user_id(user.id)
log.debug(f'{channels=}')
log.debug('channels=%r', channels)
for channel in channels:
await sio.enter_room(sid, f'channel:{channel.id}')
@@ -443,7 +443,7 @@ async def join_channel(sid, data):
# Join all the channels only if user has channels permission
if user.role == 'admin' or await has_permission(user.id, 'features.channels'):
channels = await Channels.get_channels_by_user_id(user.id)
log.debug(f'{channels=}')
log.debug('channels=%r', channels)
for channel in channels:
await sio.enter_room(sid, f'channel:{channel.id}')
@@ -480,7 +480,7 @@ async def join_note(sid, data):
log.error(f'User {user.id} does not have access to note {data["note_id"]}')
return
log.debug(f'Joining note {note.id} for user {user.id}')
log.debug('Joining note %s for user %s', note.id, user.id)
await sio.enter_room(sid, f'note:{note.id}')