Skip to content

Optimize unbounded byte scans with memchr#26265

Merged
charliemarsh-oai merged 4 commits into
mainfrom
charlie/use-memchr-for-byte-scans
Jun 4, 2026
Merged

Optimize unbounded byte scans with memchr#26265
charliemarsh-oai merged 4 commits into
mainfrom
charlie/use-memchr-for-byte-scans

Conversation

@charliemarsh-oai

@charliemarsh-oai charliemarsh-oai commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds memchr for some low-hanging performance improvements (namely, in MCP stdio, Ollama streaming, and full message-history newline counts).

Codex produced the following release benchmarks:

Operation Before After Speedup
MCP 1 MiB chunked line 2.172 s 3.984 ms 545x
Ollama 1 MiB chunked line 1.673 s 2.790 ms 600x
Count newlines in 10 MiB history 132.83 ms 20.05 ms 6.6x

With a "real" MCP setup (ExecutorStdioServerLauncher started a Python MCP server, completed initialize, requested tools/list, and deserialized a 1 MiB tool description over newline-delimited stdio), it's about 16x faster end-to-end:

Branch 50 calls Per call
main 862.53 ms 17.25 ms
this branch 53.89 ms 1.08 ms

memchr is already in our dependency tree and extremely widely used for this kind of optimized scanning.

@charliemarsh-oai charliemarsh-oai force-pushed the charlie/use-memchr-for-byte-scans branch from 8f1acf4 to 15a6c3c Compare June 3, 2026 23:35
@charliemarsh-oai charliemarsh-oai marked this pull request as ready for review June 4, 2026 00:53

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 98b972d0ce

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread codex-rs/Cargo.toml
log = "0.4"
lru = "0.16.3"
maplit = "1.0.2"
memchr = "2.7.6"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Refresh the Bazel module lockfile

This commit adds memchr as a new workspace/direct Rust dependency, but the diff does not include a corresponding MODULE.bazel.lock update. The repository rule requires just bazel-lock-update and committing the refreshed lockfile whenever Cargo.toml or Cargo.lock changes (AGENTS.md lines 34-37); otherwise Bazel lockfile checks can reject the change even though Cargo builds resolve the dependency.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think memchr 2.7.6 is already in MODULE.bazel.lock.

@fcoury-oai fcoury-oai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did some basic smoke test and worked as expected. Code looks good, approved.

@charliemarsh-oai charliemarsh-oai merged commit 7da4af6 into main Jun 4, 2026
46 checks passed
@charliemarsh-oai charliemarsh-oai deleted the charlie/use-memchr-for-byte-scans branch June 4, 2026 13:53
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants