-
Notifications
You must be signed in to change notification settings - Fork 878
Cut streaming render cost #1101
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
Open
ecokayiza
wants to merge
5
commits into
ChatGPTBox-dev:master
Choose a base branch
from
ecokayiza:feat/streaming-render-perf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a3af35a
Cut streaming render cost
ecokayiza 815a075
Keep common languages in syntax auto-detection
ecokayiza f2fae2f
Discard buffered streamed frames when the card switches question
ecokayiza 7d73b32
Flush the answer buffer when the transport drops
ecokayiza e3f81bf
Keep a foreground generation open when the keepalive Port drops
ecokayiza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /** | ||
| * Animation frames are the fastest way to coalesce a burst of updates, but not every host | ||
| * has them (jsdom and other headless renderers do not). Fall back to a timer there so the | ||
| * buffer can always be created. | ||
| * @param {typeof globalThis} [host] | ||
| * @returns {{requestFrame: (callback: () => void) => unknown, cancelFrame: (handle: unknown) => void}} | ||
| */ | ||
| export function createFrameScheduler(host = globalThis) { | ||
| if ( | ||
| typeof host.requestAnimationFrame === 'function' && | ||
| typeof host.cancelAnimationFrame === 'function' | ||
| ) { | ||
| return { | ||
| requestFrame: (callback) => host.requestAnimationFrame(callback), | ||
| cancelFrame: (handle) => host.cancelAnimationFrame(handle), | ||
| } | ||
| } | ||
| return { | ||
| requestFrame: (callback) => setTimeout(callback, 16), | ||
| cancelFrame: (handle) => clearTimeout(handle), | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Coalesces streamed answer text so a burst of chunks renders once per frame instead of | ||
| * once per chunk, while never losing the newest text. | ||
| * @param {object} params | ||
| * @param {(callback: () => void) => unknown} params.requestFrame | ||
| * @param {(handle: unknown) => void} params.cancelFrame | ||
| * @param {(answer: string) => void} params.render | ||
| */ | ||
| export function createAnswerBuffer({ requestFrame, cancelFrame, render }) { | ||
| let pending = null | ||
| let frame = null | ||
|
|
||
| const cancelPendingFrame = () => { | ||
| if (frame === null) return | ||
| cancelFrame(frame) | ||
| frame = null | ||
| } | ||
|
|
||
| const takePending = () => { | ||
| const answer = pending | ||
| pending = null | ||
| return answer | ||
| } | ||
|
|
||
| return { | ||
| /** Queue the newest answer, scheduling a render only when none is already scheduled. */ | ||
| push(answer) { | ||
| pending = answer | ||
| if (frame !== null) return | ||
| frame = requestFrame(() => { | ||
| frame = null | ||
| const latest = takePending() | ||
| if (latest !== null) render(latest) | ||
| }) | ||
| }, | ||
| /** Render the newest answer before the conversation is finalized. */ | ||
| flush() { | ||
| cancelPendingFrame() | ||
| const latest = takePending() | ||
| if (latest !== null) render(latest) | ||
| }, | ||
| /** Drop the newest answer without rendering it, e.g. when a retry starts. */ | ||
| discard() { | ||
| cancelPendingFrame() | ||
| pending = null | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * Shared rehype-highlight options. | ||
| * | ||
| * Auto-detection compiles every registered grammar the first time it runs, which costs | ||
| * >100ms on a cold page. Scanning a curated subset keeps that near a few milliseconds. | ||
| * Explicitly labelled code blocks are unaffected: the subset only narrows automatic | ||
| * detection, they still use every grammar lowlight has registered. | ||
| * | ||
| * The subset is lowlight's registered set minus entries that only mislead detection: | ||
| * `shell` (the Shell Session console-prompt grammar, not a `bash` alias; `bash` aliases | ||
| * only `sh`/`zsh`), `plaintext`/`python-repl`/`php-template` (not useful for detection), | ||
|
ecokayiza marked this conversation as resolved.
|
||
| * and `arduino`/`objectivec`/`vbnet`/`wasm` (rare in answers, and they win ambiguous `c`, | ||
| * `cpp`, `ini` and `sql` matches away from the right language). Keep the | ||
| * order as lowlight registers them: `highlightAuto` settles equal-relevance candidates by | ||
| * subset order, so reordering this list changes which language an ambiguous block detects as. | ||
| */ | ||
| export const highlightOptions = { | ||
| detect: true, | ||
| subset: [ | ||
| 'bash', | ||
| 'c', | ||
| 'cpp', | ||
| 'csharp', | ||
| 'css', | ||
| 'diff', | ||
| 'go', | ||
| 'graphql', | ||
| 'ini', | ||
| 'java', | ||
| 'javascript', | ||
| 'json', | ||
| 'kotlin', | ||
| 'less', | ||
| 'lua', | ||
| 'makefile', | ||
| 'markdown', | ||
| 'perl', | ||
| 'php', | ||
| 'python', | ||
| 'r', | ||
| 'ruby', | ||
| 'rust', | ||
| 'scss', | ||
| 'sql', | ||
| 'swift', | ||
| 'typescript', | ||
| 'xml', | ||
| 'yaml', | ||
| ], | ||
| ignoreMissing: true, | ||
| plainText: ['diagnostic'], | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.