Re: [go-nuts] efficiency of string <-> []byte conversion

2017-02-14 Thread martisch
some more uses:

string([]byte):

// Some internal compiler optimizations use this function. 
// - Used for m[string(k)] lookup where m is a string-keyed map and k is a 
[]byte. 
// - Used for "<"+string(b)+">" concatenation where b is []byte. 
// - Used for string(b)=="foo" comparison where b is []byte.

https://github.com/golang/go/blob/master/src/runtime/string.go#L125
https://github.com/golang/go/blob/master/src/cmd/compile/internal/gc/walk.go#L1448

the other way around:

[]byte(string)

// The only such case today is: 
// for i, c := range []byte(string)

https://github.com/golang/go/blob/master/src/cmd/compile/internal/gc/walk.go#L1485

Martin


On Tuesday, February 14, 2017 at 12:39:40 AM UTC+1, Ian Lance Taylor wrote:
>
> On Mon, Feb 13, 2017 at 12:31 PM, Alex Flint  > wrote: 
> > 
> > As of go1.8, do conversions between strings and byte slices always 
> generate 
> > a copy? 
>
> Usually but not absolutely always. 
>
> The gc compiler has an optimization for map lookups.  For a 
> map[string]T, when s is a []byte, m[string(s)] will not make a copy. 
>
> I'm not aware of any other similar slice <-> string optimization in 
> the gc compiler.  There could be some that I don't know about. 
>
> Ian 
>

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


Re: [go-nuts] efficiency of string <-> []byte conversion

2017-02-13 Thread Ian Lance Taylor
On Mon, Feb 13, 2017 at 12:31 PM, Alex Flint  wrote:
>
> As of go1.8, do conversions between strings and byte slices always generate
> a copy?

Usually but not absolutely always.

The gc compiler has an optimization for map lookups.  For a
map[string]T, when s is a []byte, m[string(s)] will not make a copy.

I'm not aware of any other similar slice <-> string optimization in
the gc compiler.  There could be some that I don't know about.

Ian

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


[go-nuts] efficiency of string <-> []byte conversion

2017-02-13 Thread Alex Flint
As of go1.8, do conversions between strings and byte slices always generate 
a copy?

I understand the immutability guarantees in the language spec, but I'm 
interested in this from an efficiency standpoint. I can imagine a compiler 
that analyzes whether a byte slice created from such a conversion is ever 
modified and foregoing the copy in some cases, while still adhering to the 
immutability guarantees.

I have searched this forum for a past thread on this topic but have come up 
empty. Feel free to point me to the relevant discussion.

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