summaryrefslogtreecommitdiffhomepage
path: root/pkg/buffer/buffer.go
diff options
context:
space:
mode:
authorTing-Yu Wang <anivia@google.com>2021-05-13 13:54:04 -0700
committergVisor bot <gvisor-bot@google.com>2021-05-13 13:56:16 -0700
commit84f04cc858644e9748a82f33b834a84c8b0fc934 (patch)
tree011d6915a666ea978a7b5efb7397757cef3370e0 /pkg/buffer/buffer.go
parentbaa0888f114c586ea490d49a23c3d828fd739b85 (diff)
Migrate PacketBuffer to use pkg/buffer
Benchmark iperf3: Before After native->runsc 5.14 5.01 (Gbps) runsc->native 4.15 4.07 (Gbps) It did introduce overhead, mainly at the bridge between pkg/buffer and VectorisedView, the ExtractVV method. Once endpoints start migrating away from VV, this overhead will be gone. Updates #2404 PiperOrigin-RevId: 373651666
Diffstat (limited to 'pkg/buffer/buffer.go')
-rw-r--r--pkg/buffer/buffer.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/pkg/buffer/buffer.go b/pkg/buffer/buffer.go
index 311808ae9..5b77a6a3f 100644
--- a/pkg/buffer/buffer.go
+++ b/pkg/buffer/buffer.go
@@ -33,12 +33,40 @@ func (b *buffer) init(size int) {
b.data = make([]byte, size)
}
+// initWithData initializes b with data, taking ownership.
+func (b *buffer) initWithData(data []byte) {
+ b.data = data
+ b.read = 0
+ b.write = len(data)
+}
+
// Reset resets read and write locations, effectively emptying the buffer.
func (b *buffer) Reset() {
b.read = 0
b.write = 0
}
+// Remove removes r from the unread portion. It returns false if r does not
+// fully reside in b.
+func (b *buffer) Remove(r Range) bool {
+ sz := b.ReadSize()
+ switch {
+ case r.Len() != r.Intersect(Range{end: sz}).Len():
+ return false
+ case r.Len() == 0:
+ // Noop
+ case r.begin == 0:
+ b.read += r.end
+ case r.end == sz:
+ b.write -= r.Len()
+ default:
+ // Remove from the middle of b.data.
+ copy(b.data[b.read+r.begin:], b.data[b.read+r.end:b.write])
+ b.write -= r.Len()
+ }
+ return true
+}
+
// Full indicates the buffer is full.
//
// This indicates there is no capacity left to write.