Skip to content

perf: avoid copying topology tokens - #237

Merged
ovaistariq merged 2 commits into
mainfrom
perfloop-pr-open-eyw0b53bz0
Aug 22, 2026
Merged

ovaistariq merged 2 commits into
mainfrom
perfloop-pr-open-eyw0b53bz0

Conversation

@perfloop

@perfloop perfloop Bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

GetNodeTokens now returns each normalized token slice from the current ring snapshot instead of allocating and sorting a copy for every topology response. Ring updates replace snapshots, and the current coordinator and protobuf response path only read the slices.

The bufconn RPC benchmark covers 1, 3, and 16 active members at 128, 512, and 4096 tokens. The race regression marshals topology data while memberlist publishes replacements. This relies on snapshot replacement and read-only consumers; a mutating consumer or non-memberlist KV backend must copy the slice.

Verification

  • make install-protoc-plugins && PATH="$(go env GOPATH)/bin:$PATH" make test-coordinator
  • make install-protoc-plugins && PATH="$(go env GOPATH)/bin:$PATH" GOFLAGS=-race make test-coordinator TEST=TestGetNodeTokens_ResponseSlicesRemainStableDuringRingUpdates

Workload: CacheService.GetTopology RPC over bufconn with 1 active member and 512 tokens per member

Metric Before After Change
B/op 16856 14752 12.5% lower

Workload: CacheService.GetTopology RPC over bufconn with 1 active member and 4096 tokens per member

Metric Before After Change
B/op 43414 26971 37.9% lower

Workload: CacheService.GetTopology RPC over bufconn with 3 active members and 128 tokens per member

Metric Before After Change
B/op 17272 15568 9.9% lower

Workload: CacheService.GetTopology RPC over bufconn with 3 active members and 512 tokens per member

Metric Before After Change
B/op 24627 18314 25.6% lower

Workload: CacheService.GetTopology RPC over bufconn with 3 active members and 4096 tokens per member

Metric Before After Change
B/op 110863 61533 44.5% lower

Workload: CacheService.GetTopology RPC over bufconn with 16 active members and 128 tokens per member

Metric Before After Change
B/op 42300 33211 21.5% lower

Workload: CacheService.GetTopology RPC over bufconn with 16 active members and 512 tokens per member

Metric Before After Change
B/op 92716 59045 36.3% lower

Workload: CacheService.GetTopology RPC over bufconn with 16 active members and 4096 tokens per member

Metric Before After Change
B/op 553123 290044 47.6% lower
Checks: 2 passed.

Generated by Perfloop. Measurements and checks.


Note

Medium Risk
Aliasing ring token slices is a concurrency/correctness change: mutating consumers or KV backends that edit slices in place would corrupt topology responses. Coverage is limited to memberlist snapshot replacement and protobuf marshal.

Overview
GetNodeTokens now returns the current ring snapshot’s token slices instead of allocating and sorting a copy on every topology response. Callers must treat those slices as read-only; ring updates replace the snapshot rather than mutating it in place.

Adds a concurrent marshal-vs-memberlist-update regression test and a tagged GetTopology bufconn benchmark (1/3/16 members × 128/512/4096 tokens), plus a Makefile target to compile that bench without pulling in the rest of CacheService.

Reviewed by Cursor Bugbot for commit d71a405. Bugbot is set up for automated code reviews on this repo. Configure here.

@greptile-apps

greptile-apps Bot commented Aug 22, 2026

Copy link
Copy Markdown

Greptile Summary

This PR avoids copying and sorting token slices for every topology response by returning read-only slices from the current memberlist ring snapshot.

  • Moves the topology RPC and shared CacheService definition into a topology-only compilation unit.
  • Adds memberlist snapshot-concurrency regression coverage.
  • Adds a build-tagged, bufconn-based topology RPC benchmark and Makefile build target.

Confidence Score: 5/5

The PR appears safe to merge with no concrete correctness, build, or security defects identified.

Current topology consumers do not mutate returned token slices, memberlist snapshot replacement is covered under concurrent marshaling, and clients independently normalize token ordering before routing.

Important Files Changed

Filename Overview
coordinator/ring/ring.go Returns normalized token slices directly from the ring snapshot; current production consumption is read-only and the memberlist update path receives dedicated concurrency coverage.
coordinator/ring/topology_test.go Exercises concurrent topology marshaling and memberlist snapshot replacement while validating complete, internally consistent token assignments.
server/service/topology.go Extracts the topology RPC and shared service type without changing production RPC behavior.
server/service/service.go Moves storage-specific service state behind an embedded data struct to permit a lightweight benchmark-tag build.
server/service/topology_benchmark_test.go Benchmarks the production topology RPC over an in-process bufconn transport across the configured member/token matrix.
coordinator/ring/topology_benchmark.go Creates and safely cleans up an in-memory active ring fixture for topology benchmarks.
Makefile Adds a dedicated target that compiles the build-tagged topology RPC benchmark.

Reviews (1): Last reviewed commit: "perf: avoid copying topology tokens" | Re-trigger Greptile

@perfloop
perfloop Bot force-pushed the perfloop-pr-open-eyw0b53bz0 branch from 934f1f4 to fcc90fa Compare August 22, 2026 06:39
@perfloop
perfloop Bot force-pushed the perfloop-pr-open-eyw0b53bz0 branch from fcc90fa to d71a405 Compare August 22, 2026 21:26
@ovaistariq
ovaistariq merged commit 9f7dd99 into main Aug 22, 2026
13 checks passed
@ovaistariq
ovaistariq deleted the perfloop-pr-open-eyw0b53bz0 branch August 22, 2026 23:06
@tigrisdata-argocd-bot

Copy link
Copy Markdown

🎉 This PR is included in version 1.12.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants