generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 4
feat: support grid read write buffer #45
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
Draft
jayantxie
wants to merge
7
commits into
main
Choose a base branch
from
feat/xwrite_buffer1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f8f57e
feat: xbuffer
jayantxie 28aa45e
feat: xbuffer
jayantxie 75b6f5e
feat: support grid read write buffer
jayantxie 27cb52e
feat: grid buffer tests
jayantxie 83a2233
feat: grid buffer tests
jayantxie b6645e3
feat: grid buffer tests
jayantxie 7f38c73
feat: optimize grid write buffer
jayantxie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // Copyright 2025 CloudWeGo Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package gridbuf | ||
|
|
||
| import ( | ||
| "errors" | ||
| "sync" | ||
|
|
||
| "github.com/bytedance/gopkg/lang/mcache" | ||
| ) | ||
|
|
||
| var ( | ||
| errReadBufferNotEnough = errors.New("error grid read buffer not enough") | ||
| readBufferPool = sync.Pool{ | ||
| New: func() interface{} { | ||
| return &ReadBuffer{ | ||
| pool: make([][]byte, 0, 16), | ||
| } | ||
| }, | ||
| } | ||
| ) | ||
|
|
||
| type ReadBuffer struct { | ||
| off int | ||
| chunk []byte | ||
| chunks [][]byte | ||
| pool [][]byte | ||
| } | ||
|
|
||
| func NewReadBuffer(bufs [][]byte) *ReadBuffer { | ||
| rb := readBufferPool.Get().(*ReadBuffer) | ||
| rb.chunk = bufs[0] | ||
| rb.chunks = bufs[1:] | ||
| return rb | ||
| } | ||
|
|
||
| // ReadN read n bytes from chunk, if chunk is not enough, it will read from next chunks. | ||
| // | ||
| // MAKE SURE IT CAN BE INLINE: | ||
| // `can inline (*XReadBuffer).ReadN with cost 80` | ||
| func (b *ReadBuffer) ReadN(n int) (buf []byte) { | ||
| buf = b.chunk[b.off:] | ||
| if len(buf) < n { | ||
| buf = b.readSlow(n) | ||
| } else { | ||
| b.off += n | ||
| } | ||
| return | ||
| } | ||
|
|
||
| func (b *ReadBuffer) readSlow(n int) (buf []byte) { | ||
| buf = mcache.Malloc(n) | ||
| b.pool = append(b.pool, buf) | ||
| var l, m int | ||
| if len(b.chunk)-b.off > 0 { | ||
| m = copy(buf[l:], b.chunk[b.off:]) | ||
| l += m | ||
| } | ||
| for l < n { | ||
| if len(b.chunks) == 0 { | ||
| panic(errReadBufferNotEnough.Error()) | ||
| } | ||
| b.chunk = b.chunks[0] | ||
| b.off = 0 | ||
| b.chunks = b.chunks[1:] | ||
| m = copy(buf[l:], b.chunk) | ||
| l += m | ||
| } | ||
| b.off += m | ||
| return | ||
| } | ||
|
|
||
| // CopyBytes copy bytes from chunk, if chunk is not enough, it will copy from next chunks. | ||
| // | ||
| // MAKE SURE IT CAN BE INLINE: | ||
| // `can inline (*XReadBuffer).CopyBytes with cost 80` | ||
| func (b *ReadBuffer) CopyBytes(buf []byte) { | ||
| n := copy(buf, b.chunk[b.off:]) | ||
| if len(buf) > n { | ||
| b.copySlow(buf) | ||
| } else { | ||
| b.off += n | ||
| } | ||
| } | ||
|
|
||
| func (b *ReadBuffer) copySlow(buf []byte) { | ||
| m := len(b.chunk) - b.off | ||
| l := m | ||
| for l < len(buf) { | ||
| if len(b.chunks) == 0 { | ||
| panic(errReadBufferNotEnough.Error()) | ||
| } | ||
| b.chunk = b.chunks[0] | ||
| b.off = 0 | ||
| b.chunks = b.chunks[1:] | ||
| m = copy(buf[l:], b.chunk) | ||
| l += m | ||
| } | ||
| b.off += m | ||
| } | ||
|
|
||
| func (b *ReadBuffer) Free() { | ||
| b.off = 0 | ||
| b.chunk = nil | ||
| b.chunks = nil | ||
| for i := range b.pool { | ||
| mcache.Free(b.pool[i]) | ||
| b.pool[i] = nil | ||
| } | ||
| b.pool = b.pool[:0] | ||
| readBufferPool.Put(b) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| // Copyright 2025 CloudWeGo Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package gridbuf | ||
jayantxie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "runtime/debug" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestReadBuf_Inline(t *testing.T) { | ||
| var x *ReadBuffer | ||
|
|
||
| defer func() { | ||
| r := recover() | ||
| if r == nil { | ||
| t.Fatal("should panic") | ||
| } | ||
| stack := string(debug.Stack()) | ||
| if !strings.Contains(stack, "ReadN(...)") { | ||
| t.Fatal("should inline ReadN") | ||
| } | ||
| }() | ||
| x.ReadN(10) | ||
|
|
||
| defer func() { | ||
| r := recover() | ||
| if r == nil { | ||
| t.Fatal("should panic") | ||
| } | ||
| stack := string(debug.Stack()) | ||
| if !strings.Contains(stack, "CopyBytes(...)") { | ||
| t.Fatal("should inline CopyBytes") | ||
| } | ||
| }() | ||
|
|
||
| x.CopyBytes(make([]byte, 1)) | ||
| } | ||
|
|
||
| func TestReadBuf_CrossPad(t *testing.T) { | ||
| tf := func(getBuf func(x *ReadBuffer, n int) []byte) { | ||
| ori1 := make([]byte, padLength) | ||
| for i := range ori1 { | ||
| ori1[i] = 'a' | ||
| } | ||
| ori2 := make([]byte, padLength) | ||
| for i := range ori2 { | ||
| ori2[i] = 'b' | ||
| } | ||
| ori := [][]byte{ori1, ori2} | ||
| x := NewReadBuffer(ori) | ||
| defer x.Free() | ||
| buf := getBuf(x, padLength-1) | ||
| if len(buf) < padLength-1 { | ||
| t.Fatal("buf length should be great or equal to padLength-1") | ||
| } | ||
| for _, byt := range buf[:padLength-1] { | ||
| if byt != 'a' { | ||
| t.Fatal("byt should be 'a'") | ||
| } | ||
| } | ||
| buf = getBuf(x, 2) | ||
| if len(buf) < 2 { | ||
| t.Fatal("buf length should be great or equal to 2") | ||
| } | ||
| if buf[0] != 'a' { | ||
| t.Fatal("buf[0] should be 'a'") | ||
| } | ||
| if buf[1] != 'b' { | ||
| t.Fatal("buf[1] should be 'b'") | ||
| } | ||
| buf = getBuf(x, padLength-1) | ||
| if len(buf) < padLength-1 { | ||
| t.Fatal("buf length should be great or equal to padLength-1") | ||
| } | ||
| for _, byt := range buf[:padLength-1] { | ||
| if byt != 'b' { | ||
| t.Fatal("byt should be 'b'") | ||
| } | ||
| } | ||
| } | ||
| tf(func(x *ReadBuffer, n int) []byte { | ||
| return x.ReadN(n) | ||
| }) | ||
| tf(func(x *ReadBuffer, n int) []byte { | ||
| buf := make([]byte, n) | ||
| x.CopyBytes(buf) | ||
| return buf | ||
| }) | ||
| } | ||
|
|
||
| func TestReadBuf_NoCrossPad(t *testing.T) { | ||
| tf := func(getBuf func(x *ReadBuffer, n int) []byte) { | ||
| ori1 := make([]byte, padLength/2) | ||
| for i := range ori1 { | ||
| ori1[i] = 'a' | ||
| } | ||
| ori2 := make([]byte, padLength/2) | ||
| for i := range ori2 { | ||
| ori2[i] = 'b' | ||
| } | ||
| ori := append(ori1, ori2...) | ||
| x := NewReadBuffer([][]byte{ori}) | ||
| defer x.Free() | ||
|
|
||
| buf := getBuf(x, padLength/2) | ||
| if len(buf) < padLength/2 { | ||
| t.Fatal("buf length should be great or equal to padLength/2") | ||
| } | ||
| for _, byt := range buf[:padLength/2] { | ||
| if byt != 'a' { | ||
| t.Fatal("byt should be 'a'") | ||
| } | ||
| } | ||
| buf = getBuf(x, padLength/2) | ||
| if len(buf) < padLength/2 { | ||
| t.Fatal("buf length should be great or equal to padLength/2") | ||
| } | ||
| for _, byt := range buf[:padLength/2] { | ||
| if byt != 'b' { | ||
| t.Fatal("byt should be 'b'") | ||
| } | ||
| } | ||
| } | ||
| tf(func(x *ReadBuffer, n int) []byte { | ||
| return x.ReadN(n) | ||
| }) | ||
| tf(func(x *ReadBuffer, n int) []byte { | ||
| buf := make([]byte, n) | ||
| x.CopyBytes(buf) | ||
| return buf | ||
| }) | ||
| } | ||
|
|
||
| func BenchmarkReadBuf_ReadN(b *testing.B) { | ||
| bytes := make([]byte, b.N) | ||
| buf := NewReadBuffer([][]byte{bytes}) | ||
| defer buf.Free() | ||
|
|
||
| var tmp []byte | ||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| tmp = buf.ReadN(1) | ||
| } | ||
| _ = tmp | ||
| } | ||
|
|
||
| func BenchmarkBytes_Read(b *testing.B) { | ||
| bytes := make([]byte, b.N) | ||
|
|
||
| var off int | ||
| var tmp []byte | ||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| tmp = bytes[off : off+1] | ||
| off++ | ||
| } | ||
| _ = tmp | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.