Skip to content

perf(chainlib): send response bodies as bytes via fiber.Ctx.Send#2270

Merged
nimrod-teich merged 1 commit intomainfrom
perf/fiber-send-bytes
Apr 16, 2026
Merged

perf(chainlib): send response bodies as bytes via fiber.Ctx.Send#2270
nimrod-teich merged 1 commit intomainfrom
perf/fiber-send-bytes

Conversation

@NadavLevi
Copy link
Copy Markdown
Collaborator

addHeadersAndSendString called fiber.Ctx.SendString, which copies the string into fasthttp's response bytebufferpool. All callers in the relay path already hold the body as []byte (reply.Data) or as JSON marshal output (also []byte) and were round-tripping through string() at the call site. That string() conversion is a full per-request copy on top of the unavoidable copy into fasthttp's buffer — visible in pprof as 96 GB inuse under bytebufferpool.(*ByteBuffer).WriteString on the eth router.

Rename to addHeadersAndSendBytes, take []byte, and use fiber.Ctx.Send. Update every caller (jsonRPC, tendermintRPC, rest) to drop the string() round-trip on success paths where reply.Data is already []byte; error paths keep []byte(s) on the small error strings since those aren't on the hot path. Add focused tests covering body + metadata headers, the empty-body case, and a binary payload round-trip to guard against a regression that reintroduces the string() conversion.

Description

Closes: #XXXX


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • read the contribution guide
  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the main branch
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 14, 2026

Codecov Report

❌ Patch coverage is 12.50000% with 14 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
protocol/chainlib/tendermintRPC.go 0.00% 6 Missing ⚠️
protocol/chainlib/rest.go 0.00% 4 Missing ⚠️
protocol/chainlib/jsonRPC.go 0.00% 3 Missing ⚠️
protocol/chainlib/common.go 66.66% 1 Missing ⚠️
Flag Coverage Δ
consensus 8.96% <ø> (ø)
protocol 35.31% <12.50%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
protocol/chainlib/common.go 60.44% <66.66%> (+2.80%) ⬆️
protocol/chainlib/jsonRPC.go 44.56% <0.00%> (ø)
protocol/chainlib/rest.go 42.99% <0.00%> (ø)
protocol/chainlib/tendermintRPC.go 40.74% <0.00%> (+0.15%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 14, 2026

Test Results

0 tests  ±0   0 ✅ ±0   0s ⏱️ ±0s
0 suites ±0   0 💤 ±0 
7 files   ±0   0 ❌ ±0 

Results for commit 9034038. ± Comparison against base commit 3f1baeb.

♻️ This comment has been updated with latest results.

@NadavLevi NadavLevi requested a review from avitenzer April 15, 2026 06:37
Comment thread protocol/chainlib/common.go
@NadavLevi NadavLevi force-pushed the perf/fiber-send-bytes branch from fc41b0f to 7cb5445 Compare April 15, 2026 17:02
@NadavLevi NadavLevi requested a review from avitenzer April 15, 2026 17:07
avitenzer
avitenzer previously approved these changes Apr 16, 2026
addHeadersAndSendString called fiber.Ctx.SendString, which copies the string
into fasthttp's response bytebufferpool. All callers in the relay path already
hold the body as []byte (reply.Data) or as JSON marshal output (also []byte)
and were round-tripping through string() at the call site. That string()
conversion is a full per-request copy on top of the unavoidable copy into
fasthttp's buffer — visible in pprof as 96 GB inuse under
bytebufferpool.(*ByteBuffer).WriteString on the eth router.

Rename to addHeadersAndSendBytes, take []byte, and use fiber.Ctx.Send. Update
every caller (jsonRPC, tendermintRPC, rest) to drop the string() round-trip on
success paths where reply.Data is already []byte; error paths keep []byte(s)
on the small error strings since those aren't on the hot path. Add focused
tests covering body + metadata headers, the empty-body case, and a binary
payload round-trip to guard against a regression that reintroduces the
string() conversion.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@NadavLevi NadavLevi force-pushed the perf/fiber-send-bytes branch from 7cb5445 to 9034038 Compare April 16, 2026 09:44
@nimrod-teich nimrod-teich merged commit 5921aa2 into main Apr 16, 2026
33 of 38 checks passed
@nimrod-teich nimrod-teich deleted the perf/fiber-send-bytes branch April 16, 2026 11:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants