On Fri, Apr 30, 2021 at 10:51 AM 'Valentin Deleplace' via golang-nuts <golang-nuts@googlegroups.com> 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" > return s > } > > The compiled assembly respects the apparent instructions of the source code, > with g calling runtime.concatstring2, while f does not (f benefits from an > optimization that transforms "a"+"b" into the constant "ab"). > > 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?
I agree that the compiler ought to be able to generate the same code for both cases. But I'll note that from a language perspective, the functions are quite different. "a" + "b" is a constant expression (https://golang.org/ref/spec#Constant_expressions). The language requires that "a" + "b" be evaluated at compile time. There is no such requirement for the function g. That would require a more complicated compiler optimization. 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcUt%3DSu3mMG%2B3L3Ys73Ta333R%2BR7SuFe5ONjjco%2Bq4Ez3w%40mail.gmail.com.