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.