On Fri, Nov 2, 2012 at 12:28 AM, Michael Jones wrote:

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
Thank you for sharing this great micro optimization trick.
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. ;-)
Yeah, this is a reasonable and practical use case of destructuring

However, i think that the manual sub-expression elimination also
helps to reduce cognition load so it's fairly useful and i don't expect
the compiler's help with this.
IMO, the manual scoping to reduce registerization pressure is a work
around, and i hope the compiler can handle this automatically in the

columns, c, r = destructure(exact.stack[k])
sp := &exact.stack[k]
columns, c, r = sp.columns, sp.c, sp.r

I think the latter form is quite acceptable (no error when struct fields
are reordered, thus adhere to the explicitness principle of Go)


Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 15 of 22 | next ›
Discussion Overview
groupgolang-nuts @
postedOct 31, '12 at 9:15a
activeNov 2, '12 at 7:51a



site design / logo © 2021 Grokbase