Skip to content

listener: reuse unchanged filter chains by their serialized bytes - #49278

Merged
agrawroh merged 7 commits into
envoyproxy:mainfrom
agrawroh:agrawroh/lds-filter-chain-reuse-bytes
Nov 22, 2026
Merged

agrawroh merged 7 commits into
envoyproxy:mainfrom
agrawroh:agrawroh/lds-filter-chain-reuse-bytes

Conversation

@agrawroh

@agrawroh agrawroh commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

Description

This PR makes LDS updates that keep their filter chains cheaper. The filter chain manager finds an unchanged filter chain of the listener it updates by the deterministic serialization of its message, from MessageUtil::serializeDeterministically() in #49268, instead of hashing and comparing every filter chain message by reflection. Only a filter chain whose bytes are new is hashed by reflection, so that a later update can still find it by its message.

A filter chain whose bytes differ but whose message is equal, such as one whose Any payloads were encoded again, is still found by MessageUtil::hash() and a message comparison, and every origin filter chain that shares a hash is compared. The default filter chain is compared by its bytes and then by a message comparison. The drain decisions of an in place update now compare the filter chains the new listener holds rather than their messages, for the default filter chain too. A filter chain holding a NaN, which never compares equal to itself, is now reused when its bytes are unchanged, where before it was rebuilt and drained on every in place update.

filter_chain_benchmark_test built with -c opt at this branch and at its merge base with this branch's benchmark, and run as filter_chain_benchmark_test --benchmark_filter='FilterChainManager(Build|Update)' five times each, interleaved, on a shared 48-core host, gives these median milliseconds per call, from the merge base to this branch. Every run fell within 15% of its cell's median. A size counts the listener's destination port filter chains, and each listener holds two more.

Benchmark Size 1 Size 8 Size 64 Size 512 Size 4096
Update that keeps every filter chain 0.155 → 0.055 0.537 → 0.192 3.84 → 1.32 29.9 → 10.7 248 → 89
Update that encodes every filter chain again 0.212 → 0.108 0.730 → 0.368 5.23 → 2.50 41.1 → 19.6 334 → 160
Update that changes every filter chain 0.081 → 0.074 0.289 → 0.254 2.45 → 2.06 38.1 → 34.6 1849 → 1768
First build of the filter chains 0.069 → 0.069 0.246 → 0.241 1.87 → 1.67 21.1 → 19.2 541 → 530

An update that only encodes the filter chains again matches the listener's hash and stops before the filter chain manager, so the second row stands for such filter chains inside an update that changes something else. The last two rows move by at most 16%, close to that spread. In a perf profile of the size 4096 runs, gmock's teardown of the benchmark's mock filter chains takes about half of the cycles of the changed run and a quarter of the first build's, and that cost grows faster than the number of filter chains. These numbers cover the filter chain manager's part of an update. Decoding and validating the listener and hashing its whole message by reflection come before it, and a local benchmark of that hash over the same listeners took about 5.3 ms at size 4096.

This PR was written with AI assistance.


Commit Message: listener: reuse unchanged filter chains by their serialized bytes
Additional Description:
Risk Level: Low
Testing: Added Tests
Docs Changes: N/A
Release Notes: N/A
Platform Specific Features: N/A

This pull request and its description were written by Isaac.

Signed-off-by: Rohit Agrawal <rohit.agrawal@databricks.com>

Copilot AI 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.

🟡 Changes recommended

The hash index retains only one origin chain per hash, so equal-content updates to omitted colliding chains can unnecessarily rebuild them and drain their existing instances.

1 open issue
What changed in this PR

This PR reduces the cost of Envoy listener updates by matching unchanged filter chains using deterministic serialized bytes, with a message-equality fallback.

Changes:

  • Indexes filter chains by serialized bytes and tracks reused instances when determining which chains to drain.
  • Applies byte-first comparison to default filter-chain reuse and listener diffing.
  • Adds regression tests and an unchanged-chain update benchmark covering 1–4096 chains.
File Description
test/​common/​listener_manager/​filter_chain_manager_impl_test.cc Tests reserialized payloads, unequal messages with equal hashes, and reuse across updates.
test/​common/​listener_manager/​filter_chain_benchmark_test.cc Benchmarks updates that preserve every filter chain.
test/​common/​listener_manager/​BUILD Adds dependencies for the new tests.
source/​common/​listener_manager/​listener_impl.cc Uses byte-first comparison when diffing default chains.
source/​common/​listener_manager/​filter_chain_manager_impl.h Defines serialized-byte storage and the fallback hash index.
source/​common/​listener_manager/​filter_chain_manager_impl.cc Implements byte-first reuse, fallback comparison, and instance-based draining detection.
source/​common/​listener_manager/​BUILD Adds the hash-set dependency used for draining detection.

Review effort: Balanced

if (origin != nullptr) {
origin_fc_contexts.reserve(origin->fc_contexts_.size());
for (const auto& origin_fc_context : origin->fc_contexts_) {
origin_fc_contexts.emplace(origin_fc_context.second.message_hash, &origin_fc_context);

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.

Every origin chain that shares a hash is now kept and compared, so a chain whose hash another chain shares is still reused.

agrawroh and others added 6 commits November 21, 2026 17:37
…-chain-reuse-bytes

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Rohit Agrawal <rohit.agrawal@databricks.com>
… path

Drain the default filter chain of an in place update only when the new
listener does not hold the same filter chain, which drops the second
comparison and the accessor it read. Keep the comparison helper local
to the filter chain manager, and test that unchanged bytes alone reuse
a filter chain. Add benchmarks of updates that encode every filter
chain again or change every filter chain.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Rohit Agrawal <rohit.agrawal@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Rohit Agrawal <rohit.agrawal@databricks.com>
… paths

Give the default filter chain of the in place update test a NaN, so
that only its identity shows the new listener reuses it. Assert that
the reencoded test inputs differ in bytes, and that the update
benchmarks rebuild no filter chain they mean to reuse.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Rohit Agrawal <rohit.agrawal@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Rohit Agrawal <rohit.agrawal@databricks.com>
Keep every origin filter chain of a MessageUtil::hash() and compare
each, since filter chains that differ can share a hash. Share the
struct setup of the reserialized tests, fold the default filter chain
helper into addActiveListener, and count the changed benchmark's
drains before reading its iterations.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Rohit Agrawal <rohit.agrawal@databricks.com>

@wbpcode wbpcode left a comment

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.

LGTM. Thanks.

@agrawroh
agrawroh merged commit e77cca7 into envoyproxy:main Nov 22, 2026
26 checks passed
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.

4 participants