On Friday, February 3, 2017 at 10:54:59 PM UTC+8, rog wrote:
>
> This program reports only one allocation per operation, which seems 
> to indicate that your solution is already the right one: 
>
> https://play.golang.org/p/Fx1EMYKRRd 
>
>
>
Yes, but only for small strings with length <= 32

https://play.golang.org/p/TfdahRKVr3

 

> On 1 February 2017 at 09:18, T L <tapi...@gmail.com <javascript:>> wrote: 
> > Sometimes, I want to create a new string which will not share the 
> underlying 
> > bytes with other strings. 
> > For example, to avoid the substring memory leak problem. 
> > 
> > newStr := string([]byte(oldStr)) is simple but will make two memory 
> > allocations, and one of them is a waste. 
> > 
> > There are two solutions which only make one memory allocation in 
> > https://groups.google.com/forum/#!topic/golang-nuts/naMCI9Jt6Qg 
> > (code: https://play.golang.org/p/3rVsMTaHUz). 
> > But in that thread, someone said the two are not safe from being 
> optimized 
> > unexpectedly in later compiler versions. 
> > 
> > I found the most approached function in the standard lib is 
> > strings.Repeat(), but it also makes two memory allocations. 
> > 
> > Is there a safe way to do this with only one memory allocation? 
> > 
> > 
> > 
> > -- 
> > 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...@googlegroups.com <javascript:>. 
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to