FAQ
Hello,

I have a quite large bytes slice (actually created with syscall.Mmap) and I
want to repetetivly create buffer around it and iterate over and over
again. I am using bytes.NewBuffer to create a reader around it. My question
is - does the factory function copy whole data in the memory, or does it
reuse the buffer? I am not sure from the construct if it does. Here is the
code bit:

http://golang.org/src/pkg/bytes/buffer.go?s=11578:11612#L370

func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} }

Many thanks

--

Search Discussions

  • Minux at Dec 30, 2012 at 3:59 pm

    On Sun, Dec 30, 2012 at 11:55 PM, Lukáš Zapletal wrote:
    I have a quite large bytes slice (actually created with syscall.Mmap) and
    I want to repetetivly create buffer around it and iterate over and over
    again. I am using bytes.NewBuffer to create a reader around it. My question
    is - does the factory function copy whole data in the memory, or does it
    reuse the buffer? I am not sure from the construct if it does. Here is the
    code bit:
    it won't copy the data, if will only allocate new memory if your capacity
    of your buf can't contain
    all the data you write into it.

    --
  • Bryanturley at Dec 30, 2012 at 4:02 pm

    On Sunday, December 30, 2012 9:55:53 AM UTC-6, Lukáš Zapletal wrote:
    Hello,

    I have a quite large bytes slice (actually created with syscall.Mmap) and
    I want to repetetivly create buffer around it and iterate over and over
    again. I am using bytes.NewBuffer to create a reader around it. My question
    is - does the factory function copy whole data in the memory, or does it
    reuse the buffer? I am not sure from the construct if it does. Here is the
    code bit:

    http://golang.org/src/pkg/bytes/buffer.go?s=11578:11612#L370

    func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} }

    Many thanks
    15 // A Buffer is a variable-sized buffer of bytes with Read and Write methods. 16 // The zero value for Buffer is an empty buffer ready to use. 17 type Buffer struct { 18 buf []byte // contents are the bytes buf[off : len(buf)] 19 off int // read at &buf[off], write at &buf[len(buf)] 20 runeBytes [utf8.UTFMax]byte // avoid allocation of slice on each WriteByte or Rune 21 bootstrap [64]byte // memory to hold first slice; helps small buffers (Printf) avoid allocation. 22 lastRead readOp // last read operation, so that Unread* can work correctly. 23 }

    I would say no, also it appears to spend alot of time not allocating new buffers if you read the comments in that type definition.
    I had no idea it had so many other little buffers in it... eww




    --
  • Tamás Gulácsi at Dec 31, 2012 at 6:52 am
    Use bytes.NewReader, and surely it won't allocate (no writes, thus no append and no resize happens).

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 30, '12 at 3:55p
activeDec 31, '12 at 6:52a
posts4
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase