mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
Optimize unbounded byte scans with memchr (#26265)
## 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.
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
use bytes::BytesMut;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use super::LineBuffer;
|
||||
|
||||
#[test]
|
||||
fn searches_only_new_bytes_after_partial_line() {
|
||||
let mut buffer = LineBuffer::default();
|
||||
|
||||
buffer.extend_from_slice(b"partial");
|
||||
assert_eq!(buffer.take_line(), None);
|
||||
assert_eq!(
|
||||
buffer,
|
||||
LineBuffer {
|
||||
bytes: BytesMut::from(&b"partial"[..]),
|
||||
scanned_len: 7,
|
||||
}
|
||||
);
|
||||
|
||||
buffer.extend_from_slice(b" line");
|
||||
assert_eq!(buffer.take_line(), None);
|
||||
assert_eq!(
|
||||
buffer,
|
||||
LineBuffer {
|
||||
bytes: BytesMut::from(&b"partial line"[..]),
|
||||
scanned_len: 12,
|
||||
}
|
||||
);
|
||||
|
||||
buffer.extend_from_slice(b"\nnext");
|
||||
assert_eq!(
|
||||
buffer.take_line(),
|
||||
Some(BytesMut::from(&b"partial line"[..]))
|
||||
);
|
||||
assert_eq!(
|
||||
buffer,
|
||||
LineBuffer {
|
||||
bytes: BytesMut::from(&b"next"[..]),
|
||||
scanned_len: 0,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn splits_multiple_lines_and_retains_partial_tail() {
|
||||
let mut buffer = LineBuffer::default();
|
||||
buffer.extend_from_slice(b"first\nsecond\npartial");
|
||||
|
||||
assert_eq!(buffer.take_line(), Some(BytesMut::from(&b"first"[..])));
|
||||
assert_eq!(buffer.take_line(), Some(BytesMut::from(&b"second"[..])));
|
||||
assert_eq!(buffer.take_line(), None);
|
||||
assert_eq!(
|
||||
buffer,
|
||||
LineBuffer {
|
||||
bytes: BytesMut::from(&b"partial"[..]),
|
||||
scanned_len: 7,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn takes_unterminated_remaining_bytes_at_eof() {
|
||||
let mut buffer = LineBuffer::default();
|
||||
buffer.extend_from_slice(b"remaining");
|
||||
assert_eq!(buffer.take_line(), None);
|
||||
|
||||
assert_eq!(
|
||||
buffer.take_remaining(),
|
||||
Some(BytesMut::from(&b"remaining"[..]))
|
||||
);
|
||||
assert_eq!(buffer, LineBuffer::default());
|
||||
}
|
||||
Reference in New Issue
Block a user