-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lightning/backend/external: prevent escaping buffered slice to heap #48044
Conversation
Skipping CI for Draft Pull Request. |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Hi @tangenta. Thanks for your PR. PRs from untrusted users cannot be marked as trusted with I understand the commands that are listed here. 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/test-infra repository. |
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## master #48044 +/- ##
================================================
+ Coverage 71.6271% 72.7733% +1.1462%
================================================
Files 1401 1421 +20
Lines 405768 411641 +5873
================================================
+ Hits 290640 299565 +8925
+ Misses 95337 93223 -2114
+ Partials 19791 18853 -938
Flags with carried forward coverage won't be shown. Click here to find out more.
|
@tangenta: The following test failed, say
Full PR test history. Your PR dashboard. 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/test-infra repository. I understand the commands that are listed here. |
} | ||
|
||
type slice struct { | ||
buf []byte |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
slice
has more members than a []byte
, why it's helpful to escaping 😵
b := r.next(n) | ||
readLen := len(b) | ||
if readLen == n { | ||
ret := &b | ||
ret := &slice{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe return r.curBuf[originOffset:readLen]
? I will test it in my local environment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about the improvements of this PR. Seems there must be something that escapes to heap
go build -gcflags="-m=2"
...
./byte_reader.go:204:10: &slice{...} escapes to heap:
./byte_reader.go:204:10: flow: ret = &{storage for &slice{...}}:
./byte_reader.go:204:10: from &slice{...} (spill) at ./byte_reader.go:204:10
./byte_reader.go:204:10: from ret := &slice{...} (assign) at ./byte_reader.go:204:7
./byte_reader.go:204:10: flow: {heap} = ret:
./byte_reader.go:204:10: from append(r.retPointers, ret) (call parameter) at ./byte_reader.go:209:25
What problem does this PR solve?
Issue Number: ref #47757
Problem Summary:
tidb/br/pkg/lightning/backend/external/byte_reader.go
Lines 199 to 206 in 30288c7
The buffered slice is unexpectedly moved to heap, leading to unnecessary memory allocation for each key.
According to verbose escape analysis,
b
escapes because we assign the address of local variables to a slice on heap:What is changed and how it works?
This PR stores the buffer's start offset and length instead of the address. Although it looks like boilerplate, it is worth to reduce the memory allocation.
After this PR,
b
does not escape anymore:And here is the memory usage comparison:
Check List
Tests
See above screenshot.
Side effects
Documentation
Release note
Please refer to Release Notes Language Style Guide to write a quality release note.