He's actually asking for destructing assignment, which is unsupported in Go.
right. if we had a tuple type, this destructuring assignment would bepossible.
On Thursday, 1 November 2012 09:52:13 UTC+2, minux wrote:right. if we had a tuple type, this destructuring assignment would be
possible.Are tuples being considered for Go?
Have you seen any reason for Go to get Tuples?
I agree with Kevin. I can't see how I would need this feature (I haven't so far, anyway). How often do you return a variable number of arguments and don't want a slice? Why wouldn't you want a slice?
I would have used this in one of my programs. I converted a core
recursion to a self managed combination of return stack and gotos for
a 34% speedup. My stack has three variables that I need to restore
before the goto on each "return." I tried many different ways of
coding the push and the pop: as an array of structures with three
values, as three arrays of values, etc. The performance in each case
is limited by either the cost of the array bounds test (either one or
three) or the cost of the packing and unpacking of the structure
(three assignments or structure assignment of a struct literal. The
three cases are:
{
sp := &exact.stack[k]
columns = sp.columns
c = sp.c
r = sp.r
}
/*
columns = exact.stack[k].columns
c = exact.stack[k].c
r = exact.stack[k].r
*/
The uncommented version is the fastest by far. The braces are needed
to help the compiler handle register pressure; the code is 2% slower
without them. What I wanted was this:
columns, c, r = destructure(exact.stack[k])
which would let the compiler do one array bounds check, one index
operation, and three direct moves. But I did not ask for it. I always
seem to be asking for things that nobody else wants. For example, I
would not put shaving a microsecond from the compile time above
forcing programmers to try an exponential number of brace
installations to manual show variable lifetimes. ;-)
{
sp := &exact.stack[k]
columns = sp.columns
c = sp.c
r = sp.r
}
Is it necessarily slower?A compiler could reasonably see that k is not changed and only do bounds checking once.Explicit and fast are both good Go principles :)
Do any of the Go compilers do any optimisations that involve avoiding unnecessary bounds checking? And what do you consider reasonable? I wouldn't expect this to be optimised:
columns = exact.stack[k].columnsc = exact.stack[k].c
r = exact.stack[k].r
But this seems a bit more likely to me:columns, c, r = stack[k].columns, stack[k].c, stack[k].r
... since I think the evaluation of the left and right side could reasonably be micro-optimised.
On Thursday, 1 November 2012 17:28:51 UTC+1, Michael Jones wrote:{
sp := &exact.stack[k]
columns = sp.columns
c = sp.c
r = sp.r
}Wow, I never would have thought of doing something like that (including the manual scoping). Do you have a blog or a summary page where you collect micro-optimisations like this? Preferably with the reasoning that made you discover them in the first place? It seems to be kind of your thing.
On Thursday, 1 November 2012 17:53:59 UTC+1, Peter wrote:Is it necessarily slower?A compiler could reasonably see that k is not changed and only do bounds checking once.Explicit and fast are both good Go principles :)Do any of the Go compilers do any optimisations that involve avoiding unnecessary bounds checking? And what do you consider reasonable? I wouldn't expect this to be optimised:columns = exact.stack[k].columnsc = exact.stack[k].c
r = exact.stack[k].r
But this seems a bit more likely to me:columns, c, r = stack[k].columns, stack[k].c, stack[k].r
... since I think the evaluation of the left and right side could reasonably be micro-optimised
I expect gccgo will do the right thing here.
--