FAQ
Sometimes a program needs to allocate relatively small byte slices
that have a short lifespan. Using make() works, but it creates
garbage, which could become a problem if these allocations are
frequent. Just to play with the idea, I wrote a very simple lock-free
allocator that can manage at most 64 slices in a fixed-size buffer.
The code is here:

http://play.golang.org/p/2pYTAUJoGt

The typical use pattern is to Alloc a slice from the heap and defer a
call to Free. It's faster than make in the average case (slows down a
bit if the search has to go to the end of h.used) and doesn't leave
any garbage behind.

One place where I think this might be useful is in cgo programs that
have to make frequent conversions between string and *C.char. The
standard way of doing this with C.CString/C.free is even more
expensive than make(). You can use s+"\x00" to have Go create a
null-terminated copy and then a bit of unsafe trickery to get a C.char
pointer to it, but this creates garbage.

Bug reports and ideas for improvement are welcome.

- Max

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Christoph Hack at Jun 3, 2013 at 7:59 pm
    Dmitry has recently implemented something similar. The upcoming Pool /
    Cache type of the "sync" / "container/alloc" / etc. package (the type name
    and the package name are currently under discussion...) will provide a
    similar functionality. It's implemented as part of the runtime, so that it
    can reduce the amount of shared memory by using thread local pools (in
    addition to a global one) and more relaxed memory operations. I'm already
    waiting eagerly for the final commit :)

    Am Montag, 3. Juni 2013 20:11:40 UTC+2 schrieb Maxim Khitrov:
    Sometimes a program needs to allocate relatively small byte slices
    that have a short lifespan. Using make() works, but it creates
    garbage, which could become a problem if these allocations are
    frequent. Just to play with the idea, I wrote a very simple lock-free
    allocator that can manage at most 64 slices in a fixed-size buffer.
    The code is here:

    http://play.golang.org/p/2pYTAUJoGt

    The typical use pattern is to Alloc a slice from the heap and defer a
    call to Free. It's faster than make in the average case (slows down a
    bit if the search has to go to the end of h.used) and doesn't leave
    any garbage behind.

    One place where I think this might be useful is in cgo programs that
    have to make frequent conversions between string and *C.char. The
    standard way of doing this with C.CString/C.free is even more
    expensive than make(). You can use s+"\x00" to have Go create a
    null-terminated copy and then a bit of unsafe trickery to get a C.char
    pointer to it, but this creates garbage.

    Bug reports and ideas for improvement are welcome.

    - Max
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 3, '13 at 6:12p
activeJun 3, '13 at 7:59p
posts2
users2
websitegolang.org

2 users in discussion

Christoph Hack: 1 post Maxim Khitrov: 1 post

People

Translate

site design / logo © 2022 Grokbase