Skip to content

Optimize REST query parsing and reorganize transport implementations - #202

Merged
MisterVVP merged 5 commits into
mainfrom
dev-optimize-rest-and-json-rpc-transport-performance
Aug 10, 2026
Merged

MisterVVP merged 5 commits into
mainfrom
dev-optimize-rest-and-json-rpc-transport-performance

Conversation

@MisterVVP

@MisterVVP MisterVVP commented Aug 10, 2026 •

Copy link
Copy Markdown
Owner

Motivation

  • Reduce REST/HTTP+JSON hot-path allocations and copies to improve throughput and concurrency scaling without changing protocol semantics.
  • Organize transport implementation files under dedicated client/transport and server/transport directories to clarify boundaries and make future transport-focused optimizations easier.
  • Preserve public include paths and SDK compatibility while making internal includes and build lists reflect the new layout.

Description

  • Move transport implementation sources into src/client/transport and src/server/transport and update src/CMakeLists.txt, test CMake targets, and internal #include references to use server/transport/* where applicable.
  • Optimize REST query parsing by adding a fast-path in DecodeUrlComponent (skip per-char decoding when input contains no + or %) and reserving unordered_map capacity in ParseQueryStringImpl before insertions to reduce allocations and rehashing.
  • Add unit tests covering query parsing and URL decoding (RestQueryParserTest) in tests/unit/rest_server_transport_test.cpp.
  • Update benchmark and test includes and add docs/transport-performance.md with guidance and observed parser improvement numbers from the local microbenchmark runs.

@MisterVVP MisterVVP added the codex Automatic label added by codex agent. Should not be used label Aug 10, 2026 — with ChatGPT Codex Connector
@MisterVVP MisterVVP self-assigned this Aug 10, 2026
@MisterVVP MisterVVP added AI agent AI agent contribution and removed codex Automatic label added by codex agent. Should not be used labels Aug 10, 2026
@MisterVVP
MisterVVP marked this pull request as draft August 10, 2026 11:26
Comment thread docs/transport-performance.md Resolved
@MisterVVP
MisterVVP marked this pull request as ready for review August 10, 2026 19:39
@MisterVVP

Copy link
Copy Markdown
Owner Author

@codex review this PR

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

Copy link
Copy Markdown

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: 635ec5837b

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

@MisterVVP
MisterVVP merged commit 73d9665 into main Aug 10, 2026
18 checks passed
@MisterVVP
MisterVVP deleted the dev-optimize-rest-and-json-rpc-transport-performance branch August 10, 2026 20:40
MisterVVP added a commit that referenced this pull request Aug 11, 2026
…and diagnostics (#204)

Closes #203

## Summary

This PR removes the per-request HTTP connection churn that was limiting
HTTP+JSON and JSON-RPC wire scalability.

It keeps the existing HTTP/1.1 transport model, but adds persistent
connection support so ordinary unary requests can reuse the same TCP
connection instead of paying a new accept/close cycle for every
operation.

The implementation also adds connection-scoped buffering, deterministic
persistence coverage, connection-reuse diagnostics, Windows-safe
shutdown behavior, and request-framing hardening discovered during
review.

## Why HTTP/1.1 keep-alive

Issue #203 allowed HTTP/1.1 persistence, HTTP/2, or another reusable
transport strategy.

HTTP/1.1 keep-alive was selected because it directly addresses the
measured structural bottleneck without:

- adding a large new HTTP/2 dependency;
- changing public client/server APIs;
- changing A2A transport semantics;
- implementing HTTP/2 framing in-house;
- expanding the scope beyond the connection-lifecycle bottleneck.

The server can now serve multiple sequential unary requests on one
connection while preserving existing SSE/streaming behavior.

## What changed

### Persistent HTTP connections

- Stop unconditionally forcing `Connection: close` for reusable unary
responses.
- Honor explicit `Connection: close`.
- Keep streaming responses connection-closing under the current HTTP/1.1
model.
- Allow multiple requests to be processed sequentially on one accepted
socket.
- Preserve the existing two-argument `HttpAdapter::WriteResponse(...)`
API while adding explicit connection-close control.

### Connection-scoped request buffering

`HttpConnectionState` now retains bytes that were read beyond the
current request boundary.

This allows the adapter to correctly handle:

- multiple requests arriving in one read;
- back-to-back body-bearing requests;
- fragmented requests spanning multiple reads;
- pipelined buffered carry-over;
- explicit connection closure;
- malformed request termination.

### Request-framing hardening

The adapter explicitly rejects `Transfer-Encoding`.

The current raw HTTP adapter frames request bodies using
`Content-Length`; accepting transfer-encoded requests could otherwise
leave encoded bytes in the connection buffer and desynchronize
subsequent request parsing.

Rejecting unsupported transfer encoding preserves deterministic framing
and prevents ambiguous request-stream interpretation.

### Server lifecycle and portability

- Reuse one worker for multiple requests on an active connection instead
of effectively creating a new socket/thread lifecycle for every unary
operation.
- Keep the signal-owned shutdown flag out of persistent worker-thread
control flow.
- Shut active sockets down during SUT termination so blocked workers
exit cleanly.
- Make Windows listener sockets genuinely nonblocking using Winsock
`FIONBIO`.
- Handle `WSAEWOULDBLOCK` / `WSAEINTR` in the Windows accept loop.
- Use a Windows process group plus `CTRL_BREAK_EVENT` for graceful
performance-SUT shutdown so shutdown-time diagnostics are emitted
consistently.

### Performance diagnostics

The performance runner now records:

- `http_coordinate_accepted_connections`;
- `http_coordinate_completed_unary_operations`;
- `http_coordinate_operations_per_connection`.

The denominator counts only connections that actually carried unary
work, so readiness probes and streaming-only sockets do not distort
unary connection-reuse measurements.

Diagnostics are available in both JSON and CSV reports.

The parser also accepts scientific-notation values emitted by C++ stream
formatting.

### Focused performance execution

The performance runner supports focused scenario selection, including
the case where the selected scenarios contain no wire-capable workload.

This makes targeted transport validation easier without requiring the
entire performance matrix.

## Connection reuse evidence

Latest in-memory HTTP wire diagnostics:

| Concurrency | Unary operations | Unary-carrying connections |
Operations / connection |
| ---: | ---: | ---: | ---: |
| 1 | 27,048 | 1,015 | 26.65 |
| 4 | 27,048 | 1,054 | 25.66 |
| 16 | 27,048 | 1,210 | 22.35 |
| 64 | 27,048 | 1,834 | 14.75 |

The important result is structural: ordinary unary operations no longer
require one accepted TCP connection per operation.

At c64, 27,048 unary operations were completed using 1,834
unary-carrying connections.

## Performance comparison

Representative in-memory wire throughput at c64 compared with the final
PR #202 CI artifact:

| Scenario | HTTP+JSON #202 | HTTP+JSON this PR | Δ | JSON-RPC #202 |
JSON-RPC this PR | Δ |
| --- | ---: | ---: | ---: | ---: | ---: | ---: |
| `GetTask_ExistingTask` | 5,135 ops/s | 7,515 ops/s | **+46.3%** |
2,682 ops/s | 3,314 ops/s | **+23.6%** |
| `ListTasks_WithPagination` | 1,107 ops/s | 1,188 ops/s | **+7.3%** |
977 ops/s | 1,032 ops/s | **+5.7%** |
| `ListTasks_NoPagination` | 596 ops/s | 626 ops/s | **+5.0%** | 529
ops/s | 546 ops/s | **+3.2%** |
| `SendMessage_CreateTask` | 4,383 ops/s | 5,860 ops/s | **+33.7%** |
2,386 ops/s | 2,785 ops/s | **+16.7%** |

These values come from separate GitHub-hosted CI executions, so the
exact percentages should be treated as directional rather than precise
same-machine benchmark deltas.

As an additional control, normalizing HTTP throughput against gRPC
within each corresponding run still improves the relative c64 throughput
for all four representative scenarios, supporting that the improvement
is transport-specific rather than simply a faster runner.

Tail latency is noisier across unrelated hosted-runner executions, so
this PR does not claim precise p95/p99 improvements from those cross-run
values.

The latest performance matrix completed **512 result rows with zero
operation errors**.

---------

Co-authored-by: Vladimir Pavlov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI agent AI agent contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Performance]: Optimize REST and JSON-RPC transport hot paths and reorganize transport code

1 participant