Re: [go-nuts] How string constants are propagated inside a small func

2021-05-04 Thread Jesper Louis Andersen
On Sat, May 1, 2021 at 1:28 PM Jesper Louis Andersen < jesper.louis.ander...@gmail.com> wrote: > > func g() string { > s1 := "a" > s2 = s1 + "b" > return s > } > > Errata: it should have been `return s2` rather than `return s`. I hope I didn't confuse people too much. --

Re: [go-nuts] How string constants are propagated inside a small func

2021-05-03 Thread 'Valentin Deleplace' via golang-nuts
Thank you all for the insights On Saturday, May 1, 2021 at 1:29:15 PM UTC+2 jesper.lou...@gmail.com wrote: > On Fri, Apr 30, 2021 at 7:51 PM 'Valentin Deleplace' via golang-nuts < > golan...@googlegroups.com> wrote: > >> I don't know exactly what SSA does in the compiler, but I thought it >> wou

Re: [go-nuts] How string constants are propagated inside a small func

2021-05-01 Thread Jesper Louis Andersen
On Fri, Apr 30, 2021 at 7:51 PM 'Valentin Deleplace' via golang-nuts < golang-nuts@googlegroups.com> wrote: > I don't know exactly what SSA does in the compiler, but I thought it would > be capable of optimizing f and g into the exact same generated code. Am I > missing something? > Roughly, SSA

Re: [go-nuts] How string constants are propagated inside a small func

2021-04-30 Thread Ian Lance Taylor
On Fri, Apr 30, 2021 at 10:51 AM 'Valentin Deleplace' via golang-nuts wrote: > > Hi, I was surprised that the funcs f and g do not generate the same assembly > code: > > func f() string { > s := "a" + "b" > return s > } > > func g() string { > s := "a" > s += "b" >

Re: [go-nuts] How string constants are propagated inside a small func

2021-04-30 Thread Jan Mercl
On Fri, Apr 30, 2021 at 7:51 PM 'Valentin Deleplace' via golang-nuts wrote: > func f() string { > s := "a" + "b" > return s > } Handled by constant folding. > func g() string { > s := "a" > s += "b" > return s > } The second assignment to 's' here is not

[go-nuts] How string constants are propagated inside a small func

2021-04-30 Thread 'Valentin Deleplace' via golang-nuts
Hi, I was surprised that the funcs f and g do not generate the same assembly code: func f() string { s := "a" + "b" return s } func g() string { s := "a" s += "b" return s } The compiled assembly respects the apparent instructions of the source code, wit