News for hackers. Stuff that matters.
Re:Benefits of named return values in Go by edsrzf on Thursday June 29, 2017 @11:53PM (#14668538)
I normally hate it when people immediately trot out the old "premature optimization" quote, but it really applies here.

Please don't go around naming all your returns just because today's compiler happens to generate better code with them. This is a compiler issue that I'm confident will be fixed one day, especially if you do the right thing and file an issue.

But by all means, if you're profiling and your inner loops are actually slowed down by this, then make the change. And add a comment so that someone might be able to change it back some day when the compiler's improved.

Re:Benefits of named return values in Go by bradfitz on Friday June 30, 2017 @12:12AM (#14668595)
I filed a Go bug: https://github.com/golang/go/issues/20859

We should just fix the compiler.

Re:Benefits of named return values in Go by runeks on Friday June 30, 2017 @12:57AM (#14668719)
I'm surprised Go doesn't compile down to an IR language where these differences in syntax are represented in a single manner. Seems like different ways to write the same thing.
Re:Benefits of named return values in Go by bradfitz on Friday June 30, 2017 @01:10AM (#14668755)
It increasingly does.

But it's been a process.

Go 1.5 was the first self-hosting release, with the Go compiler auto-translated from C to Go. But it was still fundamentally Ken's C compiler in Go syntax.

Every release since (Go 1.6, Go 1.7, Go 1.8, Go 1.9) has been cleaning it up and making it more Go like and less C like.

Meanwhile, the backend was also retrofitted. Go 1.7 included an SSA backend for amd64 (https://golang.org/doc/go1.7#compiler).

Go 1.8 included it for all architectures and added more SSA goodness.

Go 1.9 adds yet more, but some things are still not pushed down into SSA as well as they could be. (e.g. https://github.com/golang/go/issues/5147#issuecomment-247685...)

Nowadays we can add optimizations much more easily, including writing matching rules like in https://github.com/golang/go/blob/master/src/cmd/compile/int...

Meanwhile the whole toolchain keeps getting cleaned up and more hackable.

In Go 1.9, the compiler is now parallelized, which would've been impossible earlier. (https://tip.golang.org/doc/go1.9#parallel-compile)

So, it keeps improving. Just remember the Hello World compiler we started with.

Also amusing in retrospect is that when Go first came out, despite having a very basic compiler at the time, people coming from scripting languages thought we were so fast.

Re:Benefits of named return values in Go by pcwalton on Friday June 30, 2017 @03:14AM (#14669090)
Just for what it's worth, the way I'd fix that problem in the compiler would be to implement dead store elimination via global value numbering. With trivial alias analysis, the compiler would be able to detect that the result of the "duffzero" instruction (which I assume is a memset) is always killed by the "duffcopy" instructions and would eliminate it.

See this article for how it's done in LLVM: http://blog.llvm.org/2009/12/introduction-to-load-eliminatio...

Re:Benefits of named return values in Go by heythere124 on Friday June 30, 2017 @07:02AM (#14669841)
Not my area of expertise and it is yours, but if you eliminate a zero instruction before a copy instruction how can you be sure that doesn't affect other threads?

  var x Int
  // Pass x to a thread by reference
  x = 0
  time.Sleep(1000)
  x = 1
Re:Benefits of named return values in Go by the8472 on Friday June 30, 2017 @08:05AM (#14670168)
How can you be sure that the other threads ever see it in time? they might be suspended for a whole second because a HDD needs to spin up or something like that.

So threads never seeing the value is already a valid outcome, so the compiler might as well always do that.

Re:Benefits of named return values in Go by heythere124 on Friday June 30, 2017 @08:18AM (#14670228)
time.Sleep(1000) could be replaced with a real syncing mechanism of some kind -- it's only for illustration.
Re:Benefits of named return values in Go by the8472 on Friday June 30, 2017 @12:46PM (#14671900)
a real syncing mechanism would introduce compiler barriers that prevent such an optimization.
Re:Benefits of named return values in Go by Filligree on Friday June 30, 2017 @07:59AM (#14670136)
The answer to that one would be to embed thread-safety in the type system, aka. Rust.

For languages with less sophisticated type systems you get a choice between inefficiency (Go), or complicated rules which state that the programmer is wrong for coding that way (C).

Re:Benefits of named return values in Go by heythere124 on Friday June 30, 2017 @08:25AM (#14670263)
Your "solution" is possible in many languages. It's just to give threads complete ownership of data they use. It doesn't require a special type system.

In general I don't think Rust actually adds much abstraction that isn't already in say Python. What it does is enforce tight constraints.

Re:Benefits of named return values in Go by Filligree on Friday June 30, 2017 @09:19AM (#14670496)
The constraints are important. They're what let you code safely, and also give the compiler the ability to optimize better.
Re:Benefits of named return values in Go by heythere124 on Friday June 30, 2017 @10:09AM (#14670806)
Not sure those optimisations are worth much. And it's only safety by forcing lowest common denominator code and making you justify everything to a dumb compiler. Rust serves a niche, but it is a tight niche IMO.
Re:Benefits of named return values in Go by pcwalton on Friday June 30, 2017 @12:44PM (#14671889)
Aliasing info is hugely important in optimization. In fact, it's needed to solve this very bug.

Aliasing info is, fundamentally, what allows Rust to have memory safety without GC.

Re:Benefits of named return values in Go by pcwalton on Friday June 30, 2017 @11:55AM (#14671527)
The memory dependence analysis must prove the memory is unaliased, which ensures among other things that no other thread can have a reference to it. Presumably in Go return pointers are guaranteed to be unaliased.
Re:Benefits of named return values in Go by dom0 on Friday June 30, 2017 @04:39AM (#14669373)
Why does Go not use LLVM? Are there technical reasons to reinvent the wheel, or is it just because LLVM is Apple's pet?
Re:Benefits of named return values in Go by _pctq on Friday June 30, 2017 @07:49AM (#14670084)
I hope this won't come out harsher than I intend to, but I'm so tired to hear this expression "not reinventing the wheel" to justify using third party code. This is not what it means.

Note that there is not a single wheel that was built once in prehistory and now every human gets it lent when they need it. People build wheels everyday to fit their needs, reusing the concept of wheel, that is, knowing that a circular object allows for smooth movements with less friction. The analogy in software development means that you've better know of designs that help you solve your problem, not that you should blindly use code built by someone else to bypass the whole problem solving. This is basically trying to use a bicycle wheel for everything. This may work well on an other bicycle, not on a car.

Re:Benefits of named return values in Go by bradfitz on Friday June 30, 2017 @11:49AM (#14671483)
See this answer from Russ Cox: https://news.ycombinator.com/item?id=8817990
Re:Benefits of named return values in Go by dom0 on Friday June 30, 2017 @01:31PM (#14672265)
Thank you for an insightful answer.
Re:Benefits of named return values in Go by pkroll on Friday June 30, 2017 @05:21AM (#14669517)
First part of the "Implementation" section: they thought it was too large and slow for their compiler speed goals.

https://golang.org/doc/faq#Implementation

Re:Benefits of named return values in Go by weberc2 on Friday June 30, 2017 @01:08AM (#14668749)
I'm pretty sure Go does compile down to an IR, but it's little more than an abstraction over different architectures. I could be wrong.
Re:Benefits of named return values in Go by hornetblack on Friday June 30, 2017 @04:38AM (#14669367)
Originally it compiled to Plan9 assembly which is cross platform. They have an SSA backend for some architectures now.
Re:Benefits of named return values in Go by weberc2 on Friday June 30, 2017 @09:59AM (#14670741)
I thought the SSA backend was not replacing the Plan9 assembly, but that it was a phase that happened before the assembly was output (presumably SSA is a phase and not an IR?).
Re:Benefits of named return values in Go by hornetblack on Wednesday July 05, 2017 @04:59AM (#14700868)
Oh. That actually make more sense.
Re:Benefits of named return values in Go by noselasd on Friday June 30, 2017 @12:30PM (#14671785)
It does if you use gcc though - both variants of the code in question here compiles to the same assembly (at least with gcc 7.1 on x86-84)
Re:Benefits of named return values in Go by wlll on Friday June 30, 2017 @11:46AM (#14671457)
I think accusations of premature optimisation might be a little unfair here.

Ignoring the style issues for a second (I'll pick that up later), if I'm looking at some code and there are two equally viable alternative ways of writing it, one of which saves a chunk of memory* or is faster then it's just perverse to choose the path of larger/slower code. I do this with regular expressions/string functions. I see people use regexes a lot, but the tool I reach for first when doing string operations are the built-in string functions, eg. https://golang.org/pkg/strings/#Contains or https://ruby-doc.org/core-2.4.0/String.html#method-i-start_w.... I'm not optimising, I'm just not de-optimising.

Back to the style issue, at this point if you really feel strongly about the way it looks in the editor, or in documentation then I can see why you would choose one method over the other, choosing the less desirable, but theoretically faster code would absolutely be a case of premature optimisation. I personally don't have a particular preference either way however, and feel like the reasons outlined in the style guide are rather fragile. So ultimately, if I pick up some code full of named return values I don't think it would bother me, in the same way that code that uses none, or mixes them where someone thought it appropriate doesn't bother me either.

* There may be benefits other than just disk/distribution size. Many years ago I read about the benefits of small binaries, something relating to CPU caches, though that may be out of date now and I forget the details.

Re:Benefits of named return values in Go by hyperpape on Friday June 30, 2017 @12:03PM (#14671582)
The issue is that with the string/regex issue, there's a good reason that the string operation should be faster. Maybe a "sufficiently smart compiler" could optimize it in some cases of static regexes, but it's at least complicated. In cases where the regex is dynamically determined, even the sufficiently smart compiler probably can't optimize away the regex.

In contrast, the case in this article seems like table stakes for an optimizing compiler. It's just not eliminating common subexpressions. There's no reason to contort your code around something that should automatically happen.

Re:Benefits of named return values in Go by wlll on Friday June 30, 2017 @12:30PM (#14671792)
When I've benchmarked regexes before (Ruby ISTR) there have been situations where the regex engine optimised the code to be comparable to string functions, I can't remember the details though. Just for fun I benchmarked =~ /\Asomething/ and start_with?("something"), a situation that seems could be optimisable by the regex engine, but the string function is still faster (Ruby 2.3.1):

start_with: 5703143.1 i/s regex: 2821224.4 i/s - 2.02x slower

That aside:

> There's no reason to contort your code around something that should automatically happen.

Absolutely, but it's a question of style at that point. "Contorting your code" suggests using a less desirable style/syntax for some gain, and I'd agree would be premature optimisation. If you're just making a choice between two styles that you consider to be pretty much equal then it's just pragmatic.

edit: Forgot to add, yes, I agree that this should happen automatically in the compiler :)

Re:Benefits of named return values in Go by hyperpape on Friday June 30, 2017 @03:45PM (#14673207)
My point of view is that if you're two equally good styles based on the compiler, it's contorting your code. One doesn't have to be better than the other, if you can't make that choice between semantically identical options, you're conforming to an arbitrary standard.
Re:Benefits of named return values in Go by obstinate on Friday June 30, 2017 @12:03AM (#14668566)
I dunno. A 30% code size win is non-trivial. I'm all for filing an issue first and seeing how likely it is that there is uptake from the dev team and desire to fix the problem. However, if no fix is forthcoming . . . code size has fairly well known effects on performance.
Re:Benefits of named return values in Go by gizmo686 on Friday June 30, 2017 @12:22AM (#14668620)
A 30% code size reduction in code that does little other than construct and return a value. I have certainly seen individual functions where this is the case, but across an entire program, you will not get anywhere near 30% size reduction.

Having said that, this is certainly something that should be fixed in the compiler.

On a related note, in the final assembly, the compiler could also have optimized the 4 RETs into 1, then optimized away all of the conditionals, turning the sample code into the equivilent of "return objectInfo()"). Of course, in a real example, these optimizations would not be possible; but they do show that these reduced cases are not the best way of benchmarking performance.

Re:Benefits of named return values in Go by lobster_johnson on Friday June 30, 2017 @12:34AM (#14668647)
Fixing it in the compiler will fix it for everyone, however, which is a big argument for fixing it upstream.
Re:Benefits of named return values in Go by pjmlp on Friday June 30, 2017 @02:28AM (#14668933)
Unless it actually impacts the use case of the application, and has been confirmed by a profiler that is indeed the case, it is just cargo cult optimizations.