Unique GO language features

259 views
Skip to first unread message

joseph.p...@gmail.com

unread,
Aug 21, 2020, 1:16:17 PM8/21/20
to golang-nuts
I really like the 'defer' statement and think it would be a useful addition to other programming languages.

The feature where GO performs escape analysis and promotes stack variables to the heap: Did that originate with GO or was it first implemented elsewhere?

Thanks,

Joe

Ian Lance Taylor

unread,
Aug 21, 2020, 3:33:56 PM8/21/20
to joseph.p...@gmail.com, golang-nuts
On Fri, Aug 21, 2020 at 10:16 AM joseph.p...@gmail.com
<joseph.p...@gmail.com> wrote:
>
> I really like the 'defer' statement and think it would be a useful addition to other programming languages.

I believe that Swift has also added a defer statement, which I assume
was based on the idea in Go.

> The feature where GO performs escape analysis and promotes stack variables to the heap: Did that originate with GO or was it first implemented elsewhere?

That has been implemented in other languages, notably Java.

Ian

Jesper Louis Andersen

unread,
Aug 24, 2020, 7:43:33 AM8/24/20
to joseph.p...@gmail.com, golang-nuts
On Fri, Aug 21, 2020 at 7:16 PM joseph.p...@gmail.com <joseph.p...@gmail.com> wrote:


The feature where GO performs escape analysis and promotes stack variables to the heap: Did that originate with GO or was it first implemented elsewhere?


As Ian says, this is a "rather old" trick in the bag of tricks compiler writers use. However, note that it interacts with other features of a language which make it easier or harder to get a good escape analysis approximation to work.

It is also important to stress that you first do the escape analysis part and obtain information about what escapes a given scope. With that information in hand, you can then perform various optimizations when the analysis says they are safe to perform. You can then perform scalar replacement as an optimization, promoting field access to a temporary variable, which in turn means you can trigger other optimizations. In many cases you end up moving the fields to CPU registers, so it isn't even on the stack anymore.

Functional languages have a long history of utilizing escape analysis. The idea is to figure out if a closure, which is extremely common, needs allocation on the heap or stack, with the stack in preference. One key complication is that the analysis usually needs to work at a higher order, due to the commonality of higher-order-functions (i.e., functions where input arguments are functions and the output of the function also contains functions).


--
J.
Reply all
Reply to author
Forward
0 new messages