Skip to content

net: pool bufio reader and writer to reduce memory allocation (#776)#1085

Merged
ti-chi-bot[bot] merged 1 commit intopingcap:mainfrom
Samriddha9619:fix/issue-776-bufio-pool
Mar 18, 2026
Merged

net: pool bufio reader and writer to reduce memory allocation (#776)#1085
ti-chi-bot[bot] merged 1 commit intopingcap:mainfrom
Samriddha9619:fix/issue-776-bufio-pool

Conversation

@Samriddha9619
Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: close #776

Problem Summary:
In short-lived connection workloads, buffered IO is created frequently, leading to heavy memory allocation and GC pressure.

What is changed and how it works:

  • Implemented sync.Pool for bufio.Reader and bufio.Writer in basicReadWriter.
  • Added a pooled flag to safely manage the buffer lifecycle.
  • Implemented freeBasicReadWriter to recursively unwrap connection layers (TLS, compression) and return standard-sized buffers to the pool on connection close to prevent memory leaks.

Check List

Tests - [x] Unit test

  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Notable changes

  • Has configuration change
  • Has HTTP API interfaces change
  • Has tiproxyctl change
  • Other user behavior changes

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

Optimize memory allocation by pooling buffered IO for client connections

@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented Feb 24, 2026

Hi @Samriddha9619. Thanks for your PR.

I'm waiting for a pingcap member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented Feb 24, 2026

Welcome @Samriddha9619! It looks like this is your first PR to pingcap/tiproxy 🎉

@pingcap-cla-assistant
Copy link
Copy Markdown

pingcap-cla-assistant Bot commented Feb 24, 2026

CLA assistant check
All committers have signed the CLA.

@ti-chi-bot ti-chi-bot Bot added the size/L label Feb 24, 2026
@djshow832
Copy link
Copy Markdown
Collaborator

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9aba8c4de5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread pkg/proxy/net/packetio.go Outdated
errs = append(errs, err)
}
*/
freeBasicReadWriter(p.readWriter)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Avoid pooling buffers before closing active packet I/O

Close now returns the underlying basicReadWriter buffers to sync.Pool before the connection is actually closed, which mutates bufio.Reader/Writer state (Reset(nil)) while other goroutines may still be in ReadPacket/WritePacket; this happens in normal shutdown paths where SQLServer.Close can call ClientConnection.Close while processMsg is still reading, and onConn's deferred Close can race as well. That can lead to races/panics and even duplicate Put of the same reader/writer, allowing later connections to share a single buffer instance and corrupt traffic.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe overwrite basicReadWriter.Close()?

func (brw *basicReadWriter) Close() error {
    err := brw.Conn.Close()
    brw.Free()
    return err
}

@Samriddha9619 Samriddha9619 force-pushed the fix/issue-776-bufio-pool branch from 9aba8c4 to e69d439 Compare March 3, 2026 14:16
@chatgpt-codex-connector
Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@Samriddha9619 Samriddha9619 force-pushed the fix/issue-776-bufio-pool branch 2 times, most recently from 12bd672 to b5903d0 Compare March 3, 2026 14:26
@Samriddha9619 Samriddha9619 requested a review from djshow832 March 3, 2026 14:27
Comment thread pkg/proxy/net/packetio.go Outdated
brw := &basicReadWriter{
Conn: conn,
}
if bufferSize == DefaultConnBufferSize {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm wondering how to take full advantage of the buffer pool.
If the global buffer size config changes, all the memory in the pool is useless and becomes a leak. Meanwhile, subsequent allocations can't use the pool.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What if we check the capacity on Get() using the built-in .Size() method? If the buffer from the pool doesn't match the current bufferSize config, we can just discard it and allocate a fresh one.
Does this approach look good? I will verify and update the PR as soon as I get some time

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good idea! Please update it as you propose.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated the PR and divided the code into helper functions too

@Samriddha9619 Samriddha9619 force-pushed the fix/issue-776-bufio-pool branch from b5903d0 to 8a3fcde Compare March 18, 2026 05:11
@Samriddha9619 Samriddha9619 requested a review from djshow832 March 18, 2026 05:16
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented Mar 18, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: djshow832

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot Bot added the lgtm label Mar 18, 2026
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented Mar 18, 2026

[LGTM Timeline notifier]

Timeline:

  • 2026-03-18 09:33:19.032429624 +0000 UTC m=+349526.120087181: ☑️ agreed by djshow832.

@ti-chi-bot ti-chi-bot Bot added the approved label Mar 18, 2026
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@ca24d03). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1085   +/-   ##
=======================================
  Coverage        ?   66.80%           
=======================================
  Files           ?      141           
  Lines           ?    14658           
  Branches        ?        0           
=======================================
  Hits            ?     9792           
  Misses          ?     4194           
  Partials        ?      672           
Flag Coverage Δ
unit 66.80% <100.00%> (?)

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

@ti-chi-bot ti-chi-bot Bot merged commit a125fba into pingcap:main Mar 18, 2026
6 checks passed
@Samriddha9619 Samriddha9619 deleted the fix/issue-776-bufio-pool branch March 19, 2026 12:37
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.

Pool buffered reader or writer for better performance

3 participants