Repository navigation
Optimize MCP parameter-header processing #3032
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
HarshPopat23
wants to merge
6
commits into
sourcemeta:main
Choose a base branch
from
HarshPopat23:perf/mcp-parameter-headers-20261025
base: main
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
6 commits
Select commit
Hold shift + click to select a range
908964c
Benchmark MCP protocol helpers
HarshPopat23 2b04fb0
Optimize MCP parameter-header validation and lookup
HarshPopat23 81d356a
fix(ci): satisfy MCP comparator and benchmark clang-tidy checks
HarshPopat23 3ebb985
fix(test): satisfy clang-tidy for indexed MCP header fixture
HarshPopat23 0e320d0
fix(build): load the static allocator before C++ runtime allocation s…
HarshPopat23 0206b7f
Address MCP parameter-header review feedback
HarshPopat23 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
Some comments aren't visible on the classic Files Changed page.
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
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,93 @@ | ||
| #include <sourcemeta/core/benchmark.h> | ||
| #include <sourcemeta/core/mcp.h> | ||
|
|
||
| #include <cstddef> // std::size_t | ||
| #include <stdexcept> // std::runtime_error | ||
| #include <string> // std::string, std::to_string | ||
| #include <utility> // std::pair, std::move | ||
| #include <vector> // std::vector | ||
|
|
||
| namespace { | ||
| using namespace sourcemeta::core; | ||
|
|
||
| auto validate_headers(BenchmarkState &state, const std::size_t count) -> void { | ||
| std::vector<std::string> names; | ||
| names.reserve(count); | ||
| auto arguments{JSON::make_object()}; | ||
| for (std::size_t index{0}; index < count; ++index) { | ||
| names.push_back("field" + std::to_string(index)); | ||
| arguments.assign(names.back(), JSON{index % 2 == 0 ? "plain" : " café "}); | ||
| } | ||
|
|
||
| std::vector<MCPHeaderParameter> parameters; | ||
| parameters.reserve(count); | ||
| for (const auto &name : names) { | ||
| parameters.push_back( | ||
| {.name = name, .path = {name}, .type = JSON::Type::String}); | ||
| } | ||
|
|
||
| // Put unrelated headers first to exercise the per-parameter linear search. | ||
| std::vector<std::pair<std::string, std::string>> owned; | ||
| owned.reserve(count * 2); | ||
| for (std::size_t index{0}; index < count; ++index) { | ||
| owned.emplace_back("Other-" + std::to_string(index), "ignored"); | ||
| } | ||
| for (auto &header : mcp_make_parameter_headers(parameters, arguments)) { | ||
| owned.push_back(std::move(header)); | ||
| } | ||
| std::vector<std::pair<JSON::StringView, JSON::StringView>> headers; | ||
| headers.reserve(owned.size()); | ||
| for (const auto &header : owned) { | ||
| headers.emplace_back(header.first, header.second); | ||
| } | ||
|
|
||
| constexpr auto VERSION{MCPProtocolVersion::V_2026_07_28}; | ||
| const JSON identifier{1}; | ||
| if (mcp_validate_parameter_headers(VERSION, identifier, parameters, arguments, | ||
| headers)) { | ||
| throw std::runtime_error{"Invalid MCP header benchmark fixture"}; | ||
| } | ||
| for (auto iteration : state) { | ||
| const auto result{mcp_validate_parameter_headers( | ||
| VERSION, identifier, parameters, arguments, headers)}; | ||
| benchmark_do_not_optimize(result); | ||
| } | ||
| } | ||
| } // namespace | ||
|
|
||
| BENCHMARK(MCP_Validate_Headers_2) { validate_headers(state, 2); } | ||
| BENCHMARK(MCP_Validate_Headers_64) { validate_headers(state, 64); } | ||
|
|
||
| BENCHMARK(MCP_Intersect_Disjoint_512) { | ||
| using namespace sourcemeta::core; | ||
| std::vector<std::string> requested; | ||
| std::vector<std::string> supported; | ||
| requested.reserve(512); | ||
| supported.reserve(512); | ||
| for (std::size_t index{0}; index < 512; ++index) { | ||
| requested.push_back("file:///bench/" + std::to_string(index)); | ||
| supported.push_back("file:///bench/" + std::to_string(index + 512)); | ||
| } | ||
|
|
||
| MCPSubscriptionFilter left; | ||
| MCPSubscriptionFilter right; | ||
| left.resource_subscriptions.emplace(); | ||
| right.resource_subscriptions.emplace(); | ||
| for (const auto &uri : requested) { | ||
| left.resource_subscriptions->push_back(uri); | ||
| } | ||
| for (const auto &uri : supported) { | ||
| right.resource_subscriptions->push_back(uri); | ||
| } | ||
|
|
||
| // No matches forces a complete supported-list scan for each requested URI. | ||
| const auto control{mcp_intersect_subscription_filters(left, right)}; | ||
| if (!control.resource_subscriptions || | ||
| !control.resource_subscriptions->empty()) { | ||
| throw std::runtime_error{"Invalid MCP subscription benchmark fixture"}; | ||
| } | ||
| for (auto iteration : state) { | ||
| const auto result{mcp_intersect_subscription_filters(left, right)}; | ||
| benchmark_do_not_optimize(result); | ||
| } | ||
| } |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe worth landing the relevant benchmark first on
main, so we can then see in this PR the performance difference of these code changes?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure,
Let's Land benchmark first.