Sep 26, 2020

[Go] 'defer' optimizing notes

Proposal:
https://go.googlesource.com/proposal/+/refs/heads/master/design/34481-opencoded-defers.md


Optimizing:

  1. try not to run defer in a loop(for), which compiler will fallback to traditional defer chain implement
  2. try to have less exit point inside a function, which compiler will fallback to traditional defer chain implement


Proposal by Example:

defer f1(a)
if cond {
 defer f2(b)
}
body...
compiles to:
deferBits |= 1<<0
tmpF1 = f1
tmpA = a
if cond {
 deferBits |= 1<<1
 tmpF2 = f2
 tmpB = b
}

body...

exit:
if deferBits & 1<<1 != 0 {
 deferBits &^= 1<<1
 tmpF2(tmpB)
}

if deferBits & 1<<0 != 0 {
 deferBits &^= 1<<0
 tmpF1(tmpA)
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.