Open
Description
func f(b []byte) byte {
b = b[3:]
return b[4]
}
We compile this to something like (bounds checks omitted):
p = b.ptr
inc = 3
if b.cap == 3 {
inc = 0
}
p += inc
return *(p+4)
The if
in the middle is there to make sure we don't manufacture a pointer to the next object in memory. But the resulting pointer is never exposed to the garbage collector, so that if
is unnecessary. Manufacturing a pointer to the next object in memory is ok if that pointer is never spilled at a safe point. (Bounds checks will make sure such a pointer is never actually used.)
Unfortunately, I don't see an easy way to do this optimization in the current compiler. Marked as unplanned.
See #14849