Skip to content

Skip the special-token set rebuild when encoding a single special token - #159

Open
Hexay wants to merge 1 commit into
openai:mainfrom
Hexay:special-token-fast-path-upstream
Open

Hexay wants to merge 1 commit into
openai:mainfrom
Hexay:special-token-fast-path-upstream

Conversation

@Hexay

@Hexay Hexay commented Nov 27, 2026

Copy link
Copy Markdown

encode_with_special_tokens collects every special token (about 1.1k for HarmonyGptOss) into a fresh HashSet on each call. render_formatting_token is its only caller: it runs once per <|start|>, <|message|>, <|end|> and so on while rendering, and StreamableParser runs it for every token consumed in the ExpectStart and Header states.

This returns the id directly when the whole input is exactly one special token, which is what the general path produces for that input. Other inputs are unchanged.

Release build, same machine, before → after:

Case Before After
Render 7 messages, 2 tools (816 tokens) 3.02 ms 1.72 ms
Render 102 messages, 10 tools (11,460 tokens) 54.2 ms 13.9 ms
Render 502 messages, 30 tools (55,100 tokens) 255 ms 50.2 ms
Parse the 102-message prompt back 102 ms 16.8 ms

After the change, rendering the 102-message conversation costs about the same as tokenising its text once (13.5 ms).

The added test checks the fast path against the general path for every special token and for a few mixed inputs.

#142 precomputes the two ids used by StreamableParser; this covers those call sites and the render path in one place.

`encode_with_special_tokens` collects every special token into a fresh
`HashSet` on each call. `render_formatting_token` calls it once per
`<|start|>`, `<|message|>`, `<|end|>`, ... and `StreamableParser` calls it
for every token consumed in the `ExpectStart` and `Header` states.

Return the id directly when the whole input is exactly one special token,
which is what the general path produces for that input.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T11:09:41.579090Z ce83aed PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant