Will Newton
unread,Sep 2, 2015, 4:04:21 AM9/2/15Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Paul Borman, Tim K, golang-nuts, Ian Lance Taylor, David M.
On 2 September 2015 at 02:55, 'Paul Borman' via golang-nuts
<
golan...@googlegroups.com> wrote:
> If Lock panics then the defer is the least of your concerns.
>
> Fortunately, lock does not panic, it does not return an error. Lock does
> what it says it does. I have no interest in coding around a hypothetical
> Lock implementation that we don't use.
>
> You are free to order those two statements how ever you want. For an
> uncontested lock there is no difference in time. For a contested lock then
> doing the defer first will shorten the critical section (not by much, but by
> an amount you can measure). You are free to do them in either order, but
> both are safe (unless your program has already lost memory safety, at which
> point it does not matter) and functionally equivalent.
One measurable cost of these types of micro-optimization is that it
looks odd, so programmers who have not seen the idiom before may
change the statements back to the "right" order unless you add
comments explaining the idiom to every call site. In any case I would
expect truly performance critical critical sections to not use defer
at all.
My advice would be: do it in the natural order unless it measurably
improves performance to do otherwise, and then comment it.
--
Will Newton
Software - Cocoon (
http://cocoon.life)