Coder Social home page Coder Social logo

Comments (9)

randall77 avatar randall77 commented on May 30, 2024 2

*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.

dsnet avatar dsnet commented on May 30, 2024 1

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.

seankhliao avatar seankhliao commented on May 30, 2024

Why not just reset? https://pkg.go.dev/bytes#Buffer.Reset

from go.

ajwerner avatar ajwerner commented on May 30, 2024

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.

ajwerner avatar ajwerner commented on May 30, 2024

*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.)

Cool! I'll close this and hope that some future wayward soul may discover this trick.

from go.

dsnet avatar dsnet commented on May 30, 2024

*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.

ianlancetaylor avatar ianlancetaylor commented on May 30, 2024

Anybody want to add a test to make sure it keeps working? It's not like it's documented.

from go.

dsnet avatar dsnet commented on May 30, 2024

I can add a test. I'm about to use this trick.

from go.

gopherbot avatar gopherbot commented on May 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.