open-webui/backend/open_webui
Classic298 b2a9be27c5 perf: stop recopying the streamed message text on every delta
Every streamed token appended to the response text through a dict slot, which allocates a new string and copies everything received so far. A long answer pays that copy thousands of times on the event loop, so every other request on the worker waits behind it.

The appends now clear the slot before growing the string, which leaves a single reference and lets CPython resize it in place. This covers the six accumulation points in the Chat Completions streaming path. The Responses API transport copies its items before appending, so it holds two references and is unchanged.

Measured on CPython 3.12 with 10-character deltas:

| accumulated text | before | after |
|---|---|---|
| 100 KB | 5.97 ms | 0.98 ms |
| 250 KB | 29.53 ms | 2.83 ms |
| 1 MB | 659 ms | 15.5 ms |

Results match the replaced expressions for every input where the key exists and holds a string, key order and exception type included. Buffering into a list and joining at the end was the alternative, but each delta is read back immediately by the stream save, and joining per read measured 666 ms at 250 KB.
2026-08-23 22:37:08 +02:00
..
data refac: mv backend files to /open_webui dir 2024-09-04 16:54:48 +02:00
internal perf: route native JSON columns through JSONCodec instead of stdlib json (#28396) 2026-08-10 19:39:49 -05:00
migrations refac 2026-08-10 23:22:08 -06:00
models refac 2026-08-23 13:49:50 -04:00
retrieval refac: drop the redundant .keys() from two dict membership tests (#28859) 2026-08-23 16:02:09 -04:00
routers refac: drop the redundant .keys() from two dict membership tests (#28859) 2026-08-23 16:02:09 -04:00
socket fix: keep the usage pool cleanup task alive across lock loss and Redis errors (#28834) 2026-08-20 12:59:38 -07:00
static refac 2026-07-27 19:39:36 -04:00
storage refac 2026-07-31 17:41:14 -04:00
tools refac 2026-08-17 00:24:47 -07:00
utils perf: stop recopying the streamed message text on every delta 2026-08-23 22:37:08 +02:00
__init__.py refac 2026-07-27 19:39:36 -04:00
alembic.ini fix: Alembic CLI commands from failing 2025-08-15 04:17:47 -04:00
config.py refac 2026-08-20 13:13:51 -07:00
constants.py refac: make the web search error message a plain constant (#28948) 2026-08-23 13:27:02 -04:00
env.py refac 2026-08-20 13:13:51 -07:00
events.py refac 2026-08-16 23:21:00 -07:00
functions.py fix: honor bypass_system_prompt on the pipe route (#28739) 2026-08-19 11:08:02 -07:00
main.py refac 2026-08-17 00:47:32 -07:00
tasks.py refac 2026-08-23 13:40:13 -04:00