Skip to content

router: optimize router to avoid unnecessary headers/trailers copy#44323

Open
wbpcode wants to merge 3 commits intoenvoyproxy:mainfrom
wbpcode:dev-cleanup-router-shadow-policy
Open

router: optimize router to avoid unnecessary headers/trailers copy#44323
wbpcode wants to merge 3 commits intoenvoyproxy:mainfrom
wbpcode:dev-cleanup-router-shadow-policy

Conversation

@wbpcode
Copy link
Copy Markdown
Member

@wbpcode wbpcode commented Apr 8, 2026

Commit Message: router: optimize router to avoid unnecessary headers/trailers copy
Additional Description:

This initially is part of #44305 but I found I forgot to push it and this deserve another PR anyway.

Risk Level: low.
Testing: n/a.
Docs Changes: n/a.
Release Notes: n/a.
Platform Specific Features: n/a.

Signed-off-by: wbpcode/wangbaiping <wbphub@gmail.com>
Comment on lines -1094 to -1096
if (shadow_headers_) {
shadow_trailers_ = Http::createHeaderMap<Http::RequestTrailerMapImpl>(trailers);
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is unnecessary because we will always to copy a new trailers map for every shadow stream.

Comment on lines -886 to +887
Http::RequestMessagePtr request(new Http::RequestMessageImpl(
Http::createHeaderMap<Http::RequestHeaderMapImpl>(*shadow_headers_)));
applyShadowPolicyHeaders(shadow_policy, request->headers());
Http::RequestMessagePtr request(new Http::RequestMessageImpl(std::move(shadow_headers)));
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

to reuse the previously copied one shadow_headers rather then to copy another one here.

Comment on lines -831 to +833
active_shadow_policies_.push_back(std::cref(policy_ref));
shadow_headers_ = Http::createHeaderMap<Http::RequestHeaderMapImpl>(*downstream_headers_);
active_shadow_policies.push_back(std::cref(policy_ref));
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's unnecessary to copy the downstream headers again and again. And actually the shadow_headers_ will not be used actually.

Copy link
Copy Markdown
Member

@botengyao botengyao left a comment

Choose a reason for hiding this comment

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

thanks for the optimization, have one concern.

/wait

continue;
}
auto shadow_headers = Http::createHeaderMap<Http::RequestHeaderMapImpl>(*shadow_headers_);
auto shadow_headers = Http::createHeaderMap<Http::RequestHeaderMapImpl>(*downstream_headers_);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks like a behavior change since UpstreamRequest construction injects trace context into *parent_.downstreamHeaders(), and this is after the acceptHeadersFromRouter(), so it can make mirrored requests inherit upstream mutations, but before not?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

good point, I forgot the trace injection. I will update it.

Signed-off-by: wbpcode <wbphub@gmail.com>
@botengyao
Copy link
Copy Markdown
Member

/retest

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.

3 participants