Comments (9)
*b = *bytes.NewBuffer(buf)
works. The NewBuffer
call is inlined and the allocation inside it happens on the stack.
(Copy a struct of another package wholesale is generally frowned upon, but it works in this case.)
from go.
I still wonder if SetBytes
has value as an explicit API?
As prior precedence, we added the AvailableBuffer
method to make it more obvious how to correctly append into a bytes.Buffer
even though this functionally had always been possible by messing around with the capacity of the buffer returned by the Bytes
method.
from go.
Why not just reset? https://pkg.go.dev/bytes#Buffer.Reset
from go.
Why not just reset? https://pkg.go.dev/bytes#Buffer.Reset
Reset truncates the existing buffer. The use case here is that I have a byte slice already and I have a bytes.Buffer object and I'd like to create an io.Reader to read those bytes without allocating.
A legitimate claim is that the bytes.Reader API should work, because it indeed allows for resetting with a new slice. Where that turns out to not work is that bytes.Reader doesn't expose the underlying byte slice (it doesn't have a Bytes method).
from go.
*b = *bytes.NewBuffer(buf)
works. TheNewBuffer
call is inlined and the allocation inside it happens on the stack.(Copy a struct of another package wholesale is generally frowned upon, but it works in this case.)
Cool! I'll close this and hope that some future wayward soul may discover this trick.
from go.
*b = *bytes.NewBuffer(buf)
just blew my mind... I've been programming Go for 10 years and that didn't occur to me.
from go.
Anybody want to add a test to make sure it keeps working? It's not like it's documented.
from go.
I can add a test. I'm about to use this trick.
from go.
Change https://go.dev/cl/581297 mentions this issue: bytes: add test to ensure shallow copy of NewBuffer does not allocate
from go.
Related Issues (20)
- proposal: sync: On changing the sync.Map implementation to a Ctrie Map (HashTrieMap) HOT 1
- go/cmd: trimpath does not clean some CGO C filepaths HOT 5
- access: Approving CLs ("approvers")
- cmd/compile: changing a hot concrete method to interface method triggers a PGO ICE HOT 6
- cmd/compile: changing a hot concrete method to interface method triggers a PGO ICE [1.22 backport] HOT 2
- cmd/compile: Go 1.22.x failed to be bootstrapped from 386 to ppc64le [1.22 backport] HOT 2
- runtime/metrics: /memory/classes/heap/unused:bytes spikes HOT 9
- go/format cmd/gofmt: requires 2 pass for multiline comments with spacing HOT 3
- go test -timeout=50m0s ./...: unrecognized failures HOT 1
- x/tools/gopls: organizeImports chooses wrong import with 20+ peer package imports HOT 2
- runtime/pprof: TestLabelSystemstack failure on linux-arm HOT 4
- crypto/x509: certificate validation issues HOT 2
- cmd/compile: constraints that are impossible to satisfy are not caught HOT 3
- encoding/binary: fix NativeEndian == BigEndian || NativeEndian == LittleEndian HOT 6
- cmd/compile: internal compiler error: unexpected type: <nil> (<nil>) in for-range HOT 2
- x/net/quic: TestUDPSourceSpecified failures HOT 1
- x/image/draw: BiLinear.Scale shows GOARCH-dependent behaviour due to float operation differences HOT 1
- x/pkgsite: package removal request for [go.appmanch.org/commons] HOT 1
- proposal: x/net/http/httpguts: add IsToken HOT 1
- x/tools/refactor/eg: 'exec format error' Test failures HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go.